我正在为我们的 Go mono repo 在工作中设置一个 Jenkins 多分支管道。如何设置GOPATH
go 工具可以找到 Jenkins 获取 repo 的位置?
我尝试使用environment
语法设置它,如下所示:
pipeline {
agent {
docker {
image 'golang:1.9.2'
}
}
triggers {
gitlab(triggerOnPush: true, triggerOnMergeRequest: true, branchFilterType: 'All')
}
post {
failure {
updateGitlabCommitStatus name: 'jenkins-build', state: 'failed'
}
success {
updateGitlabCommitStatus name: 'jenkins-build', state: 'success'
}
}
environment {
GOPATH = "${pwd}"
}
stages {
stage('Build') {
steps {
updateGitlabCommitStatus name: 'jenkins-build', state: 'pending'
echo 'Linting...'
sh 'go version'
sh 'go get -u -v github.com/golang/lint/golint'
sh 'golint -set_exit_status ./...'
echo 'Building...'
sh '''
cd my_app
go build
'''
}
}
}
}
该golint
命令工作正常,因为它位于正确的目录中,但 go build 命令找不到根目录中的任何其他依赖项。例如,go build
在my_repo/my_app
其中运行不会找到任何依赖项my_repo
,比如说my_repo/my_dep
.
我还尝试GOPATH
在构建阶段进行设置:
stage('Build') {
steps {
updateGitlabCommitStatus name: 'jenkins-build', state: 'pending'
echo 'Building...'
sh '''
GOPATH=$(pwd)
cd fpweb
go build
'''
}
}
但同样的事情也会发生。
my_app.go:19:2: cannot find package "my_repo/my_db" in any of:
/usr/local/go/src/my_repo/my_db (from $GOROOT)
/data/jenkins_slave/workspace/my_repo-pipeline_master-JHGTBESY3LSHGFEUYYM2777JIAFVR4R7E4Y2YPZA2MI4XW6BBTQQ/src/my_repo/my_db (from $GOPATH)
编辑:我应该设置什么GOPATH
?
更新:当我设置GOPATH
为WORKSPACE
:
environment {
GOPATH = "$WORKSPACE"
}
我收到一个错误:
[my_repo-pipeline_master-JHGTBESY3LSHGFEUYYM2777JIAFVR4R7E4Y2YPZA2MI4XW6BBTQQ] Running shell script
+ pwd
+ // GOPATH=/data/jenkins_slave/workspace/my_repo-pipeline_master-JHGTBESY3LSHGFEUYYM2777JIAFVR4R7E4Y2YPZA2MI4XW6BBTQQ
/data/jenkins_slave/workspace/my_repo-pipeline_master-JHGTBESY3LSHGFEUYYM2777JIAFVR4R7E4Y2YPZA2MI4XW6BBTQQ@tmp/durable-18853a9e/script.sh: 3: /data/jenkins_slave/workspace/my_repo-pipeline_master-JHGTBESY3LSHGFEUYYM2777JIAFVR4R7E4Y2YPZA2MI4XW6BBTQQ@tmp/durable-18853a9e/script.sh: //: Permission denied