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

Declarative Pipeline Environment ignored

    XMLWordPrintable

    Details

    • Similar Issues:

      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

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

            Show
            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.
            Hide
            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

            Show
            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
            Hide
            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.

            Show
            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.
            Show
            csanchez Carlos Sanchez added a comment - implemented in https://github.com/jenkinsci/kubernetes-plugin/pull/204
            Hide
            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=''
            

            Carlos Sanchez new Ticket?

            Show
            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='' Carlos Sanchez new Ticket?
            Hide
            csanchez Carlos Sanchez added a comment -

            yes please

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

              People

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

                Dates

                • Created:
                  Updated:
                  Resolved: