Jenkins Shared Library Structure and Doc<\/a><\/p>\nThen in Jenkinsfile you have, at the top:<\/p>\n
<\/p>\n
\n@Library('jenkins-nebula-core-lib') _\nimport org.nebula.Poc\nimport org.nebula.Whichawsregion\nimport org.nebula.Durationtime\n<\/pre>\n <\/p>\n
I created a checkout library file called cicdcheckoutspecific.groovy, and then extensions of that library:<\/p>\n
<\/p>\n
\n\/\/ src\/org\/nebula\/Cicdcheckoutspecific.groovy\npackage org.nebula\n\n\/\/ arg is a two-part string\n\/\/ 'hash:' or 'branch:'\n\/\/ default is null\ndef cicdCheckoutSpecific(String gittarget = null) {\n \/\/ here's what jenkins checkout SCM does to work through the repo\n \/\/ git init \/var\/lib\/jenkins\/jobs\/example-cicd-directory_Test\/workspace@script\n \/\/ git fetch --tags --progress \\\n \/\/ ssh:\/\/git@git.ouroath.com\/dmunsinger15\/example-cicd-directory.git +refs\/heads\/*:refs\/remotes\/origin\/*\n \/\/ git config remote.origin.url ssh:\/\/git@git.ouroath.com\/dmunsinger15\/example-cicd-directory.git # timeout=10\n \/\/ git config --add remote.origin.fetch +refs\/heads\/*:refs\/remotes\/origin\/* # timeout=10\n \/\/ git config remote.origin.url ssh:\/\/git@git.ouroath.com\/dmunsinger15\/example-cicd-directory.git # timeout=10\n \/\/ git fetch --tags --progress \\\n \/\/ ssh:\/\/git@git.ouroath.com\/dmunsinger15\/example-cicd-directory.git +refs\/heads\/*:refs\/remotes\/origin\/*\n \/\/ git show-ref --tags -d # timeout=10\n \/\/ git config core.sparsecheckout # timeout=10\n \/\/ git checkout -f 1f5dbd4f039e881763c789d4e5b206368de6a21\n\n \/\/ if this is FIRST BUILD, directly look for and checkout latest commithash regradless of branch\n \/\/ to accomodate CICD Discover plugin \/build first build api call\n \/\/ set up tmp environs\n\n \/\/ create the tmp repo\n sh(script:\"\"\"\n rm -rf ${WORKSPACE}\/ghe_tmp\n git init ${WORKSPACE}\/ghe_tmp\n cd ${WORKSPACE}\/ghe_tmp\n git fetch --tags --progress ${GIT_URL} +refs\/heads\/*:refs\/remotes\/origin\/*\n git config remote.origin.url ${GIT_URL}\n \"\"\", returnStdout:true)\n\n if (gittarget == null) {\n \/\/ find in that tmp env latest commit hash\n def my_hash = sh(\n script:\"\"\"\n cd ${WORKSPACE}\/ghe_tmp\n git log --all -n 1 | egrep ^commit | awk '{ print \\$2 }'\n \"\"\", returnStdout:true).trim()\n\n \/\/ checkout that commit hash\n \/\/ this one works, so see if we can drop the credientials and have\n \/\/ it default back or if that breaks this again...\n def scmVars = checkout([$class:'GitSCM', branches:[[name:\"${my_hash}\"]],\n doGenerateSubmoduleConfigurations:false,\n extensions:[[$class:'LocalBranch', localBranch:'**']],\n submoduleCfg:[],\n userRemoteConfigs:[[credentialsId:'c0e21fda-697c-433c-8f1f-74e9f018b0aa',\n url:\"${GIT_URL}\"]]])\n env.GIT_COMMIT = scmVars.GIT_COMMIT\n env.GIT_BRANCH = scmVars.GIT_BRANCH\n def info = \"found 1st Build: ${my_hash}\"\n return info\n } else {\n \/\/ is this a hash or branch?\n if (gittarget.contains(\"hash:\")) {\n \/\/split out hash value\n def myrawhash = gittarget.split(\":\")\n def myspecifichash = myrawhash[1]\n \/\/ do the checkout targetting the hash instead of latest\n def scmVars = checkout([$class:'GitSCM', branches:[[name:\"${myspecifichash}\"]],\n doGenerateSubmoduleConfigurations:false,\n extensions:[[$class:'LocalBranch', localBranch:'**']],\n submoduleCfg:[],\n userRemoteConfigs:[[credentialsId:'c0e21fda-697c-433c-8f1f-74e9f018b0aa',\n url:\"${GIT_URL}\"]]])\n env.GIT_COMMIT = scmVars.GIT_COMMIT\n env.GIT_BRANCH = scmVars.GIT_BRANCH\n def info = \"checked out ${myspecifichash}\"\n return info\n } else if (gittarget.contains(\"branch:\")) {\n \/\/ split out branch value\n def myrawbranch = gittarget.split(\":\")\n def myspecificbranch = myrawbranch[1]\n \/\/ do the checkout targetting the hash instead of latest\n def scmVars = checkout([$class:'GitSCM', branches:[[name:\"${myspecificbranch}\"]],\n doGenerateSubmoduleConfigurations:false,\n extensions:[[$class:'LocalBranch', localBranch:\"${myspecificbranch}\"]],\n submoduleCfg:[],\n userRemoteConfigs:[[credentialsId:'c0e21fda-697c-433c-8f1f-74e9f018b0aa',\n url:\"${GIT_URL}\"]]])\n env.GIT_COMMIT = scmVars.GIT_COMMIT\n env.GIT_BRANCH = scmVars.GIT_BRANCH\n def info = \"found branch in gittarget: ${myspecificbranch}\"\n return info\n }\n }\n}\n<\/pre>\n <\/p>\n
This then goes in the checkout block of the Jenkinsfile something like:<\/p>\n
<\/p>\n
\n stage('Checkout') {\n steps {\n \/\/ working through checkout SCM rework\n script {\n \/\/checkout scm\n echo \"*** calling cicdCheckout, no args ***\"\n def b = new org.nebula.Cicdcheckoutspecific()\n RESULT = b.cicdCheckoutSpecific()\n echo \"${RESULT}\"\n }\n }\n }\n<\/pre>\n <\/p>\n
Thus the “throw it up in the air and pick the alphabetical first branch” behavior gets bypassed.<\/p>\n
— doug<\/p>\n","protected":false},"excerpt":{"rendered":"
Github sends the full branch and committed information with the notifyCommit it sends to Jenkins. Jenkins passes just the repo url through to the git plugin. The plugin then does calculations to find the correct commit id to build. If Jenkins has state, and has built this job in the past, and there’s a commit…<\/p>\n","protected":false},"author":1,"featured_media":1959,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,17,24,26,31],"tags":[106,162,180,250],"class_list":["post-2056","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cicd","category-devops","category-github","category-groovy","category-jenkins","tag-checkout-scm","tag-groovy","tag-jenkins","tag-shared-library"],"_links":{"self":[{"href":"https:\/\/wp.dougmunsinger.com\/wp-json\/wp\/v2\/posts\/2056"}],"collection":[{"href":"https:\/\/wp.dougmunsinger.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wp.dougmunsinger.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wp.dougmunsinger.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wp.dougmunsinger.com\/wp-json\/wp\/v2\/comments?post=2056"}],"version-history":[{"count":0,"href":"https:\/\/wp.dougmunsinger.com\/wp-json\/wp\/v2\/posts\/2056\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wp.dougmunsinger.com\/wp-json\/wp\/v2\/media\/1959"}],"wp:attachment":[{"href":"https:\/\/wp.dougmunsinger.com\/wp-json\/wp\/v2\/media?parent=2056"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wp.dougmunsinger.com\/wp-json\/wp\/v2\/categories?post=2056"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wp.dougmunsinger.com\/wp-json\/wp\/v2\/tags?post=2056"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}