我正在通过 Jenkins Pipeline 构建一个 go 应用程序,首先我将所有 go 依赖项添加到供应商目录中并推送到 VCS,然后在 Jenkins 中构建时提取所有代码,这工作正常。
然后我想迁移到 go 模块,因为我在 Gitlab 中有一个私有 lib 依赖项,所以我修改了 Jenkins 服务器中的 netrc 文件,如本页所述:git_https,并且“go build”在本地计算机中完美运行并下载所有依赖项均符合预期,但 Jenkins 服务器中存在一些问题。 这是我的 Jenkinsfile:
pipeline {
agent any
stages {
stage('build') {
agent {
docker { image 'golang:1.12' }
}
steps {
sh "export XDG_CACHE_HOME=/tmp/.cache \
&& go build"
}
}
}
}
如代码所示,我使用“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
是我的私人存储库,Jenkins服务器中netrc
中的配置似乎不起作用,我该如何解决这个问题?感谢您的任何建议。
要启用提示,您可以使用以下命令进行构建:
GIT_TERMINAL_PROMPT=1 go build...
但是,这仅在由交互式会话触发时才有效,而不能在像 Jenkins 作业这样的自动化中工作
自动化的一种方法是通过 jenkins 将 MOD 单独克隆到一个单独的文件夹中(例如:
your-repo-folder
),然后向 go.mod 添加一个覆盖模式以将其指向一个文件夹:
在 go.mod 添加:
go 1.20
require (
github.com/user/your-repo v1.0.0
...
)
# add at the bottom:
replace github.com/user/your-repo => ./your-repo-folder
最好的方法是将您的项目 ./vendor 目录添加到您的 git 存储库中。 然后 jenkins build 将使用供应商而不是尝试从内网获取。 构建命令:
env GO111MODULE=off go build