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

      The environment declarative no longer works in pipeline. 

       

      pipeline {
        agent {
          kubernetes {
              label 'maven-pod'
              containerTemplate {
              name 'maven-container'
              image 'maven'
              workingDir  '/home/jenkins'
              ttyEnabled true
              command 'cat'
              }
          }
        }
        environment {
          test = 'Hello World!'
        }
        stages {
          stage('print') {
            steps {
              //will not print
              sh 'echo ${test}'
               container('maven-container'){
                   //will also not print
                    sh 'echo ${test}'
                 }
              }
            }
          }
        }    
      
      
      

      It appears to be due to environment variables for the container being set prior to container execution.

          [JENKINS-46278] Declarative Pipeline Environment ignored

          Matthew Ludlum added a comment - - edited

          I've identified a code work around for part of this in the attached patch.

          EDIT: patch that can be applied on current master (64a6717)

          0001-Added-envvars-to-fix-missing-environment-variables-d.patch

           

          This allows the container step to work properly but not the default container context to work(the first sh command).

           

          I'd be happy to work and fix this in another way if preferred - just point me in the direction where environment variables are currently merged into the container execution scope.

          Matthew Ludlum added a comment - - edited I've identified a code work around for part of this in the attached patch. EDIT: patch that can be applied on current master (64a6717) 0001-Added-envvars-to-fix-missing-environment-variables-d.patch   This allows the container step to work properly but not the default container context to work(the first sh command).   I'd be happy to work and fix this in another way if preferred - just point me in the direction where environment variables are currently merged into the container execution scope.

          Thanks! Can you create a Pull Request in github for review?
          Would be nice if you could keep the existing formatting, Eclipse/Java defaults

          Carlos Sanchez added a comment - Thanks! Can you create a Pull Request in github for review? Would be nice if you could keep the existing formatting, Eclipse/Java defaults

          Done & done  - lemme know if the format is still wonky as I just accepted whatever defaults the netbeans eclipse format plugin gave me.

          Matthew Ludlum added a comment - Done & done  - lemme know if the format is still wonky as I just accepted whatever defaults the netbeans eclipse format plugin gave me.

          Carlos Sanchez added a comment - implemented in https://github.com/jenkinsci/kubernetes-plugin/pull/204

          Martin Sander added a comment -

          Seems we have an escaping problem:

          podTemplate(name: "horst", label: "label", containers: [
                                  containerTemplate(name: 'debian',
                                          image: 'debian',
                                          ttyEnabled: true,
                                          command: 'cat',
                                  )
          ]) {
              withEnv(['C="C', 'D=D"',]) {
                  node("label") {
                      container('debian') {
                          sh '''echo "C='$C'" '''
                          sh '''echo "D='$D'" '''
                      }
                  }
              }
          }
          

          Leads to

          [full] Running shell script
          + echo C='C
          export D=D'
          C='C
          export D=D'
          [Pipeline] sh
          [full] Running shell script
          + echo D=''
          D=''
          

          csanchez new Ticket?

          Martin Sander added a comment - Seems we have an escaping problem: podTemplate(name: "horst" , label: "label" , containers: [ containerTemplate(name: 'debian' , image: 'debian' , ttyEnabled: true , command: 'cat' , ) ]) { withEnv([ 'C= "C' , 'D=D" ' ,]) { node( "label" ) { container( 'debian' ) { sh '''echo "C=' $C '" ' '' sh '''echo "D=' $D '" ' '' } } } } Leads to [full] Running shell script + echo C='C export D=D' C='C export D=D' [Pipeline] sh [full] Running shell script + echo D='' D='' csanchez new Ticket?

          yes please

          Carlos Sanchez added a comment - yes please

          Martin Sander added a comment -

          Martin Sander added a comment - See JENKINS-46670

            csanchez Carlos Sanchez
            seakip18 Matthew Ludlum
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: