返回

如何解决GitHub Actions 运行缓慢问题?

开发配置

一般来说,使用 Github Actions 都需要在 Repo 里完成相应的 event 才能够触发(比如push/pull_request等),然后在 Github 提供的服务器(容器)中运行。这样有两个缺点:

每次都需要在 Repo 完成相应的 evenet,操作较为费时。
一个 event 可能会触发多个 Action,并且在 Github 的服务器上运行时需要排队,运行较为耗时。
所以,本文介绍如何在本地运行 Github Actions 来解决上述两个问题,主要有以下两个依赖:

act

act 是在本地运行 Github Actions 的工具,它依赖于 Docker。安装可以参考 nektos/act: Run your GitHub Actions locally 🚀,或者直接在这个仓库的 release 当中下载,只有一个 Windows 可执行文件(exe)。

# Command structure:
act [<event>] [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论坛用户「dejavudwh」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://club.rt-thread.org/ask/article/a18a22f8eac4c7c1.html

示例:RT-Thread 的 Github Actions

1、进入 RT-Thread 源代码目录(只要进入第一层即可,act 会自动搜索 .github/workflow 目录)

使用act -l查看当前目录下的 Action

screenshot_image.png

可以看到有重名(Job ID)的 Action,所以需要指定要运行的 Action(act -j test -W .\.github\workflows\action_tools.yml
如果运行时拉不下镜像可以修改一下 daemon.json 中的镜像源

"registry-mirrors": [
   "https://registry.docker-cn.com",
   "http://hub-mirror.c.163.com",
   "https://docker.mirrors.ustc.edu.cn"
]

运行结果如下:

screenshot_image.png

screenshot_51a.png