How to checkout git repo with an specific revision in Jenkins How to checkout git repo with an specific revision in Jenkins jenkins jenkins

How to checkout git repo with an specific revision in Jenkins


As git treats any branch as a pointer to commit, you can specify the commit in branch specifier.

EDIT (reply to comment):What you call a branch, is in fact just a pointer to a particular commit. Git has no such branches as svn; thus if you want to deploy a specific commit, you just supply it.

I understand you don't want to change to job every time you need to build it. You can make the build parametrized and then use the parameter as branch specifier.


In Pre Steps add this script under the Execute shell:

# update the local git repositorygit fetch # pull the desired branchgit pull origin <branch># checkout the specific commit you want.git checkout <commit version>


I am using Jenkins Pipeline.

I can deploy old commit (any old commit), just run checkout in gitlab docker image, before deploy:

node {stage('checkout') {    checkout scm}docker.image('docker.io/gitlab/gitlab-ce').inside('-u root -e MAVEN_OPTS="-Duser.home=./"') {    stage('git checkout') {        sh 'git checkout ${REV_VER}'        }}

After this, i use any other docker image for build, and deploy.

Example:

docker.image('openjdk:8').inside('-u root -e MAVEN_OPTS="-Duser.home=./"') {        stage('build') {            build process*        }        stage('deploy') {            deploy process*        }}

REV_VER, this is string parametr in Jenkins config.

Default variable - REV_VER (in Jenkins config) - Develop (my work branch).

But if you need deploy any old commit, just change variable on short ID commit (like "5221e28") in string REV_VER, when you run "Build with Parametrs"

p.s. OMG, i hope, you understand what i wrote, because my English is very bad =)

Good luck