如何本地运行和测试Github Actions?

前言
一般来说,使用 github actions 都需要在 repo 里完成相应的 event 才能够触发(比如push/pull_request等),然后在 github 提供的服务器(容器)中运行。这样有两个缺点:
每次都需要在 repo 完成相应的 evenet,操作较为费时。
一个 event 可能会触发多个 action,并且在 github 的服务器上运行时需要排队,运行较为耗时。
所以,本文介绍如何在本地运行 github actions 来解决上述两个问题,主要有以下两个依赖:
docker desktop on windows(backend 是 wsl)
act
依赖安装
docker desktop on windows
安装 docker desktop on windows 前需要先安装 wsl,安装 wsl 的操作可以参考官方文档
安装 docker desktop on windows,
进入 docker desktop on windows 时可能会一直停留在 starting the docker engine 界面,可以考虑以下解决方法:
重启 docker 服务
net stop com.docker.service
net start com.docker.service
先点击右上角登录 docker 账号
act
act 是在本地运行 github actions 的工具,它依赖于 docker。安装可以参考 nektos/act: run your github actions locally ,或者直接在这个仓库的 release 当中下载,只有一个 windows 可执行文件(exe)。
本地运行 github actions
act 使用说明
command structure:act [] [options]
if no event name passed, will default to on: push
if actions handles only one event it will be used as default instead of on: push
list all actions for all events:act -l
list the actions for a specific event:act workflow_dispatch -l
list the actions for a specific job:act -j test -l
run the default (push) event:act
run a specific event:act pull_request
run a specific job:act -j test
collect artifacts to the /tmp/artifacts folder:act --artifact-server-path /tmp/artifacts
run a job in a specific workflow (useful if you have duplicate job names)act -j lint -w .github/workflows/checks.yml
run in dry-run mode:act -n
enable verbose-logging (can be used with any of the above commands)act -v
示例:rt-thread 的 github actions
进入 rt-thread 源代码目录(只要进入第一层即可,act 会自动搜索 .github/workflow 目录)
使用act -l​查看当前目录下的 action
可以看到有重名(job id)的 action,所以需要指定要运行的 action(act -j test -w ..githubworkflowsaction_tools.yml​)
如果运行时拉不下镜像可以修改一下 daemon.json 中的镜像源
registry-mirrors: [
https://registry.docker-cn.com,
http://hub-mirror.c.163.com,
https://docker.mirrors.ustc.edu.cn
]
运行结果如下:

云天励飞携手奥比中光推动3D视觉应用赋能AIoT
基于MC34063A的LED用恒流驱动电路的设计
数据可视化用来创造一条快速认识数据集的捷径
电力系统中的非线性负载是怎样引入谐波的?
LTM8050降压型μModule转换器的反相输出特性分析
如何本地运行和测试Github Actions?
第六代Wi-Fi技术或将比5G网络还厉害
新东风标致408体验是如何应试新“四大件”标准的呢?
疫情之后,人工智能将大有可为
汽车电子车窗防夹技术的主要类型及控制系统架构
运算放大器基本公式
小米生态链于5月28日上线了一款米家投影仪
赛灵思发布自适应计算加速平台芯片系列Versal
电源应用中的单片线性稳压器
基于ZYNQ7000的交叉编译工具链Qt+OpenCV+ffmpeg等库支持总结
工业自动化产业所需的机器人关节用伺服电机
汽车引发的业务转变将会带动汽车金融世界的改变
新页微电子15W SoC Tx芯片是什么?性能如何?
AMB300低压母线红外测温装置
Redmi手机在独立后依旧保持了一贯的宗旨