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

checkout scm step does not populate environment with GIT_*** variables in non-multibranch pipelines

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Won't Do
    • Icon: Critical Critical
    • git-plugin, scm-api-plugin
    • None

      Pipeline from SCM does not populate environment with variables as claimed by https://wiki.jenkins.io/display/JENKINS/Git+Plugin when pipeline is non-multibranch.

      Contrived example:

      // a library with convenience functions
      // unused in this particular build, but relevant
      // as it's used in all 'real' builds
      @Library('jenkins-libraries') _ 
      
      // ideally, we would love to know at least the remote branch name here
      
      node ('linux_staging') {
        stage('checkout') {
          checkout scm
          // the next best thing would be to know the branch name here
      
          sh "echo ${env.GIT_BRANCH}"  // empty
          echo sh(script: "env | sort", returnStdout: true)  // no sign of them here
          echo env.GIT_BRANCH  // null
        }
      }

      Build log

      Started by an SCM change
      Checking out git git@git.tobii.intra:/project/oem/platform/tools/gitolite_testing.git into /jenkinsstorage/jenkins/workspace/infra/Tests/gitolite_hook_tests/test_trigger_feature@script to read Jenkinsfile
       > git rev-parse --is-inside-work-tree # timeout=10
      Fetching changes from the remote Git repository
       > git config remote.origin.url git@git.tobii.intra:/project/oem/platform/tools/gitolite_testing.git # timeout=10
      Fetching upstream changes from git@git.tobii.intra:/project/oem/platform/tools/gitolite_testing.git
       > git --version # timeout=10
      using GIT_SSH to set credentials These credentials are to be used by jenkins/nodes to connect with git using user tts-tools
       > git fetch --tags --progress git@git.tobii.intra:/project/oem/platform/tools/gitolite_testing.git +refs/heads/f/*:refs/remotes/origin/f/*
      Seen branch in repository origin/f/PTOOL-112233
      Seen branch in repository origin/f/PTOOL-123456
      Seen branch in repository origin/f/PTOOL-543
      Seen branch in repository origin/f/PTOOL-666
      Seen branch in repository origin/f/PTOOL-do-stuff
      Seen 5 remote branches
       > git show-ref --tags -d # timeout=10
      Checking out Revision 54d55d769c0ea5e320f8706b2d016c7bea22fe55 (origin/f/PTOOL-do-stuff)
       > git config core.sparsecheckout # timeout=10
       > git checkout -f 54d55d769c0ea5e320f8706b2d016c7bea22fe55
       > git branch -a -v --no-abbrev # timeout=10
       > git checkout -b f/PTOOL-do-stuff 54d55d769c0ea5e320f8706b2d016c7bea22fe55
      Commit message: "Jenkinsfile"
      First time build. Skipping changelog.
      Running in Durability level: MAX_SURVIVABILITY
      Loading library jenkins-libraries@master
      Attempting to resolve master from remote references...
       > git --version # timeout=10
      using GIT_SSH to set credentials These credentials are to be used by jenkins/nodes to connect with git using user tts-tools
       > git ls-remote -h -t git@git.tobii.intra:prereqs/jenkins_libraries.git # timeout=10
      Found match: refs/heads/master revision 1dd4f56b1444ffa2b65de0e6d689c195d23632ff
      Cloning the remote Git repository
      Cloning repository git@git.tobii.intra:prereqs/jenkins_libraries.git
       > git init /jenkinsstorage/jenkins/workspace/infra/Tests/gitolite_hook_tests/test_trigger_feature@libs/jenkins-libraries # timeout=10
      Fetching upstream changes from git@git.tobii.intra:prereqs/jenkins_libraries.git
       > git --version # timeout=10
      using GIT_SSH to set credentials These credentials are to be used by jenkins/nodes to connect with git using user tts-tools
       > git fetch --tags --progress git@git.tobii.intra:prereqs/jenkins_libraries.git +refs/heads/*:refs/remotes/origin/*
       > git config remote.origin.url git@git.tobii.intra:prereqs/jenkins_libraries.git # timeout=10
       > git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
       > git config remote.origin.url git@git.tobii.intra:prereqs/jenkins_libraries.git # timeout=10
      Fetching upstream changes from git@git.tobii.intra:prereqs/jenkins_libraries.git
      using GIT_SSH to set credentials These credentials are to be used by jenkins/nodes to connect with git using user tts-tools
       > git fetch --tags --progress git@git.tobii.intra:prereqs/jenkins_libraries.git +refs/heads/*:refs/remotes/origin/*
      Checking out Revision 1dd4f56b1444ffa2b65de0e6d689c195d23632ff (master)
       > git config core.sparsecheckout # timeout=10
       > git checkout -f 1dd4f56b1444ffa2b65de0e6d689c195d23632ff
      Commit message: "PTOOL-1520_doublelock_remove_printouts"
      [Pipeline] node
      Running on linux_staging in /datadisk/jenkins/workspace/infra/Tests/gitolite_hook_tests/test_trigger_feature
      [Pipeline] {
      [Pipeline] stage
      [Pipeline] { (checkout)
      [Pipeline] checkout
       > git rev-parse --is-inside-work-tree # timeout=10
      Fetching changes from the remote Git repository
       > git config remote.origin.url git@git.tobii.intra:/project/oem/platform/tools/gitolite_testing.git # timeout=10
      Fetching upstream changes from git@git.tobii.intra:/project/oem/platform/tools/gitolite_testing.git
       > git --version # timeout=10
      using GIT_SSH to set credentials These credentials are to be used by jenkins/nodes to connect with git using user tts-tools
       > git fetch --tags --progress git@git.tobii.intra:/project/oem/platform/tools/gitolite_testing.git +refs/heads/f/*:refs/remotes/origin/f/*
      Seen branch in repository origin/f/PTOOL-112233
      Seen branch in repository origin/f/PTOOL-123456
      Seen branch in repository origin/f/PTOOL-543
      Seen branch in repository origin/f/PTOOL-666
      Seen branch in repository origin/f/PTOOL-do-stuff
      Seen 5 remote branches
       > git show-ref --tags -d # timeout=10
      Checking out Revision 54d55d769c0ea5e320f8706b2d016c7bea22fe55 (origin/f/PTOOL-do-stuff)
       > git config core.sparsecheckout # timeout=10
       > git checkout -f 54d55d769c0ea5e320f8706b2d016c7bea22fe55
       > git branch -a -v --no-abbrev # timeout=10
       > git checkout -b f/PTOOL-do-stuff 54d55d769c0ea5e320f8706b2d016c7bea22fe55
      Commit message: "Jenkinsfile"
      [Pipeline] sh
      [test_trigger_feature] Running shell script
      + echo null
      null
      [Pipeline] sh
      [test_trigger_feature] Running shell script
      + sort
      + env
      [Pipeline] echo
      BUILD_DISPLAY_NAME=#9
      BUILD_ID=9
      BUILD_NUMBER=9
      BUILD_TAG=jenkins-infra-Tests-gitolite_hook_tests-test_trigger_feature-9
      BUILD_URL=https://tobiitechjenkins.tobii.intra/job/infra/job/Tests/job/gitolite_hook_tests/job/test_trigger_feature/9/
      CI=true
      EXECUTOR_NUMBER=0
      HOME=/home/tt-tools
      HUDSON_COOKIE=4e1bdaa3-3ca4-4286-b9ee-6b8e57eb1a1a
      HUDSON_HOME=/jenkinsstorage/jenkins
      HUDSON_SERVER_COOKIE=8faf0bbbf409f2f0
      HUDSON_URL=https://tobiitechjenkins.tobii.intra/
      JENKINS_HOME=/jenkinsstorage/jenkins
      JENKINS_NODE_COOKIE=fdfcfdf6-6441-4673-acf3-382798ae6c69
      JENKINS_SERVER_COOKIE=durable-1f3503ac3f6209410f79edc5a86ce920
      JENKINS_URL=https://tobiitechjenkins.tobii.intra/
      JOB_BASE_NAME=test_trigger_feature
      JOB_DISPLAY_URL=https://tobiitechjenkins.tobii.intra/job/infra/job/Tests/job/gitolite_hook_tests/job/test_trigger_feature/display/redirect
      JOB_NAME=infra/Tests/gitolite_hook_tests/test_trigger_feature
      JOB_URL=https://tobiitechjenkins.tobii.intra/job/infra/job/Tests/job/gitolite_hook_tests/job/test_trigger_feature/
      LANG=en_US.UTF-8
      LOGNAME=tt-tools
      MAIL=/var/mail/tt-tools
      NODE_LABELS=linux_staging
      NODE_NAME=linux_staging
      OLDPWD=/home/tt-tools
      PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
      PWD=/datadisk/jenkins/workspace/infra/Tests/gitolite_hook_tests/test_trigger_feature
      RUN_CHANGES_DISPLAY_URL=https://tobiitechjenkins.tobii.intra/job/infra/job/Tests/job/gitolite_hook_tests/job/test_trigger_feature/9/display/redirect?page=changes
      RUN_DISPLAY_URL=https://tobiitechjenkins.tobii.intra/job/infra/job/Tests/job/gitolite_hook_tests/job/test_trigger_feature/9/display/redirect
      SHELL=/bin/bash
      SHLVL=1
      SignPassword=*
      SSH_CLIENT=*
      SSH_CONNECTION=*
      STAGE_NAME=checkout
      USER=tt-tools
      _=/usr/bin/java
      WORKSPACE=/datadisk/jenkins/workspace/infra/Tests/gitolite_hook_tests/test_trigger_feature
      XDG_RUNTIME_DIR=/run/user/1000
      XDG_SESSION_ID=2388
      
      [Pipeline] echo
      null
      [Pipeline] }
      [Pipeline] // stage
      [Pipeline] }
      [Pipeline] // node
      [Pipeline] End of Pipeline
      Finished: SUCCESS

      The more real use case is that we have builds that use the parallel-command to execute builds on multiple OS's using various compilers, each (well, most) separate parallel stages do a checkout scm before compilation starts. Finally, after parallel returns, a final stage is supposed to use information about what was actually built to perform common things.

      Expected behavior:

      Environment populated with GIT_XYZ variables after checkout scm, or even better when the build starts.

      Actual behavior:

      checkout scm does not lead to the environment being populated with GIT_XYZ, unless in a multibranch configuration. Technically we could use the map returned from checkout scm to populate the environment ourselves, but it feels like this functionality should be provided by the plugin itself. Furthermore, in this particular instance the branch name is only necessary in a final stage, after parallel building has completed, so if using the workaround that stage would have to checkout scm as well, or rely on one of the parallel stages to set a variable in the outer scope.

            Unassigned Unassigned
            emanuel_tobiitech Emanuel Birge
            Votes:
            8 Vote for this issue
            Watchers:
            10 Start watching this issue

              Created:
              Updated:
              Resolved: