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

parameters are not available as environment variables in pod containers

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: Minor Minor
    • kubernetes-plugin
    • None
    • Jenkins 2.73.1 Kubernetes plugin 1.0

      Pipeline parameters are not available as environment variables in the pod containers that aren't the JNLP container.

      This Jenkinsfile produces output showing the issue:

      properties([
          parameters([
              string(name: 'PARAM_TEST', defaultValue: 'foo', description: ''),
          ]),
      ])
      
      podTemplate(
          cloud: 'kubernetes',
          containers: [
              containerTemplate(
                  alwaysPullImage: false,
                  envVars: [],
                  image: 'alpine',
                  name: 'alpine',
                  privileged: false,
                  ttyEnabled: true,
              ),
          ],
          serviceAccount: 'jenkins',
          idleMinutes: 0,
          instanceCap: 2,
          label: 'test-pipeline',
          name: 'test-pipeline',
      ) {
          node ('test-pipeline') {
              sh 'echo \$PARAM_TEST: $PARAM_TEST'
              container('alpine') {
                  sh 'echo \$PARAM_TEST: $PARAM_TEST'
              }
          }
      }
      
      

      Output:

      Started by user chance.zibolski
      [Pipeline] properties
      [Pipeline] podTemplate
      [Pipeline] {
      [Pipeline] node
      Running on test-pipeline-xchj7-gcv7h in /home/jenkins/workspace/test-pipeline
      [Pipeline] {
      [Pipeline] sh
      [test-pipeline] Running shell script
      + echo foo: foo
      foo: foo
      [Pipeline] container
      [Pipeline] {
      [Pipeline] sh
      [test-pipeline] Running shell script
      + echo :
      :
      [Pipeline] }
      [Pipeline] // container
      [Pipeline] }
      [Pipeline] // node
      [Pipeline] }
      [Pipeline] // podTemplate
      [Pipeline] End of Pipeline
      Finished: SUCCESS
      

          [JENKINS-47210] parameters are not available as environment variables in pod containers

          I managed to solve this one by manually passing defined variables again inside containerTemplate:

          properties([
              parameters([
                  string(name: 'PARAM_TEST', defaultValue: 'foo', description: ''),
              ]),
          ])
          
          podTemplate(
              cloud: 'kubernetes',
              containers: [
                  containerTemplate(
                      alwaysPullImage: false,
                      envVars: [containerEnvVar(key: 'PARAM_TEST', value: "$PARAM_TEST")],
                      image: 'alpine',
                      name: 'alpine',
                      privileged: false,
                      ttyEnabled: true,
                  ),
              ],
          

          Piotr Mossakowski added a comment - I managed to solve this one by manually passing defined variables again inside containerTemplate: properties([ parameters([ string(name: 'PARAM_TEST' , defaultValue: 'foo' , description: ''), ]), ]) podTemplate( cloud: 'kubernetes' , containers: [ containerTemplate( alwaysPullImage: false , envVars: [containerEnvVar(key: 'PARAM_TEST' , value: "$PARAM_TEST" )], image: 'alpine' , name: 'alpine' , privileged: false , ttyEnabled: true , ), ],

          Jesse Glick added a comment -

          Unclear if this is related to JENKINS-49370.

          Jesse Glick added a comment - Unclear if this is related to  JENKINS-49370 .

          David Schott added a comment -

          Closing the issue because the example is working now:

          Running on test-pipeline-r5njn-pcx39 in /home/jenkins/agent/workspace/JENKINS-47210
          [Pipeline] {
          [Pipeline] sh
          + echo foo: foo
          foo: foo
          [Pipeline] container
          [Pipeline] {
          [Pipeline] sh
          + echo foo: foo
          foo: foo
          [Pipeline] }
          [Pipeline] // container
          [Pipeline] }
          [Pipeline] // node
          [Pipeline] }
          [Pipeline] // podTemplate
          [Pipeline] End of Pipeline
          Finished: SUCCESS

          David Schott added a comment - Closing the issue because the example is working now: Running on test-pipeline-r5njn-pcx39 in /home/jenkins/agent/workspace/JENKINS-47210 [Pipeline] { [Pipeline] sh + echo foo: foo foo: foo [Pipeline] container [Pipeline] { [Pipeline] sh + echo foo: foo foo: foo [Pipeline] } [Pipeline] // container [Pipeline] } [Pipeline] // node [Pipeline] } [Pipeline] // podTemplate [Pipeline] End of Pipeline Finished: SUCCESS

            Unassigned Unassigned
            chancez Chance Zibolski
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: