Jenkins: Switch strategy for polled commit detection

This commit is contained in:
RichardG867
2021-11-24 20:24:06 -03:00
parent b6904db51f
commit 17d93758be

83
.ci/Jenkinsfile vendored
View File

@@ -81,24 +81,29 @@ def presetFlags = [
def anyFailure = false def anyFailure = false
def gitClone(repository, branch) { def gitClone(repository, branch) {
/* Read git tag from environment or set the default one. */
if (env.GIT_COMMIT == null)
env.GIT_COMMIT = "*/$branch"
println "[-] Using git tag [${env.GIT_COMMIT}]"
/* Clean workspace. */ /* Clean workspace. */
cleanWs() cleanWs()
/* Use stashes to avoid performing multiple clones. */ /* Use stashes to avoid performing multiple clones. */
if (env.GIT_STASHED != 'true') { if (env.GIT_STASHED != 'true') {
/* Perform clone/checkout. */ /* Perform clone/checkout. */
def scmVars = checkout poll: false, def scmVars = checkout poll: true,
changelog: true, changelog: true,
scm: [$class: 'GitSCM', scm: [$class: 'GitSCM',
branches: [[name: env.GIT_COMMIT]], branches: [[name: branch]],
extensions: [[$class: 'LocalBranch', localBranch: branch]],
userRemoteConfigs: [[url: repository]]] userRemoteConfigs: [[url: repository]]]
env.GIT_COMMIT = scmVars.GIT_COMMIT
if (env.GIT_COMMIT == null) {
/* Save the current HEAD commit. */
env.GIT_COMMIT = scmVars.GIT_COMMIT
} else {
/* Checkout the commit read from the polling log. */
if (isUnix())
sh "git checkout ${env.GIT_COMMIT} || exit 0"
else
bat "git checkout ${env.GIT_COMMIT} || exit /b 0"
}
println "[-] Using git commit [${env.GIT_COMMIT}]"
/* Stash data and mark it as stashed. */ /* Stash data and mark it as stashed. */
stash name: 'git' stash name: 'git'
@@ -147,46 +152,32 @@ pipeline {
steps { steps {
script { script {
/* Hack to extract the current HEAD commit from this build's git polling /* Extract the polled commit from the polling log, so that git checkout can be used
log. This avoids a race condition where HEAD changes in the time period to avoid JENKINS-20518 race conditions caused by two pushes too close together. */
between Jenkins polling the git repository and the first build node node('master') { /* must run on master node to read polling log */
performing the first git clone once ready. (See issue JENKINS-20518) */ /* Ignore exceptions as this is not really critical. */
if (env.GIT_COMMIT == null) { try {
/* This must run on the master node to read the polling log. */ /* Switch to this build's directory. */
node('master') { dir("${env.JENKINS_HOME}/jobs/${env.JOB_NAME}/builds/${env.BUILD_NUMBER}") {
/* Ignore exceptions as this is not really critical. */ /* Parse polling log. */
try { def pollingLog = readFile file: 'polling.log'
/* Switch to this build's directory. */ def match = pollingLog =~ /Latest remote head revision on [^ ]+ is: ([a-zA-Z0-9]+)/
dir("${env.JENKINS_HOME}/jobs/${env.JOB_NAME}/builds/${env.BUILD_NUMBER}") { if (match && match[0]) {
/* Parse polling log. */ env.GIT_COMMIT = match[0][1]
def pollingLog = readFile file: 'polling.log' println "[-] Read git commit [${env.GIT_COMMIT}] from polling log"
def match = pollingLog =~ /Latest remote head revision on [^ ]+ is: ([a-zA-Z0-9]+)/
if (match && match[0]) {
env.GIT_COMMIT = match[0][1]
println "[-] Read git tag [${env.GIT_COMMIT}] from polling log"
}
} }
} catch (e) {} }
} } catch (e) {}
}
/* Perform a git clone on any node to save polling data and, /* Adding to the above, run a git clone as soon as possible on any node
if polling log parsing failed, get the current HEAD commit. */ to further avoid race conditions caused by busy node executor delays. */
node { node {
/* Clean workspace. */ /* Run git clone. */
cleanWs() gitClone(repository, branch)
/* Perform clone/checkout. */ /* Clean workspace, in case this is running in a non-build node. */
def scmVars = checkout poll: true, cleanWs()
changelog: false,
scm: [$class: 'GitSCM',
branches: [[name: branch]],
userRemoteConfigs: [[url: repository]]]
if (env.GIT_COMMIT == null)
env.GIT_COMMIT = scmVars.GIT_COMMIT
/* Clean workspace again. */
cleanWs()
}
} }
/* Create source tarball. */ /* Create source tarball. */