日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

自定義?Github?Action?庫實戰詳解_相關技巧

作者:小鑫同學 ? 更新時間: 2022-11-15 編程語言

auto-push-oss Action

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

編寫步驟:

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

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

添加必要依賴:

"@actions/core": "^1.9.1"		// 讀取 yml 參數
"@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: "自動推動目錄到 OSS"
# 定義輸入參數
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:
  # 腳本運行環境(按官方文檔給的12版本來使用)
  using: "node12"
  # 腳本執行入口(這里我們要用@vercel/ncc編譯)
  main: "dist/index.js"

編寫自述文檔:

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

auto-push-oss

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

Inputs

參數 描述
root 待推送文件夾
bucket oss bucket
region oss region
accessKeyId oss accessKeyId
accessKeySecret oss 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 參數的@actions/core依賴~

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

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

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 接收輸入參數
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(`任務添加: ${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("發布成功");
      }
    });
  } catch (e) {
    console.log(e);
  }
}
// 上傳發布
update();

use auto-push-oss

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

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

auto-push-oss@main需要配置我們在自述文檔中提到的幾個必要參數需要通過 with 配置,其中accessKeyId和accessKeySecret由于涉及到 OSS 的相關秘鑰,不建議也不應該明文展示到 Github,所以需要使用到項目級別的環境變量。

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}}

總結

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

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


原文鏈接:https://juejin.cn/post/7147460506645692430

欄目分類
最近更新