Go GitHub Actions 克隆要从主代码使用的私有存储库

时间:2021-03-02 11:25:42

标签: go github-actions

我正在尝试使用 GitHub 操作自动构建和测试我的 go 代码推送到我的 GitHub 存储库的主分支。

这样做的基本配置工作得很好:

name: Build & Test

on:
  push:
    branches:
      - master

jobs:
  test:
    ## We want to define a strategy for our job
    strategy:
      ## this will contain a matrix of all of the combinations
      ## we wish to test again:
      matrix:
        go-version: [1.14.x] #[1.12.x, 1.13.x, 1.14.x]
        platform: [ubuntu-latest] #[ubuntu-latest, macos-latest, windows-latest]
    
    ## Defines the platform for each test run
    runs-on: ${{ matrix.platform }}
    
    ## the steps that will be run through for each version and platform
    ## combination
    steps:
    ## sets up go based on the version
    - name: Install Go
      uses: actions/setup-go@v2
      with:
        go-version: ${{ matrix.go-version }}
      env:
        GOPATH: /home/runner/work/project
        GO111MODULE: "on"

    ## runs a build
    - name: Build
      run: go build src/
      env:
        GOPATH: /home/runner/work/project
        GO111MODULE: "on"

    ## runs go test ./...
    - name: Test
      run: go test ./...
      env:
        GOPATH: /home/runner/work/project
        GO111MODULE: "on"

但是,存储库有另一个(私有)存储库作为直接依赖项,因此我需要在检出主存储库后克隆它。

- name: Check out dependency
  uses: actions/checkout@v2
  with:
      repository: mydependency
      token: ${{ secrets.GIT_ACCESS_TOKEN }}
      path: src/dependency

然后它不会找到依赖项,因为它不能从 GOROOT 访问。

src/main.go:20:2: package dependency is not in GOROOT (/opt/hostedtoolcache/go/1.14.15/x64/src/dependency)

这里的问题是无法在 GitHub 操作中访问该路径,因此我无法将存储库克隆到此特定路径。

我当然可以将 GOROOT 指定为 setup-gobuildtest 的 env 变量,这样就可以找到依赖关系,但是这样就找不到本机 go 包没有了。

env:
    GOPATH: /home/runner/work/project/go
    GOROOT: /home/runner/work/project
    GO111MODULE: "on" 
package bufio is not in GOROOT (/home/runner/work/project/src/bufio)

1 个答案:

答案 0 :(得分:0)

所以,我设法自己解决了这个问题。 checkout@v2 操作只允许相对路径,但是,我们可以手动克隆依赖项。

- name: Checkout dependencies
  run: |
      git clone https://${{ secrets.GIT_ACCESS_TOKEN }}@github.com/myorg/dependency.git ${GOROOT}/src/dependency

通过这种方式,它也可以在不同的 GOROOT 中产生不同的 Go 版本。

完整的流水线步骤:

steps:
## sets up go based on the version
- name: Install Go
  uses: actions/setup-go@v2
  with:
    go-version: ${{ matrix.go-version }}
  env:
    GO111MODULE: "on"
- name: Checkout dependencies
  run: |
    git clone https://${{ secrets.GIT_ACCESS_TOKEN }}@github.com/myorg/dependency.git 

## checks out our code locally so we can work with the files
- name: Checkout code
  uses: actions/checkout@v2

## runs a build
- name: Build
  run: go build src

## runs go test ./...
- name: Test
  run: go test ./...
相关问题