The return value of the git step should be the commit hash, etc., and there should be a way to pass that back in to a subsequent git step in case you want to check out the same revision in another workspace (without copying files).

      The workaround is to archive and then unarchive sources.

          [JENKINS-26100] SCM steps should return revision state

          Gauthier R. added a comment -

          markewaite thanks a lot for your help, upgrading the Git Plugin to 3.3.2 indeed solved my problem !

          Gauthier R. added a comment - markewaite thanks a lot for your help, upgrading the Git Plugin to 3.3.2 indeed solved my problem !

          Norbert Lange added a comment -

          I might be dense, but I still dont understand how I would do this with a declarative pipeline (without script blocks).
          Any help here?

          withCheckout is nowhere to be found

          the env variables are not set by default:

          pipeline {
              agent none
              stages {
                  stage('checkout') {
                      post { always { deleteDir() } }
                      agent any
                      steps {
                          checkout([$class: 'GitSCM', 
                              userRemoteConfigs: [[url: 'https://github.com/docker/for-linux.git']]])
                          sh 'set'
                      }
                  }
              }
          }
          

          trying to stuff into a withEnv block fails with `java.lang.UnsupportedOperationException: must specify $class with an implementation of interface java.util.List`

          pipeline {
              agent none
              stages {
                  stage('checkout') {
                      post { always { deleteDir() } }
                      agent any
                      steps {
                          withEnv(checkout([$class: 'GitSCM', 
                              userRemoteConfigs: [[url: 'https://github.com/docker/for-linux.git']]])) {
                                  sh 'set'  
                              }
                      }
                  }
              }
          }
          

          Jenkins ver. 2.219 (docker alpine installation)
          git 4.1.1 true
          git-client 3.1.1 true
          workflow-scm-step 2.10 true

          Norbert Lange added a comment - I might be dense, but I still dont understand how I would do this with a declarative pipeline (without script blocks). Any help here? withCheckout is nowhere to be found the env variables are not set by default: pipeline { agent none stages { stage( 'checkout' ) { post { always { deleteDir() } } agent any steps { checkout([$class: 'GitSCM' , userRemoteConfigs: [[url: 'https: //github.com/docker/ for -linux.git' ]]]) sh 'set' } } } } trying to stuff into a withEnv block fails with `java.lang.UnsupportedOperationException: must specify $class with an implementation of interface java.util.List` pipeline { agent none stages { stage( 'checkout' ) { post { always { deleteDir() } } agent any steps { withEnv(checkout([$class: 'GitSCM' , userRemoteConfigs: [[url: 'https: //github.com/docker/ for -linux.git' ]]])) { sh 'set' } } } } } Jenkins ver. 2.219 (docker alpine installation) git 4.1.1 true git-client 3.1.1 true workflow-scm-step 2.10 true

          Mark Waite added a comment -

          nolange79 could you review the Jenkins Minute video "Using Git Environment Variables" and see if it resolves your issue?

          If it resolves your issue, then I'll add a git plugin documentation section on using the git plugin with Pipeline and will embed that video in the new section.

          Mark Waite added a comment - nolange79 could you review the Jenkins Minute video " Using Git Environment Variables " and see if it resolves your issue? If it resolves your issue, then I'll add a git plugin documentation section on using the git plugin with Pipeline and will embed that video in the new section.

          Norbert Lange added a comment -

          markewaite: No, video is generally a bad idea, and that specific one is not helping at all.

          Here is the problem in a single paragraph:

          checkout()
          // returns ""
          sh 'echo ${GIT_COMMIT}'
          // returns "null"
          echo "${env.GIT_COMMIT}"
          

          No environment variables are set.

          Norbert Lange added a comment - markewaite : No, video is generally a bad idea, and that specific one is not helping at all. Here is the problem in a single paragraph: checkout() // returns "" sh 'echo ${GIT_COMMIT}' // returns " null " echo "${env.GIT_COMMIT}" No environment variables are set.

          Mark Waite added a comment -

          nolange79 it looks to me like you're using declarative pipeline but then ignoring the default checkout which declarative pipeline provides for you, without explicitly stating that you intend to skip the default checkout. If you intend to skip the default checkout then you should extend your declarative pipeline with

          options {
              skipDefaultCheckout()
          }
          

          If you don't intend to skip the default checkout, then the techniques described in that video will work.

          If you intend to skip the default checkout, specify your own checkout, and use the return value from the checkout step to collect the settings used in the checkout, then it appears you'll need to use a script block in your declarative Pipeline. Refer to this example that I created while experimenting with the question. The example skips the default checkout, performs its own checkout, and assigns the return value of checkout() to the variable results. It then displays values from results.

          Mark Waite added a comment - nolange79 it looks to me like you're using declarative pipeline but then ignoring the default checkout which declarative pipeline provides for you, without explicitly stating that you intend to skip the default checkout. If you intend to skip the default checkout then you should extend your declarative pipeline with options { skipDefaultCheckout() } If you don't intend to skip the default checkout, then the techniques described in that video will work. If you intend to skip the default checkout, specify your own checkout, and use the return value from the checkout step to collect the settings used in the checkout, then it appears you'll need to use a script block in your declarative Pipeline. Refer to this example that I created while experimenting with the question. The example skips the default checkout, performs its own checkout, and assigns the return value of checkout() to the variable results . It then displays values from results .

          Mark Waite added a comment -

          nolange79 also, since this bug report is about the git step and not the checkout step, it would be better to use a different location for your question and for the answer to your question. When you ask a question in an issue, you limit the number of people that will answer the question to only those that are monitoring that bug. A question asked in the Jenkins mailing lists or the Jenkins chat channels can be seen by a much larger group.

          Mark Waite added a comment - nolange79 also, since this bug report is about the git step and not the checkout step, it would be better to use a different location for your question and for the answer to your question. When you ask a question in an issue, you limit the number of people that will answer the question to only those that are monitoring that bug. A question asked in the Jenkins mailing lists or the Jenkins chat channels can be seen by a much larger group.

          Norbert Lange added a comment - - edited

          Yeah, I posted the entire declarative pipeline couple posts before.

          I managed to get the variables with a script block, but I expected that there would be a way to get to this without breaking into scripting mode.

          What is a default checkout: where would the pipeline know the url from?
          Does withCheckout exist? its referenced in this thread.
          Would be nice to get withEnv working like I illustrated above.

          Docs send you completely in the wrong direction unfortunately.

          Edit: I replied here because this is often referenced as "answer"

          Norbert Lange added a comment - - edited Yeah, I posted the entire declarative pipeline couple posts before. I managed to get the variables with a script block, but I expected that there would be a way to get to this without breaking into scripting mode. What is a default checkout: where would the pipeline know the url from? Does withCheckout exist? its referenced in this thread. Would be nice to get withEnv working like I illustrated above. Docs send you completely in the wrong direction unfortunately. Edit: I replied here because this is often referenced as "answer"

          Mark Waite added a comment - - edited

          I managed to get the variables with a script block, but I expected that there would be a way to get to this without breaking into scripting mode.

          The variables are available if you don't use the checkout step in the declarative pipeline. The declarative pipeline has an implicit checkout step that happens before the Pipeline executes.

          What is a default checkout: where would the pipeline know the url from?

          When the Jenkinsfile for the Pipeline is read from SCM, the declarative pipeline uses that URL.

          Does withCheckout exist? its referenced in this thread.

          No, withCheckout does not exist. It was added to the scm step plugin May 5, 2017 and then removed on May 16, 2017.

          Docs send you completely in the wrong direction unfortunately.

          Which docs send you in the wrong direction?

          Mark Waite added a comment - - edited I managed to get the variables with a script block, but I expected that there would be a way to get to this without breaking into scripting mode. The variables are available if you don't use the checkout step in the declarative pipeline. The declarative pipeline has an implicit checkout step that happens before the Pipeline executes. What is a default checkout: where would the pipeline know the url from? When the Jenkinsfile for the Pipeline is read from SCM, the declarative pipeline uses that URL. Does withCheckout exist? its referenced in this thread. No, withCheckout does not exist. It was added to the scm step plugin May 5, 2017 and then removed on May 16, 2017 . Docs send you completely in the wrong direction unfortunately. Which docs send you in the wrong direction?

          Norbert Lange added a comment -

          "Which docs send you in the wrong direction?"

          Plain Pipelines using "inline Jenkinsfiles" seem to be largely ignored from all docs, similarly you often need to checkout multiple sources. If this is not explicitly mentioned, one would assume it works the same.
          The step returns a map of env variables already, so pushing those into withEnv would be a natural fit.

          That there is no "declarative" way to do this, is weird. I had many Problems with scripted pipelines, particularly when it comes to error handling (storing artifacts will silently fail, then cause an error at the end of the pipeline?) that I would want to stay clear of those.

          Norbert Lange added a comment - "Which docs send you in the wrong direction?" Plain Pipelines using "inline Jenkinsfiles" seem to be largely ignored from all docs, similarly you often need to checkout multiple sources. If this is not explicitly mentioned, one would assume it works the same. The step returns a map of env variables already, so pushing those into withEnv would be a natural fit. That there is no "declarative" way to do this, is weird. I had many Problems with scripted pipelines, particularly when it comes to error handling (storing artifacts will silently fail, then cause an error at the end of the pipeline?) that I would want to stay clear of those.

          Mark Waite added a comment -

          Thanks

          Mark Waite added a comment - Thanks

            abayer Andrew Bayer
            jglick Jesse Glick
            Votes:
            85 Vote for this issue
            Watchers:
            100 Start watching this issue

              Created:
              Updated:
              Resolved: