Please enable Javascript to view the contents

本地执行没问题, GitHub Actions 却一直报错

 ·  ☕ 2 分钟

1. 一个令人困惑的问题

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
name: Go

on: [push, pull_request]

jobs:
  build:
    name: CI
    runs-on: ubuntu-latest
    steps:
      - name: Set up Go 1.13
        uses: actions/setup-go@v1
        with:
          go-version: 1.13
      - name: Check out code into the Go module directory
        uses: actions/checkout@v2
      - name: Check pr is properly formatted
        run: diff -u <(echo -n) <(gofmt -d ./pkg ./cmd ./tools ./test)
      - name: Test & Build
        run: make all

上面是项目中 workflow 的一部分, 主要用来检测代码风格、执行单元测试、编译代码。提交 Pull Requests 时, 就会触发执行。

但 GitHub Actions 一直报错, 提示如下:

1
Error: vet: pkg/kapis/cluster/v1alpha1/handler_test.go:405:87: too few arguments in call to informers.NewInformerFactories

问题的关键在于, 本地执行没有任何报错。刚开始怀疑是 GitHub Actions 不够稳定, 可能存在抖动。于是, 反复推送, 但是错误依旧。

2. 线上调试

直接祭出了杀手锏, 登录 Ngrok 拿到 Authtoken 配置到 Secrets 中。然后, 添加如下片段至失败的任务之前:

1
2
3
4
5
6
- uses: shaowenchen/debugger-action@v2
  name: debugger
  timeout-minutes: 30
  continue-on-error: true
  with:
    ngrok_token: ${{ secrets.NGROK_TOKEN }}

在 Actions 的执行日志中, 找到 SSH 登录链接。执行命令, 远程登录 Runner 进行调试:

1
ssh root@@0.tcp.ngrok.io -p 17656

输入密码 root, 即可在线调试。可是, 还是提示同样的错误, 只能另想办法。

3. 日志中发现端倪

根据错误, 我猜测是代码缓存、同步之类的问题。于是, 翻了下 Actions 的完整日志, 终于发现了端倪。

1
2
3
Checking out the ref
/usr/bin/git log -1 --format='%H'
'0f878e746fe9ce55a6c4b793c66f501c14c456f5'

日志中的一个 SHA 值引起了我的注意, 因为之前在 Jenkins 中碰到过一个 Git 记录找不到的问题。一个 SHA 就代表着一个版本记录。

然后, 我在本地 git checkout -f 0f878e746fe9ce55a6c4b793c66f501c14c456f5, 竟然提示找不到这条记录。将折叠的 Checking out the ref 日志展开:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
Checking out the ref
  /usr/bin/git checkout --progress --force refs/remotes/pull/3254/merge
  Note: switching to 'refs/remotes/pull/3254/merge'.

  You are in 'detached HEAD' state. You can look around, make experimental
  changes and commit them, and you can discard any commits you make in this
  state without impacting any branches by switching back to a branch.
  
  If you want to create a new branch to retain commits you create, you may
  do so (now or later) by using -c with the switch command. Example:
  
    git switch -c <new-branch-name>
  
  Or undo this operation with:
  
    git switch -
  
  Turn off this advice by setting config variable advice.detachedHead to false
  
  HEAD is now at 0f878e7 Merge 2ecc7398161e7a0211c59b52061c9e3800d28551 into 38eaa5cde0580ac4d29537065b687e090db557c6

Merge 2ecc7398161e7a0211c59b52061c9e3800d28551 into 38eaa5cde0580ac4d29537065b687e090db557c6, 原来 actions/checkout 默认将 Pull Requests 分支的代码合并到最新代码上, 产生了一条新记录。

问题的原因找到了, 研发在 fork 主干代码之后, 开发新的功能。但是在这个过程中, 主干分支已经合并了其他人的代码。当已经合并的代码与 Pull Requests 中的代码有冲突时, 就会导致这种现象。

解决问题的办法就是, 每次提交 Pull Requests 之前, 都重新拉取一次 upstream 的代码, 接着 rebase 解决冲突, 再提交。这里的冲突有两类, 一个是文件冲突, 另一种是功能冲突。

4. action/checkout 的参数解读

下面, 我们继续来看一看 action/checkout 的其他参数和功能。

  • fetch-depth 默认值为 1, 仅拉取当前分支。设置为 0 时, 拉取全部分支和 Tag 的记录。
1
2
3
- uses: actions/checkout@v2
  with:
    fetch-depth: 0
  • checkout 多个仓库的代码到指定目录
1
2
3
4
5
6
7
8
- name: Checkout
  uses: actions/checkout@v2

- name: Checkout tools repo
  uses: actions/checkout@v2
  with:
    repository: my-org/my-tools
    path: my-tools
  • checkout 到提交 Pull Requests 的记录上, 默认是 merge 之后的记录
1
2
3
- uses: actions/checkout@v2
  with:
    ref: ${{ github.event.pull_request.head.sha }}

微信公众号
作者
微信公众号