利用 Act 本地运行 GitHub Actions

鼠扑  论坛元老 | 2024-9-12 09:34:35 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 1042|帖子 1042|积分 3126

简介

  GitHub Actions 为仓库开发者提供了执行定制化 Job 的能力,开发者可以利用各种 Job 基于代码仓库运行测试、构建、发布等操作,实现 CI/CD 等工作流。
  
  这些 Job 默认运行在 GitHub 提供的 runner 中,但是由于仓库浩繁,设置了 actions 的代码仓库未必能得到足够的资源 (runner) 来快速、高效的运行代码仓库的 Actions。
  一个可行的方式是绑定自己的私有 runner 到代码仓库,这样每次执行 Job 时都会在自己的 runner 上,详情见 self-hosted runners[1]。
  另一个方案就是 act[2], 可以在电脑上,在代码仓库目录下,直接本地运行 Actions 中的 Job。方便开发者快递本地迭代和测试,确认没问题了再更新到主仓库,减少主仓库执行 Actions 的次数。
  act 安装

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

  • 下载二进制文件
  进入 act 的 releases[3] 页面找到最新版,笔者这里是 v0.2.63
  
  选择对应平台的文件,笔者这里是 Mac M 芯片,选择act_Darwin_arm64.tar.gz, 下载解压后,移动到 PATH 目录即可。
  

  • go install 安装
  起首下载代码仓库
  1. git clone https://github.com/nektos/act.git
复制代码
进入到 act 目录,运行 go install
  1. $ go install
  2. go: downloading github.com/spf13/cobra v1.8.1
  3. go: downloading golang.org/x/term v0.21.0
  4. go: downloading google.golang.org/protobuf v1.34.2
  5. go: downloading github.com/docker/cli v26.1.4+incompatible
  6. go: downloading golang.org/x/sys v0.21.0
复制代码
go 会主动构建二进制文件,并把构建好的二进制文件放到 gopath 中,这时查看 act 版本:
  1. $ act --version
  2. act version 0.2.63
复制代码
act 利用

  

  • 触发条件
  1. # 运行 push 触发的 action
  2. act push
  3. # 运行 pull_request 触发的 action
  4. act pull_request
复制代码

  • 运行指定 Job
  1. # -j, --job 后面跟 job id
  2. act -j 'test'
复制代码

  • 变量与机密内容
  1. # 通过 --var 指定单个 var
  2. act --var VARIABLE=somevalue
  3. # 通过 --var-file 指定文件中的多个变量
  4. act --var-file my.variables
复制代码
  1. # 通过 -s 指定单个保密值
  2. act -s MY_SECRET=somevalue
  3. # 通过 --secret-file 指定文件中的多个保密值
  4. 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 指定镜像:
  1. 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 labelNotesLinux416GB14GBubuntu-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-13NAmacOS3(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]。
  1. act -P ubuntu-latest=-self-hosted
  2. act -P windows-latest=-self-hosted
  3. act -P macos-latest=-self-hosted
复制代码
act runner 的局限

  act runner 不是万能的。起首表示 runner 的平台标签 (ubuntu-latest 等)要和宿主机的指令架构同等。好比在 M 芯片的 苹果机器上,是不能运行
  [code]# 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] 

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

鼠扑

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表