起因是以前在做 issues-helper 推广的时候一直没仔细钻研过具体原理,就想着把这个链路跑通顺便自己做一个玩具。
玩具目标是:指定项目的有新的 issue 时打印出一张标签来提醒我有新的任务了。
流程
项目 -> 注册本地 runner
创建issue -> 触发 github action -> 本地 runner 触发 -> 打印一张贴纸
实践
(一)注册 runner
- 项目点击 Action 注册一个 Self hosted runners
- 按照流程在本地注册 runner
- 检查是否在线
(二)触发 issue 指定动作
- 设计,我想在 issue 创建的时候进行提醒,并且在分配给某位同事的时候打印出一张贴纸,然后把任务贴在同事电脑上
- 编写 action 在issues 有指定动作时触发,由于本地已经有 node 环境了,脚本不用编写安装 Node环境
yaml
name: CI
on:
issues:
types: [opened, edited, closed, reopened, assigned, unassigned]
jobs:
issue-create:
runs-on: self-hosted
steps:
- name: Check Out
uses: actions/checkout@v2
- name: Issue Create
uses: ./
with:
action: ${{ github.event.action }}
issue: ${{ toJson(github.event.issue) }}
repo: ${{ toJson(github.event.repository) }}
- 在根路径(uses: ./)下创建 action.yml 作为运行配置
vbnet
name: 'Issue to Print'
description: 'test action'
author: 'ajuner'
runs:
using: 'node16'
main: 'index.js'
inputs:
action:
description: 'Action name'
issue:
description: 'Issue number'
repo:
description: 'Repository name'
- 在 index.js 中接收 action 参数
ini
const action = core.getInput("action");
const issue = JSON.parse(core.getInput("issue"));
console.log("action", action);
console.log("issue", issue);
(三)编写打印相关代码
使用条码热敏打印机 TSPL 指令
ini
const core = require("@actions/core");
const iconv = require("iconv-lite");
const escpos = require("node-escpos-win");
const usb = escpos.GetDeviceList("USB");
const list = usb.list.filter(
(item) => item.service === "usbprint" || item.name === "USB 打印支持"
);
const printer = list[0];
if (!printer) {
console.warn("没有连接usb打印机");
return;
}
const action = core.getInput("action");
const issue = JSON.parse(core.getInput("issue"));
console.log("action", action);
console.log("issue", issue);
const cmds = [
"SIZE 45 mm,60 mm",
"GAP 2 mm,0 mm",
"DENSITY 15",
"SPEED 4",
"DIRECTION 1",
"CLS",
];
const actionText = {
opened: `有新的issue #${issue.number}`,
closed: `issue #${issue.number} 已关闭`,
reopened: `issue #${issue.number} 已重新打开`,
edited: `issue #${issue.number} 有更新`,
assigned: `issue #${issue.number} 已分配`,
unassigned: `issue #${issue.number} 已取消分配`,
};
cmds.push(`TEXT 0,20,"TSS24.BF2",0,1,1,"${actionText[action]}"`);
cmds.push(`TEXT 0,60,"TSS24.BF2",0,1,1,"标题:${issue.title}"`);
cmds.push(`TEXT 0,100,"TSS24.BF2",0,1,1,"用户:${issue.user.login}"`);
if (!!issue?.assignees?.length) {
const logins = issue.assignees.map((item) => item.login).join("、");
cmds.push(`TEXT 0,180,"TSS24.BF2",0,1,1,"任务分配者:${logins}"`);
}
cmds.push("PRINT 1", "");
const content = iconv.encode(cmds.join("\r\n"), "gbk");
const res = escpos.Print(printer.path, content);
console.log(res);
(四)测试issue
查看打印效果
(五)揉掉这个需求然后丢到垃圾桶里
资源
源代码:github.com/ajuner/issu... (node_modules比较小,不想在action里加install流程,别喷)
相关资料: