drb56625 2018-01-29 14:30
浏览 215
已采纳

声明性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
  • 写回答

1条回答 默认 最新

  • dsnhalq37505 2018-01-29 17:53
    关注

    You have to make $GOPATH/src/my_repo/my_db exist. The value of $GOPATH is not really important as far as Go is concerned. $GOPATH/src/my_repo/my_db may be a symlink. We create that symlink as part of the test script in our CI system.

    I'm not very familiar with Jenkins, but I'm going to assume that $WORKSPACE is where the code is checked out (e.g. $WORKSPACE/.git exists). In that case, see if you can execute the following script before running any go command:

    mkdir -p $GOPATH/src/my_repo
    ln -s $WORKSPACE $GOPATH/src/my_repo/my_db
    

    Again, the important thing is that $GOPATH/src/my_repo/my_db exists. How you make this happen is up to you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿