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

Pipeline build step should support using environment variables

    XMLWordPrintable

Details

    • Improvement
    • Status: Closed (View Workflow)
    • Minor
    • Resolution: Not A Defect
    • None
    • Pipeline build step 2.12
      Jenkins 2.235.2.

    Description

      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.

      Attachments

        Activity

          After some more investigation I have realized that I fell into the trap och single quotes vs double quotes. Since this is Groovy under the hood, single quotes and double quotes are not the same.
          http://docs.groovy-lang.org/latest/html/documentation/#_single_quoted_string
          http://docs.groovy-lang.org/latest/html/documentation/#_double_quoted_string

          I needed to change from single quotes to double quotes for variable interpoaltion to occur.

                          build(job: 'mdh-webapp-deploy-to-dev', parameters: [string(name: 'ARTIFACT_ID', value: "${env.JOB_NAME}")])
          

          Sorry for the noise.
           

          dennisl Dennis Lundberg added a comment - After some more investigation I have realized that I fell into the trap och single quotes vs double quotes. Since this is Groovy under the hood, single quotes and double quotes are not the same. http://docs.groovy-lang.org/latest/html/documentation/#_single_quoted_string http://docs.groovy-lang.org/latest/html/documentation/#_double_quoted_string I needed to change from single quotes to double quotes for variable interpoaltion to occur. build(job: 'mdh-webapp-deploy-to-dev' , parameters: [string(name: 'ARTIFACT_ID' , value: "${env.JOB_NAME}" )]) Sorry for the noise.  

          People

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

            Dates

              Created:
              Updated:
              Resolved: