自定义 Github Action 库实战详解
作者:小鑫同学 时间:2022-04-07 13:19:00
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}}
来源:https://juejin.cn/post/7147460506645692430
标签:Github,Action,自定义库
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python运算符重载用法实例分析
2023-03-18 07:28:11
Python按行读取文件的实现方法【小文件和大文件读取】
2021-12-11 01:31:08
用asp编写文档搜索页面
2008-01-13 07:04:00
使用python实现一个简单ping pong服务器
2022-10-25 03:38:48
![](https://img.aspxhome.com/file/2023/7/94027_0s.jpg)
python自动化生成IOS的图标
2021-04-26 19:16:34
Python在不同目录下导入模块的实现方法
2022-03-12 09:34:52
python使用rstrip函数删除字符串末位字符
2023-06-10 16:59:56
Python3 json模块之编码解码方法讲解
2021-07-15 17:29:15
pandas处理csv文件的方法步骤
2022-05-31 10:51:04
![](https://img.aspxhome.com/file/2023/3/83803_0s.png)
详谈配置phpstorm完美支持Codeigniter(CI)代码自动完成(代码提示)
2023-09-06 14:34:52
![](https://img.aspxhome.com/file/2023/4/55644_0s.jpg)
python装饰器底层原理详解
2021-12-31 08:48:32
![](https://img.aspxhome.com/file/2023/6/75216_0s.png)
代码详解Python的函数基础(1)
2023-08-16 17:47:22
在python中list作函数形参,防止被实参修改的实现方法
2022-11-15 19:27:25
![](https://img.aspxhome.com/file/2023/7/87777_0s.jpg)
Python数据分析入门之教你怎么搭建环境
2023-03-15 12:21:11
![](https://img.aspxhome.com/file/2023/3/115833_0s.png)
Access数据库导入Mysql的方法之一
2023-11-17 12:41:41
MySQL表设计优化与索引 (八)
2010-10-25 19:46:00
python3+pyqt5+itchat微信定时发送消息的方法
2022-02-28 04:31:49
![](https://img.aspxhome.com/file/2023/4/69494_0s.jpg)
python判断文件夹内是否存在指定后缀文件的实例
2021-12-27 23:05:19
go和python变量赋值遇到的一个问题
2023-11-15 01:54:40
利用Python实现自动工作汇报的脚本分享
2022-05-31 10:55:37
![](https://img.aspxhome.com/file/2023/8/128498_0s.png)