如何解决Azure 管道构建正确运行模块
由于用于构建 go 代码的默认 azure-pipelines.yml 模板不支持 go 模块,因此支持它的效果并不明显。
这是不适用于 go.modules 的默认模板:
# Go
# Build your Go project.
# Add steps that test,save build artifacts,deploy,and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/go
trigger:
- master
pool:
vmImage: ubuntu-latest
variables:
GOBIN: '$(GOPATH)/bin' # Go binaries path
GOROOT: '/usr/local/go1.11' # Go installation path
GOPATH: '$(system.defaultWorkingDirectory)/gopath' # Go workspace path
modulePath: '$(GOPATH)/src/github.com/$(build.repository.name)' # Path to the module's code
steps:
- script: |
mkdir -p '$(GOBIN)'
mkdir -p '$(GOPATH)/pkg'
mkdir -p '$(modulePath)'
shopt -s extglob
shopt -s dotglob
mv !(gopath) '$(modulePath)'
echo '##vso[task.prependpath]$(GOBIN)'
echo '##vso[task.prependpath]$(GOROOT)/bin'
displayName: 'Set up the Go workspace'
- script: |
go version
go get -v -t -d ./...
if [ -f Gopkg.toml ]; then
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
dep ensure
fi
go build -v .
workingDirectory: '$(modulePath)'
displayName: 'Get dependencies,then build'
解决方法
我也想在这里分享一个正确构建 go modules 包的模板的答案。也许这只是为了您的灵感,需要考虑什么。我花了一些时间才到达那里。
主要的痛点是默认模板将 GOPATH 设置为管道工作目录,如果您通过 go mod download
将模块下载到其中,这是错误的。这将导致在下一次管道运行中无法访问文件,从而使管道在存储库签出期间失败。
以下方法只是将 GOPATH 设置为 Agent.HomeDirectory,这也使下载的模块可用于后续管道运行。
也许对某人有帮助
# Go
# Build your Go project.
# Add steps that test,save build artifacts,deploy,and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/go
trigger:
- main
- feature/*
pool: ubuntu-latest
variables:
GOPATH: '$(Agent.HomeDirectory)/go' # Go workspace path
GOBIN: '$(GOPATH)/bin' # Go binaries path
GOROOT: '/opt/hostedtoolcache/go/1.15.8/x64' # Go installation path
stages:
- stage: Build
displayName: Build image
jobs:
- job: BuildAndTest
displayName: Build And Test
pool: ubuntu-latest
steps:
- checkout: self
- script: |
export PATH="$(GOROOT)/bin:$(PATH)"
printenv
ls -la
go env
go version
go mod download
go build ./...
go test ./...
workingDirectory: '$(Build.SourcesDirectory)'
displayName: 'Get dependencies,then build and test'
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。