Jenkinsfile pipeline for go builds... need an example.

2,675 views
Skip to first unread message

Tom Lester

unread,
Feb 17, 2017, 2:15:35 PM2/17/17
to Jenkins Users
Hi all -

I'm not a go (golang) developer, but the team that I support write their services in go.   I need to build a pipeline for go builds.  However...  because of the strict pathing that go expects (/go/src/Y/Y/X), I'm having a hard time making a pipeline that works.  My problem is that I need to run the build inside of a docker container and the host user is jenkins (501:501), so the docker user is 501, which can't write to "/" to create the necessary paths.

I've tried to set the GOPATH, but when I do it to the work space directory, it turns around and says that directory doesn't exist.   It's baffling .  So... if you have a working pipeline script for building go, I'd love for you to share it.   Here's some examples of what I'm getting...

pipeline {
    agent { docker 'golang:1.7.3-alpine' }
    stages {
        stage('build') {
            steps {
                sh 'env.GOPATH=$PWD'
                sh 'mkdir -p $GOPATH/src/marketability-service'
                dir('$GOPATH/src/marketability-service') {
                    sh 'go version'
                    sh 'echo $GOPATH'
                    sh 'mkdir -p $GOPATH/src/marketability-service/'
                    sh 'ls -al $GOPATH/src/marketability-service/'
                    checkout scm
                    sh 'ls -al'
                }
            }

        }
    }
}

Results:

+ docker pull golang:1.7.3-alpine
1.7.3-alpine: Pulling from library/golang
Digest: sha256:25ad25d8bece9d403be107b07c86fb6b2b588a5b26133baa9d1215c07bdbfa39
Status: Image is up to date for golang:1.7.3-alpine
[Pipeline] }
[Pipeline] // stage
[Pipeline] sh
[ity-service_tlester_jenkins-ZAK24AVEOSII6FI5ZPP6GCRQQ7ISJCROTD5BZVI3M4MT7EMS4ERA] Running shell script
+ docker inspect -f . golang:1.7.3-alpine
.
[Pipeline] withDockerContainer
$ docker run -t -d -u 501:501 -w /home/jenkins/workspace/ity-service_tlester_jenkins-ZAK24AVEOSII6FI5ZPP6GCRQQ7ISJCROTD5BZVI3M4MT7EMS4ERA -v /home/jenkins/workspace/ity-service_tlester_jenkins-ZAK24AVEOSII6FI5ZPP6GCRQQ7ISJCROTD5BZVI3M4MT7EMS4ERA:/home/jenkins/workspace/ity-service_tlester_jenkins-ZAK24AVEOSII6FI5ZPP6GCRQQ7ISJCROTD5BZVI3M4MT7EMS4ERA:rw -v /home/jenkins/workspace/ity-service_tlester_jenkins-ZAK24AVEOSII6FI5ZPP6GCRQQ7ISJCROTD5BZVI3M4MT7EMS4ERA@tmp:/home/jenkins/workspace/ity-service_tlester_jenkins-ZAK24AVEOSII6FI5ZPP6GCRQQ7ISJCROTD5BZVI3M4MT7EMS4ERA@tmp:rw -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** --entrypoint cat golang:1.7.3-alpine
[Pipeline] {
[Pipeline] stage
[Pipeline] { (build)
[Pipeline] sh
[ity-service_tlester_jenkins-ZAK24AVEOSII6FI5ZPP6GCRQQ7ISJCROTD5BZVI3M4MT7EMS4ERA] Running shell script
+ env.GOPATH=/home/jenkins/workspace/ity-service_tlester_jenkins-ZAK24AVEOSII6FI5ZPP6GCRQQ7ISJCROTD5BZVI3M4MT7EMS4ERA
/home/jenkins/workspace/ity-service_tlester_jenkins-ZAK24AVEOSII6FI5ZPP6GCRQQ7ISJCROTD5BZVI3M4MT7EMS4ERA@tmp/durable-264541e0/script.sh: line 1: env.GOPATH=/home/jenkins/workspace/ity-service_tlester_jenkins-ZAK24AVEOSII6FI5ZPP6GCRQQ7ISJCROTD5BZVI3M4MT7EMS4ERA: not found
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
$ docker stop --time=1 141956393cfde63600f9fbb54475f50848279444b80abe647fef24c2bd9a5925
$ docker rm -f 141956393cfde63600f9fbb54475f50848279444b80abe647fef24c2bd9a5925
[Pipeline] // withDockerContainer
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline

GitHub has been notified of this commit’s build result

ERROR: script returned exit code 127
Finished: FAILURE


Tom Lester

unread,
Feb 17, 2017, 3:35:28 PM2/17/17
to Jenkins Users
It's adding this "@durable" stuff in there when I use "dir() {}".  Is there anyway to keep it from doing that?

Eric Pyle

unread,
Feb 17, 2017, 3:41:37 PM2/17/17
to jenkins...@googlegroups.com
No, there isn't. There have been many people posting about coming to grief by trying to work with the exact path to the workspace in Pipeline. If your difficulty is that you can't write to /go on the root level, can you set up an alternate location for GO, one that does not depend on the workspace?


On 2/17/2017 3:35 PM, Tom Lester wrote:
It's adding this "@durable" stuff in there when I use "dir() {}".  Is there anyway to keep it from doing that?
--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/6498212f-0062-4471-9283-8ad975b6c2cc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
Eric Pyle
Siemens PLM Software
Lebanon, NH
+1 603-277-3060 (T)
+1 603-359-8670 (M)
eric...@siemens.com
http://www.siemens.com/plm

Tom Lester

unread,
Feb 17, 2017, 4:03:09 PM2/17/17
to Jenkins Users
Yeah... I've played with that by setting the GOPATH to a different location that I can write to, however... every time I use "dir(<path>) {}' It breaks with the durable garbage.  I am trying to use "dir" to do a "checkout scm" into the GOPATH.  It fails every time with the durable problem.
--

Tom Lester

unread,
Feb 17, 2017, 4:08:41 PM2/17/17
to Jenkins Users
Also...  do we know if they are working to fix the durable problem?

Chris R

unread,
Aug 4, 2017, 9:08:41 AM8/4/17
to Jenkins Users
Might be late to this question, but for anyone else whose searches came here - I managed to get it to work like this:

 
        withEnv(['GOPATH=' + pwd()]){
            dir ('src/github.com/username') {
                // insert git checkout here
                sh 'go build -o $GOPATH/my-bin-file'
            }
        }


ctr...@gmail.com

unread,
Mar 12, 2019, 9:51:43 AM3/12/19
to Jenkins Users
Hi Chris,

I need your help here. I tried as you said but it did not worked for me
I want to clone my git repo into /opt/go/src/myproject directory where (/opt/go is my GOPATH)


I tried the below way and it did not worked for me i am using alpine image

withEnv(['GOPATH=' + pwd()]){
            dir ('/opt/go/src/myproject') {
                checkout scm
            }
        }
Reply all
Reply to author
Forward
0 new messages