如何基于Jenkins声明式管道中的参数使用不同的私有docker代理? [英] How can I use different private docker agents based on parameter in Jenkins declarative pipeline?

查看:80
本文介绍了如何基于Jenkins声明式管道中的参数使用不同的私有docker代理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据Jenkins管道中的参数从私有容器注册表中选择其他Docker代理.对于我的示例,假设我在凭据存储中保存了"credsProd"和"credsTest".我的尝试如下:

I am trying to choose a different docker agent from a private container registry based on an a parameter in Jenkins pipeline. For my example let's say I have 'credsProd' and 'credsTest' saved in the credentials store. My attempt is as follows:

pipeline {
    parameters {
        choice(
            name: 'registrySelection', 
            choices: ['TEST', 'PROD'],
            description: 'Is this a deployment to STAGING or PRODUCTION environment?'
        )
    }
    environment {
        URL_VAR = "${env.registrySelection == "PROD" ? "urlProd.azure.io" : "urlTest.azure.io"}"
        CREDS_VAR = "${env.registrySelection == "PROD" ? "credsProd" : "credsTest"}"
    }
    agent {
        docker {
            image "${env.URL_VAR}/image:tag"
            registryUrl "https://${env.URL_VAR}"
            registryCredentialsId "${env.CREDS_VAR}"
        }
    }
  stages{
      stage('test'){
          steps{
            echo "${env.URL_VAR}"
            echo "${env.CREDS_VAR}"
          }
      }
  }
}

我收到错误消息: Error response from daemon: Get https://null/v2/: dial tcp: lookup null on

如果我对registryUrl进行硬编码,我会遇到与registryCredentialsId类似的问题:

If I hard code the registryUrl I get a similar issue with registryCredentialsId:

agent {
    docker {
        image "${env.URL_VAR}/image:tag"
        registryUrl "https://urlTest.azure.io"
        registryCredentialsId "${env.CREDS_VAR}"
    }
}

ERROR: Could not find credentials matching null

如果我同时对registryUrl和registryCredentialsId进行硬编码,则表示成功.

It is successful if I hardcode both registryUrl and registryCredentialsId.

agent {
    docker {
        image "${env.URL_VAR}/image:tag"
        registryUrl "https://urlTest.azure.io"
        registryCredentialsId "credsTest"
    }
}

agent{docker{}}的docker登录阶段似乎无法访问/解析环境变量.

It appears that the docker login stage of the agent{docker{}} cannot access/resolve environment variables.

有没有一种解决方法,该方法不涉及代码重复?我使用多分支管道来管理更改,因此理想情况下,我不想有单独的Prod和groovy文件,也不想在同一文件中使用不同的集顺序步骤.

Is there a way around this that does not involve code duplication? I manage changes with multi branch pipeline so ideally do not want to have separate Prod and test groovy files or different sets sequential steps in the same file.

推荐答案

尝试在声明式之前运行脚本化管道:

Try running a scripted pipeline before declarative:

URL_VAR = null
CREDS_VAR = null

node('master') {
    stage('Choose') {
        URL_VAR = params.registrySelection == "PROD" ? "urlProd.azure.io" : "urlTest.azure.io"
        CREDS_VAR = params.registrySelection == "PROD" ? "credsProd" : "credsTest"
    }
}


pipeline {
    agent {
        docker {
            image "${URL_VAR}/image:tag"
            registryUrl "https://${URL_VAR}"
            registryCredentialsId "${CREDS_VAR}"
        }
    }
...

或者,您可以定义两个阶段(带有硬编码的url和cred),但只能运行其中一个,在两个阶段均使用when.

Alternatively, you can define two stages (with hard-coded url and creds) but run only one of them, using when in both.

这篇关于如何基于Jenkins声明式管道中的参数使用不同的私有docker代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆