简体   繁体   中英

How to use go modules in jenkins with private repository?

I'm building a go app through Jenkins Pipeline, at first I add all my go depedencies in vendor dir and push to VCS, and then pull all the code when building in Jenkins, this works fine.

Then I want to migrate to go modules, because I have a private lib dependency in Gitlab, so I modify the netrc file in Jenkins server as this page says: git_https , and 'go build' works perfectly in local machine and download all the dependencies as expected, but in Jenkins server has some problem. Here is my Jenkinsfile:

pipeline {
    agent any

    stages {

        stage('build') {
            agent {
                docker { image 'golang:1.12' }
            }

            steps {

                sh "export XDG_CACHE_HOME=/tmp/.cache \
                && go build"
            }
        }

    }
}

As the code shows, I use 'go build' to trigger go module build process, , but still I got this error when building:

go get gitlab.com/gbuda/dblib: git ls-remote -q origin in /go/pkg/mod/cache/vcs/a8fb1674af641945219afa3eee63923c22afe0df48bc030a6bf714abb7116332: exit status 128:
    fatal: could not read Username for 'https://gitlab.com': terminal prompts disabled
If this is a private repository, see https://golang.org/doc/faq#git_https for additional information.

gitlab.com/gbuda/dblib is my private repository, it seems the config in netrc in Jenkins server didn't work, how can I fix this problem? Thanks for any advice.

Best approch is make your project ./vendor directory to you git repositry. Then jenkins build will use vendor and not try to get from intnernet. build command:

env GO111MODULE=off go build

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM