How To Checkout at a given commit in a Pipeline

Issue

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

Environment

Resolution

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 {
    checkout([
        $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.

References

Have more questions?

0 Comments

Please sign in to leave a comment.