在 Jenkinsfile 中使用带有身份验证的私有 docker 注册表 [英] Use private docker registry with Authentication in Jenkinsfile
问题描述
如何在此设置中教我的 Jenkisfile 通过基本身份验证登录?
我正在为我的 Jenkins 构建使用自定义 docker 映像.如文档 here 中所述,我这样定义了一个 docker 代理:
I'm using a custom docker image for my Jenkins build. As described in the documentation here I defined a docker agent like so:
pipeline {
agent {
docker {
image 'registry.az1:5043/maven-proto'
registryUrl 'https://registry.az1'
args '-v /var/jenkins_home/.m2:/root/.m2'
}
}
options {
timeout(time: 1, unit: 'HOURS')
buildDiscarder(logRotator(numToKeepStr:'10'))
}
stages {
stage ('Build') {
steps{
sh ...
}
}
stage ('Test') {
steps {
sh ...
}
}
stage ('Deploy') {
steps {
sh ...
}
}
}
post {
always {
echo 'Clean up workspace'
deleteDir()
}
}
}
如果我使用以下代理设置:
If I use the following agent setup:
pipeline {
agent {
docker.withRegistry('https://registry.az1', 'registry_login'){
image 'registry.az1:5043/maven-proto'
registryUrl 'https://registry.az1'
args '-v /var/jenkins_home/.m2:/root/.m2'
}
}
管道执行失败,出现以下异常:
The execution of the pipeline fails with the following exception:
WorkflowScript: 3: Too many arguments for map key "withRegistry" @ line 3, column 16.
docker.withRegistry('https://registry.az1', 'registry_login'){
^
WorkflowScript: 3: Invalid agent type "withRegistry" specified. Must be one of [docker, dockerfile, label, any, none] @ line 3, column 16.
docker.withRegistry('https://registry.az1', 'registry_login'){
^
问题是使用的注册表需要基本的身份验证登录.注册表使用 this 配置在 nginx 反向代理后面运行.
The problem is that the used registry requires a basic auth login. The registry runs behind a nginx reverse proxy using this configuration.
推荐答案
如 使用自定义注册表,您可以指定要使用的凭据和注册表 URL:
As specified in Using a custom registry, you can specify the credentials and registry url to use as such:
docker.withRegistry('https://registry.az1', 'credentials-id') {
...
}
您需要创建一个 Jenkins 凭据对象,该对象将包含存储库的凭据,并为其命名以替换上面的 credentials-id
.
You need to create a Jenkins credentials object which will contain the credentials for the repository and give it a name to replace credentials-id
above.
更新:
对于声明式管道,语法如下:
For declarative pipelines, the syntax is as such:
agent {
docker {
image 'registry.az1:5043/maven-proto'
registryUrl 'https://registry.az1'
registryCredentialsId 'credentials-id'
args '-v /var/jenkins_home/.m2:/root/.m2'
}
}
这篇关于在 Jenkinsfile 中使用带有身份验证的私有 docker 注册表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!