How To Checkout at a given commit in a Pipeline


I would like to checkout code at a given commit in a Pipeline but the git step only allows to use a branch.



You can accomplish this using the generic scm step.
This steps is much more verbose that the git step but it provides access to most of the parameters of the git plugin.

The easiest way to configure it is to use the Pipeline Snippet Generator.
Please refer to Pipeline: Best Practices if you are unsure how to access the Snippet Generator.

Here is a simple example that will checkout from gitUrl at commit commitId:

node {
        $class: 'GitSCM', 
        branches: [[name: ${commitId}]], 
        doGenerateSubmoduleConfigurations: false, 
        extensions: [], 
        submoduleCfg: [], 
        userRemoteConfigs: [[url: ${gitUrl}]]])
    sh 'ls -al'

Tested product/plugin versions

Jenkins 2.176.x with Git Plugin version 3.10.0.


Have more questions?


Please sign in to leave a comment.