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

Respect Build/Node environment variables in Pipeline Checkout with Git

XMLWordPrintable

      The pipeline checkout step, in the context of GitSCM, does not pass the Build or Node environment variables down to the launched git process.

      As an example, if you pass an environment variable such as NO_PROXY or GIT_TRACE to the build environment either with withEnv or by adding it to a permanent agent configuration (Node Properties), the launched git process that execute the fetch/checkout on the executor does not use it. However if you export the NO_PROXY or GIT_TRACE variable in the node host and restart the agent process, then the variable is used...

      Note: I took NO_PROXY as an example as this is the scenario that I was testing and made me found that behavior. But the same happens for any environment variables. Such as GIT_TRACE.

      This seems to be pipeline specific. In a freestyle job, the build environment seems to be respected.

      This is probably an enhancement request but not 100 % which plugin(s) (Git Plugin ? Git Client Plugin ? Pipeline SCM ?) that does not pass the build / node environment variables properly to the process that execute the git command.

      Reproduction Steps

      • Spin up a fresh Jenkins instance
      • Configure a fake proxy under *Manage Jenkins > Manage Plugins > Advanced* (a proxy that does not allow to access the Git server we are about to use or does not really exist)
      • Connect a permanent agent
      • Create a Pipeline job that does the following:
      pipeline {
          agent {
              label "my-agent"
          }
          stages {
              stage('Main') {
                  steps {
                      sh "env"
                      checkout([$class: 'GitSCM', branches: [[name: '*/master']], [[url: '<https://github.com/jenkinsci/support-core.git']]])>
                  }
              }
          }
      }
      

      --> The checkout should fail because of the proxy configuration. EXPECTED.

      *Test 1: Test with withEnv*

      Now modify the Pipeline job with the following and run it:

      pipeline {
          agent {
              label "my-agent"
          }
          stages {
              stage('Main') {
                  steps {
                      withEnv(['NO_PROXY=.github.com,github.com']) {
                          sh "env"
                          checkout([$class: 'GitSCM', branches: [[name: '*/master']], [[url: '<https://github.com/jenkinsci/support-core.git']]])>
                      }
                  }
              }
          }
      }
      

      --> The checkout still fails because of the proxy configuration. NOT EXPECTED. Notice that 'env' shows the NO_PROXY variable.

      *Test 2: Test with Node Properties*

      • Configure the Permanent agent and add a *Node Properties > Environment Variables* named NO_PROXY with value github.com,.github.com
      • Modify the Pipeline to remove the withEnv { } block:
      pipeline {
          agent {
              label "my-agent"
          }
          stages {
              stage('Main') {
                  steps {
                      sh "env"
                      checkout([$class: 'GitSCM', branches: [[name: '*/master']], [[url: '<https://github.com/jenkinsci/support-core.git']]])>
                  }
              }
          }
      }
      
      • Run the Pipeline job again.

      --> The checkout still fails because of the proxy configuration. NOT EXPECTED. Notice that 'env' shows the NO_PROXY variable.

      *Test 3: Test with Agent Host environment*

      • Stop the permanent agent process and start it with NO_PROXY=github.com,.github.com java -jar agent.jar ...
      • Run the Pipeline job again

      --> it works! EXPECTED.

      *Test 4: Test with Freestyle and Node Environment*

      Last test that shows that this is pipeline specific:

      • Stop the permanent agent process and start it with java -jar agent.jar ...
      • Configure the Permanent agent and make sure it has the *Node Properties > Environment Variables* named NO_PROXY with value github.com,.github.com
      • Create a freestyle job that checkout the same repo and build on the permanent agent

      --> it works! EXPECTED. The variable configured in the *Node Properties > Environment Variables* of the node is used.

            markewaite Mark Waite
            allan_burdajewicz Allan BURDAJEWICZ
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: