Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-63302

Pipeline build step should support using environment variables

XMLWordPrintable

    • Icon: Improvement Improvement
    • Resolution: Not A Defect
    • Icon: Minor Minor
    • None
    • Pipeline build step 2.12
      Jenkins 2.235.2.

      We are migrating to declarative pipeline and want to invoke another job. This can be done with the build step. Here's how we do it now:

      pipeline {
          agent any
          stages {
              ...
              stage('Deploy') {
                  steps {
                      echo "Deploying ${env.JOB_NAME}..."
                      build(job: 'mdh-webapp-deploy-to-dev', parameters: [string(name: 'ARTIFACT_ID', value: '${JOB_NAME}')])
                  }
              }
          }
      }
      

      The other job is parameterized and takes one parameter named ARTIFACT_ID, in the form of a Choice Parameter.

      This is what we see in the log of the pipeline job:

      ...
      Deploying mdh-bestallning-webapp...
      [Pipeline] build (Building mdh-webapp-deploy-to-dev)
      Scheduling project: mdh-webapp-deploy-to-dev
      ...
      ERROR: Value for choice parameter 'ARTIFACT_ID' is '${JOB_NAME}', but valid choices are [<a list of possible values that includes mdh-bestallning-webapp>]
      

      So it seems that injecting environment variables is not possible. We've tried different syntaxes like $JOB_NAME, ${JOB_NAME} and ${env.JOB_NAME}, but none of them works.

      I have had a look at the code, but can't find the correct place to inject environment variables using something like
      https://javadoc.jenkins-ci.org/hudson/EnvVars.html#expand-java.lang.String-
      If you can point me in the right direction I will try to make a pull request.

            dennisl Dennis Lundberg
            dennisl Dennis Lundberg
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: