声明性Jenkinsfile中的GOPATH

I'm setting up a Jenkins multibranch pipeline for our Go mono repo at work. How do I set the GOPATH so the go tool can find where Jenkins has fetched the repo?

I've tried setting it with the environment syntax, like so:

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
                '''
            }
        }
    }
}

The golint command works fine as it's within the right directory, but the go build command can't find any of the other dependencies that are within the root directory. For instance running go build in my_repo/my_app it won't find any of the dependencies in my_repo, say my_repo/my_dep.

I also tried setting GOPATH within the build stage:

stage('Build') {
            steps {
                updateGitlabCommitStatus name: 'jenkins-build', state: 'pending'
                echo 'Building...'
                sh '''
                GOPATH=$(pwd)
                cd fpweb
                go build
                '''
            }
        }

But the same happens.

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)

EDIT: What should I set my GOPATH to?

UPDATE: when I set the GOPATH to the WORKSPACE:

environment {
        GOPATH = "$WORKSPACE"
    }

I get an error:

[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
查看全部
drb56625
drb56625
2018/01/29 14:30
  • jenkins
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

1个回复