跳至主要內容

Github Action 调试的痛

ruleeeer原创其它Github Action大约 3 分钟约 888 字

前几天正好在调试 vite-plugin-federationopen in new window 的一个 Github Action 问题,调试起来非常痛苦,大概流程就是不停的修改代码,然后 PUSHgithub,然后继续等待 Github Action 触发,不行的话继续修改 Github CI 的代码,后来我实在无法忍受这样的折磨,找到了一款可以在本地模拟 Github Action 的工具 ACTopen in new window

用途

ACTopen in new window 是一款使用 Docker 在本地模拟 Github Action 的工具,这样可以在本地调试 Github Action,省去一边又一遍的 PUSH 和枯燥的等待 CI 触发。

安装

官网给出了很多安装方式,涵盖 Linux/MacOS/Winodws,可以自行前往 官网open in new window 安装,这里我使用的是 Linux,所以我使用以下的方式

curl https://raw.githubusercontent.com/nektos/act/master/install.sh | sudo bash

安装成功后会出现如下字样就说明安装成功了

nektos/act info checking GitHub for latest tag
nektos/act info found version: 0.2.31 for v0.2.31/Linux/x86_64
nektos/act info installed ./bin/act

更多的安装方式可以到 官网 #installopen in new window 查看

使用

理论上安装完成之后第一次运行会让你选择基础镜像,也就是选择什么环境来运行 Github Actions

./act
? Please choose the default image you want to use with act:

  - Large size image: +20GB Docker image, includes almost all tools used on GitHub Actions (IMPORTANT: currently only ubuntu-18.04 platform is available)
  - Medium size image: ~500MB, includes only necessary tools to bootstrap actions and aims to be compatible with all actions
  - Micro size image: <200MB, contains only NodeJS required to bootstrap actions, doesn't work with all actions

Default image and other options can be changed manually in ~/.actrc (please refer to https://github.com/nektos/act#configuration for additional information about file structure)  [Use arrows to move, type to filter, ? for more help]
  Large
> Medium
  Micro

  • Large: 较大的镜像,包含完整
  • Medium: 中等大小的镜像(推荐)
  • Micro: 较小的镜像

由于 ACT 使用的是 docker 来模拟 Github Action,所以这里对应的大小就是 docker 镜像的大小,除非你基本不会进入 docker 容器内调试,否则不建议使用 Micro 镜像,该镜像裁减了很多包导致进入容器内部后很多命令都不支持,需要单独安装。

选择完成后理论上就可以直接选择项目来执行了,注意进入项目的根目录,也就是隐藏文件夹 .github 的父目录执行。

  1. 首先我们使用 act -l 可以查看项目有哪些 Github Action,可以看到只有一个配置,需要关注 Job nameEvent 两列,可以使用这两列来决定触发哪个 Action
$ act -l
Stage  Job ID  Job name  Workflow name  Workflow file  Events
0      build   build     Publish        publish.yml    push  
  1. 然后我们可以单独触发这个 Action,可以使用 act EventName(也就是上面显示的 Events 的)或者 act -j JobName 来执行该 Action

    1. 使用 act EventName 形式
    act push
    
    1. 使用 act -j JobName 形式
    act -j 
    

执行后可以看到已经成功在本地运行 Github Action,由于执行时间较长,不再演示完整的流程

test

Action 中包含 Secret?

如果你的 Github Action 中包含了 Secret,需要在执行 act 命令时指定该值

  1. act -s MY_SECRET=VALUE

    注意,不要再非安全环境使用该方式,因为可以使用命令记录工具例如 history 获取到该值。

  2. act -s MY_SECRET

    检查是否存在 MY_SECRET 的环境变量,如果有,直接使用,没有的话将会提示用户手动输入。

  3. act --secret-file mySecrets

    从文件中获取 Secret的值

上次编辑于:
贡献者: ruleeeer