Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins pipeline: checkout explicit git commit

I want to be able to say something like:

git branch: commitHash, credentialsId: credentialsId, url: url 

The usecase: I'm doing parallel build and test runs on different platforms, and want to ensure each gets the same code. It is C++, and we build on separate platforms as well as building on them.

If I do the above, it fails - the underlying code assumes the given branch actually is a branch, or you get something like:

[Linux64 Build]  > git rev-parse origin/e4b6c976a0a986c348a211579f1e8fd32cf29567^{commit} # timeout=10 [Pipeline] [Linux64 Build] } [Pipeline] [Linux64 Build] // dir [Pipeline] [Linux64 Build] } [Pipeline] [Linux64 Build] // node [Pipeline] [Linux64 Build] } [Linux64 Build] Failed in branch Linux64 Build 

I've seen variations on this question asked before, although no actual answers - just suggestions like to stash the source code instead, etc. Not really what I'm after.

The documentation suggests it ought to be possible to give explicit commit hashes, possibly using branches instead, but I can't work out the syntax and can't find any examples. When I do it, I get the master branch, I think - in our setup, master does not work.

So far, the only solution I've found has been to checkout the branch and then explicitly call git to get the commit:

                git branch: branch, credentialsId: credentialsId, url: url                 sh 'git checkout ' + commitHash 

(where branch is the branch I originally got the hash for at the top of the job. It works but is not the neatest.

Anybody got a better way?

like image 200
johnfo Avatar asked Apr 25 '17 13:04

johnfo


People also ask

How do I create a specific commit in Jenkins?

Pass the commit sha1 to a predefined parameter and in Build-Execute shell run git checkout <commit> before the build process starts. Some extra work is needed to make sure the check-out goes well. Check the box This project is parameterized and then you can add predefined parameters.

What is scm checkout in Jenkins?

Since the Jenkinsfile is pulled from the source repo, “checkout scm” provides an easy way to access right revision of source code. Here, “checkout” variable will checkout the source from source repo and it accepts the scm variable which instructs the checkout step to clone the specific revision.


1 Answers

Use a general scm step

checkout([$class: 'GitSCM', branches: [[name: commitHash ]],      userRemoteConfigs: [[url: 'http://git-server/user/repository.git']]]) 
like image 165
Yuri G. Avatar answered Sep 26 '22 06:09

Yuri G.