简体   繁体   English

如何在带有私有存储库的 jenkins 中使用 go 模块?

[英]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.我正在通过 Jenkins Pipeline 构建一个 go 应用程序,首先我在供应商目录中添加我所有的 go 依赖项并推送到 VCS,然后在 Jenkins 中构建时提取所有代码,这很好用。

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.然后我想迁移到go模块,因为我在Gitlab中有一个私有的lib依赖项,所以我修改了Jenkins服务器中的netrc文件,正如这个页面所说: git_https ,并且'go build'在本地机器上完美运行并下载所有依赖项正如预期的那样,但在 Jenkins 服务器中存在一些问题。 Here is my Jenkinsfile:这是我的 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 build”来触发 go 模块构建过程,但在构建时仍然出现此错误:

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? gitlab.com/gbuda/dblib是我的私人存储库,看来 Jenkins 服务器中netrc中的配置不起作用,我该如何解决这个问题? Thanks for any advice.感谢您的任何建议。

Best approch is make your project ./vendor directory to you git repositry.最好的方法是让你的项目 ./vendor 目录成为你的 git 存储库。 Then jenkins build will use vendor and not try to get from intnernet.然后 jenkins build 将使用供应商而不是尝试从互联网获取。 build command:构建命令:

env GO111MODULE=off go build

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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