使用 Act 本地运行 GitHub Actions

简介

GitHub Actions 为仓库开发者提供了执行定制化 Job 的能力,开发者可以使用各种 Job 基于代码仓库运行测试、构建、发布等操作,实现 CI/CD 等工作流。

outside_default.png

这些 Job 默认运行在 GitHub 提供的 runner 中,但是由于仓库众多,配置了 actions 的代码仓库未必能得到足够的资源 (runner) 来快速、高效的运行代码仓库的 Actions。

一个可行的方式是绑定自己的私有 runner 到代码仓库,这样每次执行 Job 时都会在自己的 runner 上,详情见 self-hosted runners[1]

另一个方案就是 act[2], 可以在电脑上,在代码仓库目录下,直接本地运行 Actions 中的 Job。方便开发者快递本地迭代和测试,确认没问题了再更新到主仓库,减少主仓库执行 Actions 的次数。

act 安装

act 使用 go 语言开发,可以直接下载编译好的二进制文件,或者使用 go install 安装。

  1. 下载二进制文件

进入 act 的 releases[3] 页面找到最新版,笔者这里是 v0.2.63

57836e56d5d1efaa69b356fb0cd5cb66.jpeg

选择对应平台的文件,笔者这里是 Mac M 芯片,选择act_Darwin_arm64.tar.gz, 下载解压后,移动到 PATH 目录即可。

  1. go install 安装

首先下载代码仓库

git clone https://github.com/nektos/act.git

进入到 act 目录,运行 go install

$ go install
go: downloading github.com/spf13/cobra v1.8.1
go: downloading golang.org/x/term v0.21.0
go: downloading google.golang.org/protobuf v1.34.2
go: downloading github.com/docker/cli v26.1.4+incompatible
go: downloading golang.org/x/sys v0.21.0

go 会自动构建二进制文件,并把构建好的二进制文件放到 gopath 中,这时查看 act 版本:

$ act --version
act version 0.2.63

act 使用

  1. 触发条件

# 运行 push 触发的 action
act push

# 运行 pull_request 触发的 action
act pull_request
  1. 运行指定 Job

# -j, --job 后面跟 job id
act -j 'test'
  1. 变量与机密内容

# 通过 --var 指定单个 var
act --var VARIABLE=somevalue
# 通过 --var-file 指定文件中的多个变量
act --var-file my.variables
# 通过 -s 指定单个保密值
act -s MY_SECRET=somevalue
# 通过 --secret-file 指定文件中的多个保密值
act --secret-file my.secrets

act runner

act 同样有 runner 的概念,支持在容器和宿主机两个平台作为 runner 运行 Actions Job。在容器中运行需要指定容器镜像,默认镜像是 catthehacker/ubuntu:act-latest。宿主机支持 Mac、Linux 和 Windows。需要注意,容器的指令集架构要和宿主机架构一致,如果宿主机是 Windows 系统 Intel amd64 架构,那么镜像也应该是 amd64 架构。

如果 act 默认提供的镜像不满足自己的需求,可以自己构建一个 runner 镜像,在运行 act 时通过 -P或者 --platform 指定镜像:

act -P ubuntu-latest=adolphlwq/act-runner-ubuntu:22.04

act 中的 runner 和 GitHub Runners 一致(表格来自 Standard GitHub-hosted runners for Public repositories[4]):

Virtual MachineProcessor (CPU)Memory (RAM)Storage (SSD)Workflow labelNotes
Linux416GB14GBubuntu-latest, ubuntu-24.04 [Beta], ubuntu-22.04, ubuntu-20.04The ubuntu-latest label currently uses the Ubuntu 22.04 runner image.
Windows416GB14GBwindows-latest, windows-2022, windows-2019The windows-latest label currently uses the Windows 2022 runner image.
macOS314GB14GBmacos-12 or macos-11The macos-11 label has been deprecated and will no longer be available after 28 June 2024.
macOS414GB14GBmacos-13NA
macOS3(M1)7GB14GBmacos-latest or macos-14The macos-latest label currently uses the macOS 14 runner image.

**Workflow label **指定不同平台的 Runner,act 中通过 -P label=image 来指定不同平台使用什么容器镜像运行。如果没有镜像,则可以通过 -P label=self-hosted表示 act 直接在宿主机运行 Actions[5]

act -P ubuntu-latest=-self-hosted
act -P windows-latest=-self-hosted
act -P macos-latest=-self-hosted

act runner 的局限

act runner 不是万能的。首先表示 runner 的平台标签 (ubuntu-latest 等)要和宿主机的指令架构一致。比如在 M 芯片的 苹果机器上,是不能运行

# Apple M 芯片不能运行 amd 64 芯片镜像的
$ act -P ubuntu-latest=adolphlwq/act-runner-ubuntu:22.04
INFO[0000] Using docker host 'unix:///var/run/docker.sock', and daemon socket 'unix:///var/run/docker.sock'
WARN  ⚠ You are using Apple M-series chip and you have not specified container architecture, you might encounter issues while running act. If so, try running it with '--container-architecture linux/amd64'. ⚠
INFO[0000] Start server on http://192.168.6.234:34567
[release/generate-windows-rocm] 🚧  Skipping unsupported platform -- Try running with `-P windows=...`
[release/build-linux-arm64    ] 🚧  Skipping unsupported platform -- Try running with `-P linux-arm64=...`
[release/build-darwin         ] 🚧  Skipping unsupported platform -- Try running with `-P macos-12=...`
[release/generate-windows-cpu ] 🚧  Skipping unsupported platform -- Try running with `-P windows=...`
[release/generate-windows-cuda] 🚧  Skipping unsupported platform -- Try running with `-P windows=...`
[release/build-linux-amd64    ] 🚧  Skipping unsupported platform -- Try running with `-P linux=...`

报错** Skipping unsupported platform**

上面的命令在 intel amd64 架构的 Apple 笔记本上是能正确运行的:

# Apple amd64 架构 芯片能运行 amd 64 芯片镜像
$ act -j 'test' pull_request
INFO[0000] Using docker host 'unix:///var/run/docker.sock', and daemon socket 'unix:///var/run/docker.sock'
INFO[0000] Start server on http://192.168.6.199:34567
[test/test-1] 🚀  Start image=adolphlwq/act-runner-ubuntu:22.04
[test/test-3] 🚀  Start image=adolphlwq/act-runner-ubuntu:22.04
[test/test-2] 🚧  Skipping unsupported platform -- Try running with `-P macos-latest=...`
[test/test-1]   🐳  docker pull image=adolphlwq/act-runner-ubuntu:22.04 platform= username= forcePull=true
[test/test-3]   🐳  docker pull image=adolphlwq/act-runner-ubuntu:22.04 platform= username= forcePull=true
[test/test-1]   🐳  docker create image=adolphlwq/act-runner-ubuntu:22.04 platform= entrypoint=["tail" "-f" "/dev/null"] cmd=[] network="host"
[test/test-3]   🐳  docker create image=adolphlwq/act-runner-ubuntu:22.04 platform= entrypoint=["tail" "-f" "/dev/null"] cmd=[] network="host"
[test/test-1]   🐳  docker run image=adolphlwq/act-runner-ubuntu:22.04 platform= entrypoint=["tail" "-f" "/dev/null"] cmd=[] network="host"
[test/test-3]   🐳  docker run image=adolphlwq/act-runner-ubuntu:22.04 platform= entrypoint=["tail" "-f" "/dev/null"] cmd=[] network="host"
[test/test-1]   ☁  git clone 'https://github.com/actions/setup-go' # ref=v5
[test/test-3]   ☁  git clone 'https://github.com/actions/setup-go' # ref=v5
[test/test-1] Non-terminating error while running 'git clone': some refs were not updated
[test/test-1]   ☁  git clone 'https://github.com/actions/upload-artifact' # ref=v4
[test/test-3] Non-terminating error while running 'git clone': some refs were not updated
[test/test-3]   ☁  git clone 'https://github.com/actions/upload-artifact' # ref=v4
^C[test/test-1] Cleaning up container for job test
[test/test-1] 🏁  Job succeeded
[test/test-3] Cleaning up container for job test
[test/test-3] 🏁  Job succeeded
Error: context canceled

对于像 windows 和 mac 这样的平台,镜像不好运行,可以通过 -P windows-latest=-self-hosted-P macos-latest=-self-hosted 直接在宿主机上运行。

act -P windows-latest=-self-hosted
act -P macos-latest=-self-hosted

.actrc 文件

act 命令的参数过多时,可以放到 .actrc 文件中,act 运行时,会自动寻找当前目录的 .actrc 文件,找到后会把其中的内容解析出来作为 act 的参数。

-P ubuntu-latest=adolphlwq/act-runner-ubuntu:22.04
-P linux=adolphlwq/act-runner-ubuntu:22.04
-P macos-12=-self-hosted
--artifact-server-path /tmp/artifacts

上面的 act 文件,等同于下面的命令:

act -P ubuntu-latest=adolphlwq/act-runner-ubuntu:22.04 -P linux=adolphlwq/act-runner-ubuntu:22.04 -P macos-12=-self-hosted --artifact-server-path /tmp/artifacts

参考资料

[1]

self-hosted runners: https://docs.github.com/en/actions/hosting-your-own-runners/managing-self-hosted-runners/about-self-hosted-runners

[2]

act: https://github.com/nektos/act

[3]

releases: https://github.com/nektos/act/releases

[4]

Standard GitHub-hosted runners for Public repositories: https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners/about-github-hosted-runners#standard-github-hosted-runners-for-public-repositories

[5]

act 直接在宿主机运行 Actions: https://nektosact.com/usage/runners.html

  • 22
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
GitHub Actions是GitHub的持续集成服务,它可以帮助开发者通过自动化的构建、发布和测试来验证代码,从而尽快发现集成错误。\[2\]GitHub Actions由多个操作组成,比如抓取代码、运行测试、登录远程服务器、发布到第三方服务等等,这些操作被称为actions。一个workflow是一次持续集成的运行过程,由一个或多个jobs构成,每个job又由多个steps构成,每个step可以执行一个或多个命令(action)。\[3\] GitHub Actions允许开发者把每个操作写成独立的脚本文件,存放到代码仓库,使得其他开发者可以引用该脚本,这个脚本就是一个Action。开发者可以从GitHub社区共享的官方市场查找需要的Action,也可以自己编写Action并开源供其他人使用Action可以通过指定commit、标签或分支来引用不同的版本。\[1\] 在使用GitHub Actions之前,需要了解持续集成/持续交付的概念、Git相关知识、Linux/Windows/macOS脚本相关知识以及Yaml基础语法。Yaml是一种用于配置文件的简洁易读的数据序列化格式。\[2\] GitHub Actions提供了Linux、Windows和macOS虚拟机来运行工作流程,也可以在自己的数据中心或云基础架构中托管自己的自托管运行器。通过在代码仓库中的.github/workflows目录下创建.yml文件,可以配置自动触发的工作流程。\[2\]\[3\] #### 引用[.reference_title] - *1* *2* [GitHubActions详解](https://blog.csdn.net/unreliable_narrator/article/details/124468384)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Github Actions](https://blog.csdn.net/SeriousLose/article/details/121476152)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值