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

Declarative Pipeline Environment ignored

    XMLWordPrintable

Details

    Description

      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.

      Attachments

        Issue Links

          Activity

            seakip18 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.

            seakip18 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

            csanchez 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.

            seakip18 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.
            csanchez Carlos Sanchez added a comment - implemented in https://github.com/jenkinsci/kubernetes-plugin/pull/204
            0x89 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?

            0x89 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

            csanchez Carlos Sanchez added a comment - yes please
            0x89 Martin Sander added a comment - See JENKINS-46670

            People

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

              Dates

                Created:
                Updated:
                Resolved: