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

Path is not getting set correctly in pipeline when there is a variable present

    XMLWordPrintable

    Details

    • Similar Issues:

      Description

      After upgrading to latest Pipeline version and it's dependencies (we also upgraded the blue ocean plugins but don't think that caused an issue here), we noticed that the PATH was not getting set in our Jenkins Pipeline Jobs. After debugging found that if I set PATH to a string inside the "environment" section, as such:

       

          environment {
              PATH="/a/new/bin:$PATH"
         }

       

      The PATH was updated successfully without any issues. However if I introduce a variable, the path does not get updated, and defaults to what is on the agent that runs the job. Example:

      pipeline {
            agent {  label 'docker'    }
          environment {
              PATH="/a/new/bin:${env.SCM_HOME}:$PATH"
              SCM_HOME="${env.SCM_HOME}"
         }
        stages {
          stage ('build') {
            steps {
              echo "PATH=$PATH"
              echo "SCM_HOME=$SCM_HOME"
            }
          }
        }
      }

       

      OUTPUT:

       
      [Pipeline] {

      [Pipeline] withEnv

      [Pipeline] {

      [Pipeline] stage

      [Pipeline] { (build)

      [Pipeline] echoPATH=/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/bin

      [Pipeline] echoSCM_HOME=/usr/local/bin/scmtools/

      [Pipeline] }

      [Pipeline] // stage

      [Pipeline] }

      [Pipeline] // withEnv

      [Pipeline] }

      [Pipeline] // node

      [Pipeline] End of PipelineFinished: SUCCESS
       

       

      Notice that SCM_HOME resolves fine outside of PATH and that /a/new/bin was not added to the path. However if I drop the variable it will set PATH fine.

        Attachments

          Issue Links

            Activity

            Hide
            abayer Andrew Bayer added a comment -

            Blanca Borro Escribano - please provide a Jenkinsfile that can reproduce this.

            Show
            abayer Andrew Bayer added a comment - Blanca Borro Escribano - please provide a Jenkinsfile that can reproduce this.
            Hide
            redeamer Michael Musenbrock added a comment - - edited

            Hi, I'm not sure if this is was Blanca Borro Escribano was referencing:

            pipeline {
                agent {
                    docker {
                        image 'alpine:latest'
                    }
                }
            
                environment {
                    PATHX = "${env.WORKSPACE}:${env.PATH}"
                    PATH = "${env.WORKSPACE}:${env.PATH}"
                }
            
                stages {
                    stage('Test') {
                        steps {
                            echo "PATH : ${env.PATH}"
                            echo "PATHX: ${env.PATHX}"
                            sh "printenv | grep PATH"
                        }
                    }
                }
            }

            Which prints:

            [Pipeline] echo PATH : /jenkins_slave/workspace/test:/usr/local/bin:/usr/bin:/bin:/usr/games
            [Pipeline] echo PATHX: /jenkins_slave/workspace/test:/jenkins_slave/workspace/test:/usr/local/bin:/usr/bin:/bin:/usr/games
            [Pipeline] sh
            [test] Running shell script
            + printenv
            + grep PATH
            PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
            CLASSPATH=
            PATHX=/jenkins_slave/workspace/test:/jenkins_slave/workspace/test:/usr/local/bin:/usr/bin:/bin:/usr/games
            

            So both variables are expanded correctly, but PATH is not passed to the environment of sh. Could also be a problem in the sh step.

            I'm also confused with the order of expansion: whether (in the environment stage) i put PATHX first or second, it seems that PATH gets always set first, because PATHX has the WORKSPACE variable content always prefixed twice

            Show
            redeamer Michael Musenbrock added a comment - - edited Hi, I'm not sure if this is was Blanca Borro Escribano was referencing: pipeline { agent { docker { image 'alpine:latest' } } environment { PATHX = "${env.WORKSPACE}:${env.PATH}" PATH = "${env.WORKSPACE}:${env.PATH}" } stages { stage( 'Test' ) { steps { echo "PATH : ${env.PATH}" echo "PATHX: ${env.PATHX}" sh "printenv | grep PATH" } } } } Which prints: [Pipeline] echo PATH : /jenkins_slave/workspace/test:/usr/local/bin:/usr/bin:/bin:/usr/games [Pipeline] echo PATHX: /jenkins_slave/workspace/test:/jenkins_slave/workspace/test:/usr/local/bin:/usr/bin:/bin:/usr/games [Pipeline] sh [test] Running shell script + printenv + grep PATH PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin CLASSPATH= PATHX=/jenkins_slave/workspace/test:/jenkins_slave/workspace/test:/usr/local/bin:/usr/bin:/bin:/usr/games So both variables are expanded correctly, but PATH is not passed to the environment of sh. Could also be a problem in the sh step. I'm also confused with the order of expansion: whether (in the environment stage) i put PATHX first or second, it seems that PATH gets always set first, because PATHX has the WORKSPACE variable content always prefixed twice
            Hide
            bbborro Blanca Borro Escribano added a comment -

            Hello, i managed to solve the problem. I think the problem was due to lack of knowledge from my part.

            Show
            bbborro Blanca Borro Escribano added a comment - Hello, i managed to solve the problem. I think the problem was due to lack of knowledge from my part.
            Hide
            acommuni Anthony Communier added a comment -

            Blanca Borro Escribano : I have the same issue , could you explain how you solve the problem ?

            Show
            acommuni Anthony Communier added a comment - Blanca Borro Escribano : I have the same issue , could you explain how you solve the problem ?
            Hide
            bbborro Blanca Borro Escribano added a comment -

            Anthony Communier the problem i had was that i was mixing the use of declarative pipeline with scripting pipeline. I finally used declarative pipeline for everything and found no problem.

            Show
            bbborro Blanca Borro Escribano added a comment - Anthony Communier the problem i had was that i was mixing the use of declarative pipeline with scripting pipeline. I finally used declarative pipeline for everything and found no problem.

              People

              • Assignee:
                abayer Andrew Bayer
                Reporter:
                jammurp James Murphy
              • Votes:
                0 Vote for this issue
                Watchers:
                6 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: