在此设置中,如何教我的 Jenkisfile 通过基本身份验证登录?
我正在为我的 Jenkins 构建使用自定义 docker 映像。 正如文档中所述here我定义了一个docker代理,如下所示:
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()
}
}
}
如果我使用以下代理设置:
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'
}
}
管道执行失败,出现以下异常:
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 反向代理后面运行。
如使用自定义注册表中所述,您可以指定要使用的凭据和注册表 URL:
脚本化管道语法:
docker.withRegistry('https://registry.az1', 'credentials-id') {
...
}
声明式管道语法:
agent {
docker {
image 'registry.az1:5043/maven-proto'
registryUrl 'https://registry.az1'
registryCredentialsId 'credentials-id'
args '-v /var/jenkins_home/.m2:/root/.m2'
}
}
您需要创建一个 Jenkins 凭证对象,其中包含存储库的凭证,并为其指定一个名称以替换上面的
credentials-id
。根据文档,Jenkins 中的凭据对象需要具有“类型”“用户名和密码”。