欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

自定義?Github?Action?庫實(shí)戰(zhàn)詳解

 更新時(shí)間:2022年09月26日 08:47:45   作者:小鑫同學(xué)  
這篇文章主要為大家介紹了自定義?Github?Action?庫實(shí)戰(zhàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

auto-push-oss Action

雖然在 Github 市場有推送 OSS 相關(guān)的 Action,但是我還是選擇改造我運(yùn)行了好多年的腳本來自定義符合自己要求的 Action 庫。

編寫步驟:

  • 添加依賴、編譯腳本、action.yml配置
  • 編寫自述文檔
  • 編寫indnex.js腳本

添加依賴、編譯腳本、action.yml配置:

添加必要依賴:

"@actions/core": "^1.9.1"		// 讀取 yml 參數(shù)
"@vercel/ncc": "^0.34.0"    // 打包腳本
"ali-oss": "^6.17.1"        // ali-oss依賴

添加編譯腳本:

"build": "ncc build index.js --license licenses.txt"

編寫 action.yml 配置文件:

name: "auto-push-oss"
description: "自動(dòng)推動(dòng)目錄到 OSS"
# 定義輸入?yún)?shù)
inputs:
  root:
    description: "待推送路徑"
    required: true
  bucket:
    description: "oss bucket"
    required: true
  region:
    description: "oss region"
    required: true
  accessKeyId:
    description: "oss accessKeyId"
    required: true
  accessKeySecret:
    description: "oss accessKeySecret"
    required: true
runs:
  # 腳本運(yùn)行環(huán)境(按官方文檔給的12版本來使用)
  using: "node12"
  # 腳本執(zhí)行入口(這里我們要用@vercel/ncc編譯)
  main: "dist/index.js"

編寫自述文檔:

自述文檔需要說明這個(gè) Action 的主要作用、需要配置的參數(shù)和最小使用的例子~

auto-push-oss

方便將常見的 Vue 項(xiàng)目,VuePress 項(xiàng)目構(gòu)建到根目錄的 dist 文件夾推送到指定從 oss 桶的根目錄,特別適合在 oss 托管 VuePress 博客~

Inputs

參數(shù)描述
root待推送文件夾
bucketoss bucket
regionoss region
accessKeyIdoss accessKeyId
accessKeySecretoss accessKeySecret

Example usage

uses: OSpoon/auto-push-oss@main
with:
  root: public
  bucket: it200
  region: oss-cn-beijing
  accessKeyId: ${{secrets.accessKeyId}}
  accessKeySecret: ${{secrets.accessKeySecret}}

編寫indnex.js腳本:

提供path、fs、ali-oss 和獲取 yml 參數(shù)的@actions/core依賴~

const path = require("path");
const fs = require("fs");
const core = require("@actions/core");
const OSS = require("ali-oss");

通過@actions/core提供的getInput來獲取 yml 配置的參數(shù)變量~

const root = core.getInput("root");
const bucket = core.getInput("bucket");
const region = core.getInput("region");
const accessKeyId = core.getInput("accessKeyId");
const accessKeySecret = core.getInput("accessKeySecret");

OSS 推送文件主腳本

// TODO 必要依賴
// TODO 接收輸入?yún)?shù)
const client = new OSS({
  bucket,
  region,
  accessKeyId,
  accessKeySecret,
});
const rootPath = root || "dist";
const isHave = fs.existsSync(rootPath);
if (!isHave) {
  throw new Error("路勁不存在");
}
let filepaths = [];
let putCount = 0;
function readFileSync(filepath) {
  let files = fs.readdirSync(filepath);
  files.forEach((filename) => {
    let p = path.join(filepath, filename);
    let stats = fs.statSync(p);
    if (stats.isFile()) {
      filepaths.push(p);
    } else if (stats.isDirectory()) {
      readFileSync(p);
    }
  });
}
function put(filepath) {
  const p = filepath.replace(rootPath, "").substr(1);
  return client.put(p.replace("\", "/"), filepath);
}
async function update() {
  try {
    // 遞歸獲取所有待上傳文件路徑
    readFileSync(rootPath);
    let retAll = await filepaths.map((filepath) => {
      putCount++;
      console.log(`任務(wù)添加: ${path.basename(filepath)}`);
      return put(filepath);
    });
    Promise.all(retAll).then((res) => {
      const resAll = res.map((r) => {
        return r.res.statusCode === 200;
      });
      if (Object.keys(resAll).length === putCount) {
        console.log("發(fā)布成功");
      }
    });
  } catch (e) {
    console.log(e);
  }
}
// 上傳發(fā)布
update();

use auto-push-oss

下面這份配置就是將網(wǎng)站打包并推送 OSS 的工作流程,當(dāng)監(jiān)測到新代碼 PUSH 到 Github 后就開始執(zhí)行auto-push-2-oss工作流,分別是:

    • 第一步使用actions/checkout@v2拉取代碼;
    • 第二步執(zhí)行npm install && npm run build安裝依賴并輸出網(wǎng)站資源;
    • 第三步使用OSpoon/auto-push-oss@main推送網(wǎng)站資源到 OSS;

auto-push-oss@main需要配置我們在自述文檔中提到的幾個(gè)必要參數(shù)需要通過 with 配置,其中accessKeyId和accessKeySecret由于涉及到 OSS 的相關(guān)秘鑰,不建議也不應(yīng)該明文展示到 Github,所以需要使用到項(xiàng)目級(jí)別的環(huán)境變量。

name: push-2-oss
on: [push]
jobs:
  auto-push-2-oss:
    runs-on: ubuntu-latest
    steps:
      - name: checkout
        uses: actions/checkout@v2
      - name: install & build 
        run: npm install && npm run build
      - name: push public oss
        uses: OSpoon/auto-push-oss@main
        with:
          root: public
          bucket: it200
          region: oss-cn-beijing
          accessKeyId: ${{secrets.accessKeyId}}
          accessKeySecret: ${{secrets.accessKeySecret}}

總結(jié)

編寫完 Action 后成功也接入了 workflows ,往后就不再重復(fù)的執(zhí)行構(gòu)建命令和發(fā)布腳本了,只需要將修改的代碼 PUSH 到 Github 后面的工作將自動(dòng)完成~

本文項(xiàng)目已推送至GitHub,歡迎克隆演示:git clone git@github.com:OSpoon/auto-push-oss.git

以上就是自定義 Github Action 庫實(shí)戰(zhàn)詳解的詳細(xì)內(nèi)容,更多關(guān)于Github Action 庫自定義的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論