2018-10-16

Jenkins checkout git by commit id parameter

Discussion forums on Internet are providing answers for your question "how to check out specific git hash in Jenkins" quite easily for a manual procedure, but if you want to do it programmatically inside pipeline defined in Jenkins file or even by parameter problems raise.


It makes sense to check out specific git hash because it's unique inside repository and if you add git sha in job configuration GUI (Branch Specifier (blank for 'any')) then you really get the specific version in your workspace, so where is the problem?


Inside pipeline, block checkout you need to echo this commit from the variable by only one syntax even there are at least 3 other different ways usually works for printing the variable.

checkout([$class: 'GitSCM', branches: [[name: "ECHO GIT SHA HERE"]], userRemoteConfigs: [[credentialsId: 'string', url: 'string']]])

So the only one correct way is to use:
  • branches: [[name: "${params.BRANCH_NAME}"]],

because this are not working (but they are still printing value):
  • branches: [[name: env.BRANCH_NAME]], 
  • branches: [[name: ${env.BRANCH_NAME}]], 
  • branches: [[name: "${BRANCH_NAME}"]],


So to solve this I burned many hours. The syntax of Groovy is not enough mature yet, anyway I hope this can help you and if you need to do more magic with parameters as git values, I can recommend this plugin.

Žádné komentáře :

Okomentovat

Dotaz, připomínka, oprava?
(pokud máte problém s vložením příspěvku, vyzkoušejte to v prohlížeči Chrome)