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

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/[email protected]
        with:
          go-version: 1.13
      - name: Check out code into the Go module directory
        uses: actions/[email protected]
      - name: Check pr is properly formatted
        run: diff -u <(echo -n) <(gofmt -d ./pkg ./cmd ./tools ./test)
      - name: Test & Build
        run: make all

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/[email protected]
  with:
    fetch-depth: 0
  • checkout 多个仓库的代码到指定目录
1
2
3
4
5
6
7
8
- name: Checkout
  uses: actions/[email protected]

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