• Icon: Bug Bug
    • Resolution: Fixed
    • Icon: Major Major
    • kubernetes-plugin
    • None

      WithEnv inside a container('x'){} is not working for us, when I did sh 'printenv' none of the variables were in the ENV.

          [JENKINS-40647] With Env not working after .10 k8 plugin update

          Lars Lawoko created issue -

          can you post a full example?

          Carlos Sanchez added a comment - can you post a full example?

          Mike Splain added a comment - - edited

          I'm seeing this as well, with both WithEnv and WithCredentials (while populating env vars)

          Simple example

          container('jnlp') {
            withEnv(['MYTOOL_HOME=/usr/local/mytool']) {
              sh 'env'
            }
          }
          

          In the output of env, 'MYTOOL_HOME' will not be set.

          Jenkins ver. 2.38, Jenkins k8s 0.10

          Mike Splain added a comment - - edited I'm seeing this as well, with both WithEnv and WithCredentials (while populating env vars) Simple example container( 'jnlp' ) { withEnv([ 'MYTOOL_HOME=/usr/local/mytool' ]) { sh 'env' } } In the output of env, 'MYTOOL_HOME' will not be set. Jenkins ver. 2.38, Jenkins k8s 0.10

          Mike Splain added a comment -

          Nevermind it just doesn't show up in env.... it's there using substitution like this

                  echo "${env.MYTOOL_HOME}"
          

          Mike Splain added a comment - Nevermind it just doesn't show up in env.... it's there using substitution like this echo "${env.MYTOOL_HOME}"

          Lars Lawoko added a comment - - edited
          runEnv = []
          runEnv.add('JAVA_OPTS=-Xmx6g')
          runEnv.add('npm_config_registry=http://nexus.default.svc.cluster.local:8081/repository/npm/')
          withEnv(runEnv) {
                try {
                     sh './gradlew --stacktrace --parallel jenkinsBuild'
                } catch (Exception e) {
                     junit allowEmptyResults: true, testResults: '**/build/test-results/**/*.xml'
                     step([$class: 'CheckStylePublisher', canComputeNew: false, defaultEncoding: '', healthy: '', pattern: '**/main.xml,**/test.xml', unHealthy: ''])
                     throw e
          }
          

          Adding a

          sh 'printenv'
          

          Shows the env is not populated

          P.s it is inside a container which contains JDK 7 & gcloud

           node('JavaPod') {
                          container('gcloud-jdk7') {
          

          Lars Lawoko added a comment - - edited runEnv = [] runEnv.add( 'JAVA_OPTS=-Xmx6g' ) runEnv.add( 'npm_config_registry=http: //nexus. default .svc.cluster.local:8081/repository/npm/' ) withEnv(runEnv) { try { sh './gradlew --stacktrace --parallel jenkinsBuild' } catch (Exception e) { junit allowEmptyResults: true , testResults: '**/build/test-results /**/ *.xml' step([$class: 'CheckStylePublisher' , canComputeNew: false , defaultEncoding: '', healthy: ' ', pattern: ' **/main.xml,**/test.xml ', unHealthy: ' ']) throw e } Adding a sh 'printenv' Shows the env is not populated P.s it is inside a container which contains JDK 7 & gcloud node( 'JavaPod' ) { container( 'gcloud-jdk7' ) {

          Lars Lawoko added a comment -

          Following up, can confirm it is a problem with the container step, The first printenv is correctly populated, but the second is not

          node('JavaPod') {
              withEnv(runEnv) {
                      sh 'printenv'
              }
              container('gcloud-jdk7') {
                      withEnv(runEnv) {
                              sh 'printenv'
                       } 
          }
          

          Lars Lawoko added a comment - Following up, can confirm it is a problem with the container step, The first printenv is correctly populated, but the second is not node( 'JavaPod' ) { withEnv(runEnv) { sh 'printenv' } container( 'gcloud-jdk7' ) { withEnv(runEnv) { sh 'printenv' } }

          Lars Lawoko added a comment -

          Can we confirm that this is a bug with the plugin, and not our setup? It requires ugly workarounds in its present state.

          Lars Lawoko added a comment - Can we confirm that this is a bug with the plugin, and not our setup? It requires ugly workarounds in its present state.

          It was never meant to work in the first place.

          The container block is only meant to be used with the `sh` function or shell based function.

          I would add then env variables in the pod definition or if that is not possible I would set them in the `sh` command.

          Ioannis Canellos added a comment - It was never meant to work in the first place. The container block is only meant to be used with the `sh` function or shell based function. I would add then env variables in the pod definition or if that is not possible I would set them in the `sh` command.
          Jon Whitcraft made changes -
          Link New: This issue is duplicated by JENKINS-43200 [ JENKINS-43200 ]

          Jon Whitcraft added a comment -

          iocanel if that is indeed the case, then the README should be updated to state that.

          Jon Whitcraft added a comment - iocanel if that is indeed the case, then the README should be updated to state that.

            csanchez Carlos Sanchez
            larslawoko Lars Lawoko
            Votes:
            7 Vote for this issue
            Watchers:
            19 Start watching this issue

              Created:
              Updated:
              Resolved: