title: How can I use different private docker agents based on parameter in Jenkins declarative pipeline?
body: <p>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:</p>
  
  <pre><code>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}"
            }
        }
    }
  }
  </code></pre>
  
  <p>I get error:
  <code>Error response from daemon: Get https://null/v2/: dial tcp: lookup null on</code></p>
  
  <p>If I hard code the registryUrl I get a similar issue with registryCredentialsId:</p>
  
  <pre><code>agent {
      docker {
          image "${env.URL_VAR}/image:tag"
          registryUrl "https://urlTest.azure.io"
          registryCredentialsId "${env.CREDS_VAR}"
      }
  }
  </code></pre>
  
  <p><code>ERROR: Could not find credentials matching null</code></p>
  
  <p>It is successful if I hardcode both registryUrl and registryCredentialsId. </p>
  
  <pre><code>agent {
      docker {
          image "${env.URL_VAR}/image:tag"
          registryUrl "https://urlTest.azure.io"
          registryCredentialsId "credsTest"
      }
  }
  </code></pre>
  
  <p>It appears that the docker login stage of the <code>agent{docker{}}</code> cannot access/resolve environment variables.</p>
  
  <p>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.</p>