How To Checkout at a given commit in a Pipeline

Article ID:360033341851
1 minute readKnowledge base

Issue

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

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

This article is part of our Knowledge Base and is provided for guidance-based purposes only. The solutions or workarounds described here are not officially supported by CloudBees and may not be applicable in all environments. Use at your own discretion, and test changes in a safe environment before applying them to production systems.