首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >使用 Act 本地运行 GitHub Actions

使用 Act 本地运行 GitHub Actions

作者头像
陆道峰
发布于 2024-07-02 08:10:25
发布于 2024-07-02 08:10:25
1.5K00
代码可运行
举报
运行总次数:0
代码可运行

简介

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 安装。

  1. 下载二进制文件

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

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

  1. go install 安装

首先下载代码仓库

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
git clone https://github.com/nektos/act.git

进入到 act 目录,运行 go install

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
$ 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 版本:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
$ act --version
act version 0.2.63

act 使用

  1. 触发条件
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# 运行 push 触发的 action
act push

# 运行 pull_request 触发的 action
act pull_request
  1. 运行指定 Job
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# -j, --job 后面跟 job id
act -j 'test'
  1. 变量与机密内容
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# 通过 --var 指定单个 var
act --var VARIABLE=somevalue
# 通过 --var-file 指定文件中的多个变量
act --var-file my.variables
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# 通过 -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、LinuxWindows。需要注意,容器的指令集架构要和宿主机架构一致,如果宿主机是 Windows 系统 Intel amd64 架构,那么镜像也应该是 amd64 架构。

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

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
act -P ubuntu-latest=adolphlwq/act-runner-ubuntu:22.04

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

Virtual Machine

Processor (CPU)

Memory (RAM)

Storage (SSD)

Workflow label

Notes

Linux

4

16GB

14GB

ubuntu-latest, ubuntu-24.04 [Beta], ubuntu-22.04, ubuntu-20.04

The ubuntu-latest label currently uses the Ubuntu 22.04 runner image.

Windows

4

16GB

14GB

windows-latest, windows-2022, windows-2019

The windows-latest label currently uses the Windows 2022 runner image.

macOS

3

14GB

14GB

macos-12 or macos-11

The macos-11 label has been deprecated and will no longer be available after 28 June 2024.

macOS

4

14GB

14GB

macos-13

NA

macOS

3(M1)

7GB

14GB

macos-latest or macos-14

The macos-latest label currently uses the macOS 14 runner image.

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

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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 芯片的 苹果机器上,是不能运行

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# 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 笔记本上是能正确运行的:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# 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 直接在宿主机上运行。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
act -P windows-latest=-self-hosted
act -P macos-latest=-self-hosted

.actrc 文件

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

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
-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 文件,等同于下面的命令:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2024-06-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 机器学习与系统 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
如何给 GitHub Actions 添加自己的 Runner 主机
在前面的文档中,我对 GitLab 提供的 CI 功能进行了实践,点击查看 。使用 GitLab 的好处是可以私有化部署、无限的私有仓库数量、CI 配置简单、能接入自建的 Runner 。但随着 GitHub 越来越开放,GitLab 的这些优势在逐步丧失。
陈少文
2020/12/04
9.1K0
如何给 GitHub Actions 添加自己的 Runner 主机
GitHub Actions 指南
GitHub Actions 使你可以直接在你的 GitHub 库中创建自定义的工作流,工作流指的就是自动化的流程,比如构建、测试、打包、发布、部署等等,也就是说你可以直接进行 CI(持续集成)和 CD (持续部署)。
凌虚
2020/07/20
1.1K0
【翻译】.NET 💜 GitHub Actions: .NET 的 GitHub Actions 简介
原文 https://devblogs.microsoft.com/dotnet/dotnet-loves-github-actions/
晓晨
2022/05/07
1K0
【翻译】.NET 💜 GitHub Actions: .NET 的 GitHub Actions 简介
玩转Gitea之Gitea Actions安装使用
这里同样是使用Docker Compose进行安装,安装之前先在部署好的Gitea上去获取Token用于注册Runner。
熙世
2024/05/26
2.4K0
零基础入门 GitHub/Gitea Actions 流程自动化
几年前开始,为了将自己的杂七杂八的代码托管起来,自己建了代码私服,当时综合调研了下选择了比较轻量的Gitea,一是防止github、gitee、coding等产品托管的代码审查,二是为了应对墙,不然推送代码真吃力,Gitea1.19 版本之前不支持内置的CI/CD解决方案,所以是我自己使用的Gitea+Drone实现流程自动化。
Lcry
2024/07/17
9580
零基础入门 GitHub/Gitea Actions 流程自动化
5 分钟玩转史上最强大的自动发布工具 GitHub Actions
GitHub Actions 是 GitHub 的持续集成服务,于 2018 年 10 月推出。
iMike
2019/09/16
1.6K0
如何利用github action实现自动构建、打包部署
持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第1天,点击查看活动详情
鳄鱼儿
2024/05/22
1.8K0
github actions 入门指南及实践
自从 github 提供了 github actions 后,个人或者依赖于 github 的公司可以考虑把持续集成方案迁到 github actions。
山月
2020/12/16
1.1K0
github actions 入门指南及实践
10 个你该了解的 GitHub Actions 进阶技巧
在执行 workflow 时, 允许在 GitHub Actions 页面输入参数,控制执行逻辑。我们可以将人工处理的逻辑,在 GitHub Actions 参数化执行,适用于持续部署场景。
陈少文
2021/01/31
3K0
10 个你该了解的 GitHub Actions 进阶技巧
用 GitHub Action 构建一套 CI/CD 系统
Nebula Graph 最早的自动化测试是使用搭建在 Azure 上的 Jenkins,配合着 GitHub 的 Webhook 实现的,在用户提交 Pull Request 时,加个 ready-for-testing 的 label 再评论一句 Jenkins go 就可以自动的运行相应的 UT 测试,效果如下:
NebulaGraph
2020/05/08
1.3K0
用 GitHub Action 构建一套 CI/CD 系统
GitHub Actions,卧槽!牛批!
前段时间我更新了我的分布式爬虫管理框架—— Gerapy(话都说到这儿了打个广告,跟繁琐的命令行说拜拜!Gerapy分布式爬虫管理框架来袭!,哇,哇,就是,哇!)
崔庆才
2019/12/11
1.6K0
2023-04: 为什么你该试试 Sccache?
Sccache[1] 是由 mozilla 团队发起的类 ccache[2] 项目,支持 C/CPP, Rust, nvcc 等语言,并将缓存存储在本地或者云存储后端。在 v0.3.3 版本中,Sccache 加入了原生的 Github Action Cache Service 支持;在后续的 v0.4.0-pre.6[3] 版本中,社区对该功能进行了持续的改进,目前已经初步具备了在生产 CI 中应用的能力。
MikeLoveRust
2023/02/15
1.7K0
2023-04: 为什么你该试试 Sccache?
完整解析使用 Github Action 构建和发布 Flutter 应用
Github Actions 是 Github 提供的免费自动化构建实现,特别适用于持续集成和持续交付的场景,它具备自动化完成许多不同任务的能力,例如构建、测试和部署等等。
GSYTech
2022/04/02
1.5K0
完整解析使用 Github Action 构建和发布 Flutter 应用
GitHub免费支持CI/CD了,开发测试部署高度自动化,支持各种语言,网友:第三方凉凉
CI\CD,全称:持续集成 (Continuous Integration) ,持续部署 (Continuous Deployment) ,是开发流程的自动化利器,如今可以在公有项目上免费使用了。
量子位
2019/08/15
8290
你真的会用Github吗?Github Actions魔法之Electron自动打包
相信很多web前端开发的小伙伴和我一样,在想到要开发桌面端应用的时候会第一时间想到用Electron来开发。它可以让我们使用熟悉的HTML+JS+CSS来开发桌面应用。只需要一套代码,你的应用就可以轻松的运行在Windows,macOS,Linux三大操作系统上。
大帅老猿
2022/03/03
2.3K0
你真的会用Github吗?Github Actions魔法之Electron自动打包
利用github actions进行自动化开发
CI/CD网上的解释为持续集成、持续交付和持续部署。光这么说,可能确实有点迷茫,那我拿开发写代码来举个例子:
Jumbo
2020/11/03
1.2K0
【前端部署第十篇】CICD基础概念了解,并实现基于 docker 的自动部署
大家好,我是山月,这是我最近新开的专栏:「前端部署系列」。包括 Docker、CICD 等内容,大纲图示如下:
山月
2022/05/23
2.4K0
【前端部署第十篇】CICD基础概念了解,并实现基于 docker 的自动部署
【玩转腾讯云】Github Actions+CVM实践(CICD如此简单)
简言之:它是github推出的持续集成部署的工具,目前优秀的cicd工具包括:travis ci,jenkins
醉酒鞭名马
2020/04/15
2.6K3
【玩转腾讯云】Github Actions+CVM实践(CICD如此简单)
Github Action 补充介绍
补充罗列下其他常用的地方,不得不说设计太精妙了。Github被微软收购后,竟然变更强了。Intel推出的12代酷睿,性能远超mac的m1了,这次没挤牙膏。Wintel的时代没有过去,老当益壮,还可再战。
后端云
2022/04/18
2.2K0
Github Action 补充介绍
踩坑: "Waiting for a runner to pick up this job"
GitHub Actions 是一个 CI/CD(持续集成和持续部署)平台,可以让您自动化工作流程并与 GitHub 存储库中的代码集成。使用 GitHub Actions,您可以配置自动化任务来处理代码更改,例如自动运行测试、构建、部署和发布工件等。
测试开发囤货
2023/05/25
1.1K0
踩坑: "Waiting for a runner to pick up this job"
推荐阅读
相关推荐
如何给 GitHub Actions 添加自己的 Runner 主机
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验