我正在尝试使用GitHub操作在推送到GitHub存储库的主分支时自动构建和测试我的go代码。
这样做的基本配置非常好用:
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-go、build和test的环境变量,这样就可以找到依赖项,但这样它就不会再找到原生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)发布于 2021-03-05 00:02:21
所以,我设法自己解决了这个问题。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 ./...https://stackoverflow.com/questions/66438446
复制相似问题