diff --git a/libraries/access-token-encryptor/Jenkinsfile b/libraries/access-token-encryptor/Jenkinsfile index 412fe33b39..9d8026210c 100644 --- a/libraries/access-token-encryptor/Jenkinsfile +++ b/libraries/access-token-encryptor/Jenkinsfile @@ -14,11 +14,38 @@ pipeline { environment { BRANCH_NAME = "${env.CHANGE_BRANCH ? env.CHANGE_BRANCH : env.BRANCH_NAME}" JENKINS_BUILD_NUMBER = "${BUILD_NUMBER}" - BUILD_NUMBER = "${SHORT_SHA}_${BUILD_NUMBER}" - COMMIT_SHA = "${GIT_COMMIT}" - SHORT_SHA = "${GIT_COMMIT.take(7)}" } stages { + stage('Set Build Variables') { + steps { + script { + def relevantCommitHash + if (env.CHANGE_BRANCH) { + def commitExistsOnRemote = sh(script: "git branch --remotes --contains ${GIT_COMMIT}", returnStdout: true).trim() + if (commitExistsOnRemote) { + echo "PR build detected, but commit exists on remote. Using ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } else { + def parentCommits = sh(script: 'git rev-parse HEAD^@', returnStdout: true).trim().split('\n') + if (parentCommits.size() >= 2) { + echo "PR build detected. Jenkins checked out a merge commit: ${GIT_COMMIT} (parents: ${parentCommits.join(', ')})" + relevantCommitHash = parentCommits[0] + echo "Using first parent (branch commit): ${relevantCommitHash}" + } else { + echo "WARN: PR build detected, but ${GIT_COMMIT} is neither a merge commit, nor does it exist on the remote." + relevantCommitHash = "${GIT_COMMIT}" + } + } + } else { + echo "Branch build detected. Using commit: ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } + env.COMMIT_SHA = relevantCommitHash + env.SHORT_SHA = relevantCommitHash.take(7) + env.BUILD_NUMBER = "${env.SHORT_SHA}_${env.JENKINS_BUILD_NUMBER}" + } + } + } stage('Stage 1') { parallel { stage('Install monorepo') { diff --git a/libraries/fetch-utils/Jenkinsfile b/libraries/fetch-utils/Jenkinsfile index c056b8cecb..f75d4faa0b 100644 --- a/libraries/fetch-utils/Jenkinsfile +++ b/libraries/fetch-utils/Jenkinsfile @@ -14,11 +14,38 @@ pipeline { environment { BRANCH_NAME = "${env.CHANGE_BRANCH ? env.CHANGE_BRANCH : env.BRANCH_NAME}" JENKINS_BUILD_NUMBER = "${BUILD_NUMBER}" - BUILD_NUMBER = "${SHORT_SHA}_${BUILD_NUMBER}" - COMMIT_SHA = "${GIT_COMMIT}" - SHORT_SHA = "${GIT_COMMIT.take(7)}" } stages { + stage('Set Build Variables') { + steps { + script { + def relevantCommitHash + if (env.CHANGE_BRANCH) { + def commitExistsOnRemote = sh(script: "git branch --remotes --contains ${GIT_COMMIT}", returnStdout: true).trim() + if (commitExistsOnRemote) { + echo "PR build detected, but commit exists on remote. Using ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } else { + def parentCommits = sh(script: 'git rev-parse HEAD^@', returnStdout: true).trim().split('\n') + if (parentCommits.size() >= 2) { + echo "PR build detected. Jenkins checked out a merge commit: ${GIT_COMMIT} (parents: ${parentCommits.join(', ')})" + relevantCommitHash = parentCommits[0] + echo "Using first parent (branch commit): ${relevantCommitHash}" + } else { + echo "WARN: PR build detected, but ${GIT_COMMIT} is neither a merge commit, nor does it exist on the remote." + relevantCommitHash = "${GIT_COMMIT}" + } + } + } else { + echo "Branch build detected. Using commit: ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } + env.COMMIT_SHA = relevantCommitHash + env.SHORT_SHA = relevantCommitHash.take(7) + env.BUILD_NUMBER = "${env.SHORT_SHA}_${env.JENKINS_BUILD_NUMBER}" + } + } + } stage('Stage 1') { parallel { stage('Install monorepo') { diff --git a/libraries/logger/Jenkinsfile b/libraries/logger/Jenkinsfile index 820844b1f3..8d5f60b88d 100644 --- a/libraries/logger/Jenkinsfile +++ b/libraries/logger/Jenkinsfile @@ -14,11 +14,38 @@ pipeline { environment { BRANCH_NAME = "${env.CHANGE_BRANCH ? env.CHANGE_BRANCH : env.BRANCH_NAME}" JENKINS_BUILD_NUMBER = "${BUILD_NUMBER}" - BUILD_NUMBER = "${SHORT_SHA}_${BUILD_NUMBER}" - COMMIT_SHA = "${GIT_COMMIT}" - SHORT_SHA = "${GIT_COMMIT.take(7)}" } stages { + stage('Set Build Variables') { + steps { + script { + def relevantCommitHash + if (env.CHANGE_BRANCH) { + def commitExistsOnRemote = sh(script: "git branch --remotes --contains ${GIT_COMMIT}", returnStdout: true).trim() + if (commitExistsOnRemote) { + echo "PR build detected, but commit exists on remote. Using ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } else { + def parentCommits = sh(script: 'git rev-parse HEAD^@', returnStdout: true).trim().split('\n') + if (parentCommits.size() >= 2) { + echo "PR build detected. Jenkins checked out a merge commit: ${GIT_COMMIT} (parents: ${parentCommits.join(', ')})" + relevantCommitHash = parentCommits[0] + echo "Using first parent (branch commit): ${relevantCommitHash}" + } else { + echo "WARN: PR build detected, but ${GIT_COMMIT} is neither a merge commit, nor does it exist on the remote." + relevantCommitHash = "${GIT_COMMIT}" + } + } + } else { + echo "Branch build detected. Using commit: ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } + env.COMMIT_SHA = relevantCommitHash + env.SHORT_SHA = relevantCommitHash.take(7) + env.BUILD_NUMBER = "${env.SHORT_SHA}_${env.JENKINS_BUILD_NUMBER}" + } + } + } stage('Stage 1') { parallel { stage('Install monorepo') { diff --git a/libraries/metrics/Jenkinsfile b/libraries/metrics/Jenkinsfile index c40a70807d..4abeccbf81 100644 --- a/libraries/metrics/Jenkinsfile +++ b/libraries/metrics/Jenkinsfile @@ -14,11 +14,38 @@ pipeline { environment { BRANCH_NAME = "${env.CHANGE_BRANCH ? env.CHANGE_BRANCH : env.BRANCH_NAME}" JENKINS_BUILD_NUMBER = "${BUILD_NUMBER}" - BUILD_NUMBER = "${SHORT_SHA}_${BUILD_NUMBER}" - COMMIT_SHA = "${GIT_COMMIT}" - SHORT_SHA = "${GIT_COMMIT.take(7)}" } stages { + stage('Set Build Variables') { + steps { + script { + def relevantCommitHash + if (env.CHANGE_BRANCH) { + def commitExistsOnRemote = sh(script: "git branch --remotes --contains ${GIT_COMMIT}", returnStdout: true).trim() + if (commitExistsOnRemote) { + echo "PR build detected, but commit exists on remote. Using ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } else { + def parentCommits = sh(script: 'git rev-parse HEAD^@', returnStdout: true).trim().split('\n') + if (parentCommits.size() >= 2) { + echo "PR build detected. Jenkins checked out a merge commit: ${GIT_COMMIT} (parents: ${parentCommits.join(', ')})" + relevantCommitHash = parentCommits[0] + echo "Using first parent (branch commit): ${relevantCommitHash}" + } else { + echo "WARN: PR build detected, but ${GIT_COMMIT} is neither a merge commit, nor does it exist on the remote." + relevantCommitHash = "${GIT_COMMIT}" + } + } + } else { + echo "Branch build detected. Using commit: ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } + env.COMMIT_SHA = relevantCommitHash + env.SHORT_SHA = relevantCommitHash.take(7) + env.BUILD_NUMBER = "${env.SHORT_SHA}_${env.JENKINS_BUILD_NUMBER}" + } + } + } stage('Stage 1') { parallel { stage('Install monorepo') { diff --git a/libraries/mongo-utils/Jenkinsfile b/libraries/mongo-utils/Jenkinsfile index b98430073b..372ff69e8d 100644 --- a/libraries/mongo-utils/Jenkinsfile +++ b/libraries/mongo-utils/Jenkinsfile @@ -14,11 +14,38 @@ pipeline { environment { BRANCH_NAME = "${env.CHANGE_BRANCH ? env.CHANGE_BRANCH : env.BRANCH_NAME}" JENKINS_BUILD_NUMBER = "${BUILD_NUMBER}" - BUILD_NUMBER = "${SHORT_SHA}_${BUILD_NUMBER}" - COMMIT_SHA = "${GIT_COMMIT}" - SHORT_SHA = "${GIT_COMMIT.take(7)}" } stages { + stage('Set Build Variables') { + steps { + script { + def relevantCommitHash + if (env.CHANGE_BRANCH) { + def commitExistsOnRemote = sh(script: "git branch --remotes --contains ${GIT_COMMIT}", returnStdout: true).trim() + if (commitExistsOnRemote) { + echo "PR build detected, but commit exists on remote. Using ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } else { + def parentCommits = sh(script: 'git rev-parse HEAD^@', returnStdout: true).trim().split('\n') + if (parentCommits.size() >= 2) { + echo "PR build detected. Jenkins checked out a merge commit: ${GIT_COMMIT} (parents: ${parentCommits.join(', ')})" + relevantCommitHash = parentCommits[0] + echo "Using first parent (branch commit): ${relevantCommitHash}" + } else { + echo "WARN: PR build detected, but ${GIT_COMMIT} is neither a merge commit, nor does it exist on the remote." + relevantCommitHash = "${GIT_COMMIT}" + } + } + } else { + echo "Branch build detected. Using commit: ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } + env.COMMIT_SHA = relevantCommitHash + env.SHORT_SHA = relevantCommitHash.take(7) + env.BUILD_NUMBER = "${env.SHORT_SHA}_${env.JENKINS_BUILD_NUMBER}" + } + } + } stage('Stage 1') { parallel { stage('Install monorepo') { diff --git a/libraries/o-error/Jenkinsfile b/libraries/o-error/Jenkinsfile index a2658d0870..9bd59ab7c7 100644 --- a/libraries/o-error/Jenkinsfile +++ b/libraries/o-error/Jenkinsfile @@ -14,11 +14,38 @@ pipeline { environment { BRANCH_NAME = "${env.CHANGE_BRANCH ? env.CHANGE_BRANCH : env.BRANCH_NAME}" JENKINS_BUILD_NUMBER = "${BUILD_NUMBER}" - BUILD_NUMBER = "${SHORT_SHA}_${BUILD_NUMBER}" - COMMIT_SHA = "${GIT_COMMIT}" - SHORT_SHA = "${GIT_COMMIT.take(7)}" } stages { + stage('Set Build Variables') { + steps { + script { + def relevantCommitHash + if (env.CHANGE_BRANCH) { + def commitExistsOnRemote = sh(script: "git branch --remotes --contains ${GIT_COMMIT}", returnStdout: true).trim() + if (commitExistsOnRemote) { + echo "PR build detected, but commit exists on remote. Using ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } else { + def parentCommits = sh(script: 'git rev-parse HEAD^@', returnStdout: true).trim().split('\n') + if (parentCommits.size() >= 2) { + echo "PR build detected. Jenkins checked out a merge commit: ${GIT_COMMIT} (parents: ${parentCommits.join(', ')})" + relevantCommitHash = parentCommits[0] + echo "Using first parent (branch commit): ${relevantCommitHash}" + } else { + echo "WARN: PR build detected, but ${GIT_COMMIT} is neither a merge commit, nor does it exist on the remote." + relevantCommitHash = "${GIT_COMMIT}" + } + } + } else { + echo "Branch build detected. Using commit: ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } + env.COMMIT_SHA = relevantCommitHash + env.SHORT_SHA = relevantCommitHash.take(7) + env.BUILD_NUMBER = "${env.SHORT_SHA}_${env.JENKINS_BUILD_NUMBER}" + } + } + } stage('Stage 1') { parallel { stage('Install monorepo') { diff --git a/libraries/object-persistor/Jenkinsfile b/libraries/object-persistor/Jenkinsfile index eb5f1b840f..6cf2daed93 100644 --- a/libraries/object-persistor/Jenkinsfile +++ b/libraries/object-persistor/Jenkinsfile @@ -14,11 +14,38 @@ pipeline { environment { BRANCH_NAME = "${env.CHANGE_BRANCH ? env.CHANGE_BRANCH : env.BRANCH_NAME}" JENKINS_BUILD_NUMBER = "${BUILD_NUMBER}" - BUILD_NUMBER = "${SHORT_SHA}_${BUILD_NUMBER}" - COMMIT_SHA = "${GIT_COMMIT}" - SHORT_SHA = "${GIT_COMMIT.take(7)}" } stages { + stage('Set Build Variables') { + steps { + script { + def relevantCommitHash + if (env.CHANGE_BRANCH) { + def commitExistsOnRemote = sh(script: "git branch --remotes --contains ${GIT_COMMIT}", returnStdout: true).trim() + if (commitExistsOnRemote) { + echo "PR build detected, but commit exists on remote. Using ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } else { + def parentCommits = sh(script: 'git rev-parse HEAD^@', returnStdout: true).trim().split('\n') + if (parentCommits.size() >= 2) { + echo "PR build detected. Jenkins checked out a merge commit: ${GIT_COMMIT} (parents: ${parentCommits.join(', ')})" + relevantCommitHash = parentCommits[0] + echo "Using first parent (branch commit): ${relevantCommitHash}" + } else { + echo "WARN: PR build detected, but ${GIT_COMMIT} is neither a merge commit, nor does it exist on the remote." + relevantCommitHash = "${GIT_COMMIT}" + } + } + } else { + echo "Branch build detected. Using commit: ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } + env.COMMIT_SHA = relevantCommitHash + env.SHORT_SHA = relevantCommitHash.take(7) + env.BUILD_NUMBER = "${env.SHORT_SHA}_${env.JENKINS_BUILD_NUMBER}" + } + } + } stage('Stage 1') { parallel { stage('Install monorepo') { diff --git a/libraries/overleaf-editor-core/Jenkinsfile b/libraries/overleaf-editor-core/Jenkinsfile index 91d0cc3c9d..8a1346f2aa 100644 --- a/libraries/overleaf-editor-core/Jenkinsfile +++ b/libraries/overleaf-editor-core/Jenkinsfile @@ -14,11 +14,38 @@ pipeline { environment { BRANCH_NAME = "${env.CHANGE_BRANCH ? env.CHANGE_BRANCH : env.BRANCH_NAME}" JENKINS_BUILD_NUMBER = "${BUILD_NUMBER}" - BUILD_NUMBER = "${SHORT_SHA}_${BUILD_NUMBER}" - COMMIT_SHA = "${GIT_COMMIT}" - SHORT_SHA = "${GIT_COMMIT.take(7)}" } stages { + stage('Set Build Variables') { + steps { + script { + def relevantCommitHash + if (env.CHANGE_BRANCH) { + def commitExistsOnRemote = sh(script: "git branch --remotes --contains ${GIT_COMMIT}", returnStdout: true).trim() + if (commitExistsOnRemote) { + echo "PR build detected, but commit exists on remote. Using ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } else { + def parentCommits = sh(script: 'git rev-parse HEAD^@', returnStdout: true).trim().split('\n') + if (parentCommits.size() >= 2) { + echo "PR build detected. Jenkins checked out a merge commit: ${GIT_COMMIT} (parents: ${parentCommits.join(', ')})" + relevantCommitHash = parentCommits[0] + echo "Using first parent (branch commit): ${relevantCommitHash}" + } else { + echo "WARN: PR build detected, but ${GIT_COMMIT} is neither a merge commit, nor does it exist on the remote." + relevantCommitHash = "${GIT_COMMIT}" + } + } + } else { + echo "Branch build detected. Using commit: ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } + env.COMMIT_SHA = relevantCommitHash + env.SHORT_SHA = relevantCommitHash.take(7) + env.BUILD_NUMBER = "${env.SHORT_SHA}_${env.JENKINS_BUILD_NUMBER}" + } + } + } stage('Stage 1') { parallel { stage('Install monorepo') { diff --git a/libraries/promise-utils/Jenkinsfile b/libraries/promise-utils/Jenkinsfile index 08e63e3ac3..29c4458c8d 100644 --- a/libraries/promise-utils/Jenkinsfile +++ b/libraries/promise-utils/Jenkinsfile @@ -14,11 +14,38 @@ pipeline { environment { BRANCH_NAME = "${env.CHANGE_BRANCH ? env.CHANGE_BRANCH : env.BRANCH_NAME}" JENKINS_BUILD_NUMBER = "${BUILD_NUMBER}" - BUILD_NUMBER = "${SHORT_SHA}_${BUILD_NUMBER}" - COMMIT_SHA = "${GIT_COMMIT}" - SHORT_SHA = "${GIT_COMMIT.take(7)}" } stages { + stage('Set Build Variables') { + steps { + script { + def relevantCommitHash + if (env.CHANGE_BRANCH) { + def commitExistsOnRemote = sh(script: "git branch --remotes --contains ${GIT_COMMIT}", returnStdout: true).trim() + if (commitExistsOnRemote) { + echo "PR build detected, but commit exists on remote. Using ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } else { + def parentCommits = sh(script: 'git rev-parse HEAD^@', returnStdout: true).trim().split('\n') + if (parentCommits.size() >= 2) { + echo "PR build detected. Jenkins checked out a merge commit: ${GIT_COMMIT} (parents: ${parentCommits.join(', ')})" + relevantCommitHash = parentCommits[0] + echo "Using first parent (branch commit): ${relevantCommitHash}" + } else { + echo "WARN: PR build detected, but ${GIT_COMMIT} is neither a merge commit, nor does it exist on the remote." + relevantCommitHash = "${GIT_COMMIT}" + } + } + } else { + echo "Branch build detected. Using commit: ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } + env.COMMIT_SHA = relevantCommitHash + env.SHORT_SHA = relevantCommitHash.take(7) + env.BUILD_NUMBER = "${env.SHORT_SHA}_${env.JENKINS_BUILD_NUMBER}" + } + } + } stage('Stage 1') { parallel { stage('Install monorepo') { diff --git a/libraries/ranges-tracker/Jenkinsfile b/libraries/ranges-tracker/Jenkinsfile index c94e6241dc..5e89ef5a19 100644 --- a/libraries/ranges-tracker/Jenkinsfile +++ b/libraries/ranges-tracker/Jenkinsfile @@ -14,11 +14,38 @@ pipeline { environment { BRANCH_NAME = "${env.CHANGE_BRANCH ? env.CHANGE_BRANCH : env.BRANCH_NAME}" JENKINS_BUILD_NUMBER = "${BUILD_NUMBER}" - BUILD_NUMBER = "${SHORT_SHA}_${BUILD_NUMBER}" - COMMIT_SHA = "${GIT_COMMIT}" - SHORT_SHA = "${GIT_COMMIT.take(7)}" } stages { + stage('Set Build Variables') { + steps { + script { + def relevantCommitHash + if (env.CHANGE_BRANCH) { + def commitExistsOnRemote = sh(script: "git branch --remotes --contains ${GIT_COMMIT}", returnStdout: true).trim() + if (commitExistsOnRemote) { + echo "PR build detected, but commit exists on remote. Using ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } else { + def parentCommits = sh(script: 'git rev-parse HEAD^@', returnStdout: true).trim().split('\n') + if (parentCommits.size() >= 2) { + echo "PR build detected. Jenkins checked out a merge commit: ${GIT_COMMIT} (parents: ${parentCommits.join(', ')})" + relevantCommitHash = parentCommits[0] + echo "Using first parent (branch commit): ${relevantCommitHash}" + } else { + echo "WARN: PR build detected, but ${GIT_COMMIT} is neither a merge commit, nor does it exist on the remote." + relevantCommitHash = "${GIT_COMMIT}" + } + } + } else { + echo "Branch build detected. Using commit: ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } + env.COMMIT_SHA = relevantCommitHash + env.SHORT_SHA = relevantCommitHash.take(7) + env.BUILD_NUMBER = "${env.SHORT_SHA}_${env.JENKINS_BUILD_NUMBER}" + } + } + } stage('Stage 1') { parallel { stage('Install monorepo') { diff --git a/libraries/redis-wrapper/Jenkinsfile b/libraries/redis-wrapper/Jenkinsfile index f136b65606..8d5f4b22f8 100644 --- a/libraries/redis-wrapper/Jenkinsfile +++ b/libraries/redis-wrapper/Jenkinsfile @@ -14,11 +14,38 @@ pipeline { environment { BRANCH_NAME = "${env.CHANGE_BRANCH ? env.CHANGE_BRANCH : env.BRANCH_NAME}" JENKINS_BUILD_NUMBER = "${BUILD_NUMBER}" - BUILD_NUMBER = "${SHORT_SHA}_${BUILD_NUMBER}" - COMMIT_SHA = "${GIT_COMMIT}" - SHORT_SHA = "${GIT_COMMIT.take(7)}" } stages { + stage('Set Build Variables') { + steps { + script { + def relevantCommitHash + if (env.CHANGE_BRANCH) { + def commitExistsOnRemote = sh(script: "git branch --remotes --contains ${GIT_COMMIT}", returnStdout: true).trim() + if (commitExistsOnRemote) { + echo "PR build detected, but commit exists on remote. Using ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } else { + def parentCommits = sh(script: 'git rev-parse HEAD^@', returnStdout: true).trim().split('\n') + if (parentCommits.size() >= 2) { + echo "PR build detected. Jenkins checked out a merge commit: ${GIT_COMMIT} (parents: ${parentCommits.join(', ')})" + relevantCommitHash = parentCommits[0] + echo "Using first parent (branch commit): ${relevantCommitHash}" + } else { + echo "WARN: PR build detected, but ${GIT_COMMIT} is neither a merge commit, nor does it exist on the remote." + relevantCommitHash = "${GIT_COMMIT}" + } + } + } else { + echo "Branch build detected. Using commit: ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } + env.COMMIT_SHA = relevantCommitHash + env.SHORT_SHA = relevantCommitHash.take(7) + env.BUILD_NUMBER = "${env.SHORT_SHA}_${env.JENKINS_BUILD_NUMBER}" + } + } + } stage('Stage 1') { parallel { stage('Install monorepo') { diff --git a/libraries/settings/Jenkinsfile b/libraries/settings/Jenkinsfile index 042c0c6c18..24a618f98c 100644 --- a/libraries/settings/Jenkinsfile +++ b/libraries/settings/Jenkinsfile @@ -14,11 +14,38 @@ pipeline { environment { BRANCH_NAME = "${env.CHANGE_BRANCH ? env.CHANGE_BRANCH : env.BRANCH_NAME}" JENKINS_BUILD_NUMBER = "${BUILD_NUMBER}" - BUILD_NUMBER = "${SHORT_SHA}_${BUILD_NUMBER}" - COMMIT_SHA = "${GIT_COMMIT}" - SHORT_SHA = "${GIT_COMMIT.take(7)}" } stages { + stage('Set Build Variables') { + steps { + script { + def relevantCommitHash + if (env.CHANGE_BRANCH) { + def commitExistsOnRemote = sh(script: "git branch --remotes --contains ${GIT_COMMIT}", returnStdout: true).trim() + if (commitExistsOnRemote) { + echo "PR build detected, but commit exists on remote. Using ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } else { + def parentCommits = sh(script: 'git rev-parse HEAD^@', returnStdout: true).trim().split('\n') + if (parentCommits.size() >= 2) { + echo "PR build detected. Jenkins checked out a merge commit: ${GIT_COMMIT} (parents: ${parentCommits.join(', ')})" + relevantCommitHash = parentCommits[0] + echo "Using first parent (branch commit): ${relevantCommitHash}" + } else { + echo "WARN: PR build detected, but ${GIT_COMMIT} is neither a merge commit, nor does it exist on the remote." + relevantCommitHash = "${GIT_COMMIT}" + } + } + } else { + echo "Branch build detected. Using commit: ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } + env.COMMIT_SHA = relevantCommitHash + env.SHORT_SHA = relevantCommitHash.take(7) + env.BUILD_NUMBER = "${env.SHORT_SHA}_${env.JENKINS_BUILD_NUMBER}" + } + } + } stage('Stage 1') { parallel { stage('Install monorepo') { diff --git a/libraries/stream-utils/Jenkinsfile b/libraries/stream-utils/Jenkinsfile index 69d6fabf32..cfcb541b47 100644 --- a/libraries/stream-utils/Jenkinsfile +++ b/libraries/stream-utils/Jenkinsfile @@ -14,11 +14,38 @@ pipeline { environment { BRANCH_NAME = "${env.CHANGE_BRANCH ? env.CHANGE_BRANCH : env.BRANCH_NAME}" JENKINS_BUILD_NUMBER = "${BUILD_NUMBER}" - BUILD_NUMBER = "${SHORT_SHA}_${BUILD_NUMBER}" - COMMIT_SHA = "${GIT_COMMIT}" - SHORT_SHA = "${GIT_COMMIT.take(7)}" } stages { + stage('Set Build Variables') { + steps { + script { + def relevantCommitHash + if (env.CHANGE_BRANCH) { + def commitExistsOnRemote = sh(script: "git branch --remotes --contains ${GIT_COMMIT}", returnStdout: true).trim() + if (commitExistsOnRemote) { + echo "PR build detected, but commit exists on remote. Using ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } else { + def parentCommits = sh(script: 'git rev-parse HEAD^@', returnStdout: true).trim().split('\n') + if (parentCommits.size() >= 2) { + echo "PR build detected. Jenkins checked out a merge commit: ${GIT_COMMIT} (parents: ${parentCommits.join(', ')})" + relevantCommitHash = parentCommits[0] + echo "Using first parent (branch commit): ${relevantCommitHash}" + } else { + echo "WARN: PR build detected, but ${GIT_COMMIT} is neither a merge commit, nor does it exist on the remote." + relevantCommitHash = "${GIT_COMMIT}" + } + } + } else { + echo "Branch build detected. Using commit: ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } + env.COMMIT_SHA = relevantCommitHash + env.SHORT_SHA = relevantCommitHash.take(7) + env.BUILD_NUMBER = "${env.SHORT_SHA}_${env.JENKINS_BUILD_NUMBER}" + } + } + } stage('Stage 1') { parallel { stage('Install monorepo') { diff --git a/libraries/validation-tools/Jenkinsfile b/libraries/validation-tools/Jenkinsfile index f19d14dd35..58a90e86e0 100644 --- a/libraries/validation-tools/Jenkinsfile +++ b/libraries/validation-tools/Jenkinsfile @@ -14,11 +14,38 @@ pipeline { environment { BRANCH_NAME = "${env.CHANGE_BRANCH ? env.CHANGE_BRANCH : env.BRANCH_NAME}" JENKINS_BUILD_NUMBER = "${BUILD_NUMBER}" - BUILD_NUMBER = "${SHORT_SHA}_${BUILD_NUMBER}" - COMMIT_SHA = "${GIT_COMMIT}" - SHORT_SHA = "${GIT_COMMIT.take(7)}" } stages { + stage('Set Build Variables') { + steps { + script { + def relevantCommitHash + if (env.CHANGE_BRANCH) { + def commitExistsOnRemote = sh(script: "git branch --remotes --contains ${GIT_COMMIT}", returnStdout: true).trim() + if (commitExistsOnRemote) { + echo "PR build detected, but commit exists on remote. Using ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } else { + def parentCommits = sh(script: 'git rev-parse HEAD^@', returnStdout: true).trim().split('\n') + if (parentCommits.size() >= 2) { + echo "PR build detected. Jenkins checked out a merge commit: ${GIT_COMMIT} (parents: ${parentCommits.join(', ')})" + relevantCommitHash = parentCommits[0] + echo "Using first parent (branch commit): ${relevantCommitHash}" + } else { + echo "WARN: PR build detected, but ${GIT_COMMIT} is neither a merge commit, nor does it exist on the remote." + relevantCommitHash = "${GIT_COMMIT}" + } + } + } else { + echo "Branch build detected. Using commit: ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } + env.COMMIT_SHA = relevantCommitHash + env.SHORT_SHA = relevantCommitHash.take(7) + env.BUILD_NUMBER = "${env.SHORT_SHA}_${env.JENKINS_BUILD_NUMBER}" + } + } + } stage('Stage 1') { parallel { stage('Install monorepo') { diff --git a/services/chat/Jenkinsfile b/services/chat/Jenkinsfile index 3c9acb00ab..8f79c250b9 100644 --- a/services/chat/Jenkinsfile +++ b/services/chat/Jenkinsfile @@ -14,12 +14,39 @@ pipeline { environment { BRANCH_NAME = "${env.CHANGE_BRANCH ? env.CHANGE_BRANCH : env.BRANCH_NAME}" JENKINS_BUILD_NUMBER = "${BUILD_NUMBER}" - BUILD_NUMBER = "${SHORT_SHA}_${BUILD_NUMBER}" DOCKER_COMPOSE_FLAGS = '-f docker-compose.ci.yml' - COMMIT_SHA = "${GIT_COMMIT}" - SHORT_SHA = "${GIT_COMMIT.take(7)}" } stages { + stage('Set Build Variables') { + steps { + script { + def relevantCommitHash + if (env.CHANGE_BRANCH) { + def commitExistsOnRemote = sh(script: "git branch --remotes --contains ${GIT_COMMIT}", returnStdout: true).trim() + if (commitExistsOnRemote) { + echo "PR build detected, but commit exists on remote. Using ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } else { + def parentCommits = sh(script: 'git rev-parse HEAD^@', returnStdout: true).trim().split('\n') + if (parentCommits.size() >= 2) { + echo "PR build detected. Jenkins checked out a merge commit: ${GIT_COMMIT} (parents: ${parentCommits.join(', ')})" + relevantCommitHash = parentCommits[0] + echo "Using first parent (branch commit): ${relevantCommitHash}" + } else { + echo "WARN: PR build detected, but ${GIT_COMMIT} is neither a merge commit, nor does it exist on the remote." + relevantCommitHash = "${GIT_COMMIT}" + } + } + } else { + echo "Branch build detected. Using commit: ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } + env.COMMIT_SHA = relevantCommitHash + env.SHORT_SHA = relevantCommitHash.take(7) + env.BUILD_NUMBER = "${env.SHORT_SHA}_${env.JENKINS_BUILD_NUMBER}" + } + } + } stage('Stage 1') { parallel { stage('Build') { diff --git a/services/clsi/Jenkinsfile b/services/clsi/Jenkinsfile index f7ac73346b..b510a582bb 100644 --- a/services/clsi/Jenkinsfile +++ b/services/clsi/Jenkinsfile @@ -14,12 +14,39 @@ pipeline { environment { BRANCH_NAME = "${env.CHANGE_BRANCH ? env.CHANGE_BRANCH : env.BRANCH_NAME}" JENKINS_BUILD_NUMBER = "${BUILD_NUMBER}" - BUILD_NUMBER = "${SHORT_SHA}_${BUILD_NUMBER}" DOCKER_COMPOSE_FLAGS = '-f docker-compose.ci.yml' - COMMIT_SHA = "${GIT_COMMIT}" - SHORT_SHA = "${GIT_COMMIT.take(7)}" } stages { + stage('Set Build Variables') { + steps { + script { + def relevantCommitHash + if (env.CHANGE_BRANCH) { + def commitExistsOnRemote = sh(script: "git branch --remotes --contains ${GIT_COMMIT}", returnStdout: true).trim() + if (commitExistsOnRemote) { + echo "PR build detected, but commit exists on remote. Using ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } else { + def parentCommits = sh(script: 'git rev-parse HEAD^@', returnStdout: true).trim().split('\n') + if (parentCommits.size() >= 2) { + echo "PR build detected. Jenkins checked out a merge commit: ${GIT_COMMIT} (parents: ${parentCommits.join(', ')})" + relevantCommitHash = parentCommits[0] + echo "Using first parent (branch commit): ${relevantCommitHash}" + } else { + echo "WARN: PR build detected, but ${GIT_COMMIT} is neither a merge commit, nor does it exist on the remote." + relevantCommitHash = "${GIT_COMMIT}" + } + } + } else { + echo "Branch build detected. Using commit: ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } + env.COMMIT_SHA = relevantCommitHash + env.SHORT_SHA = relevantCommitHash.take(7) + env.BUILD_NUMBER = "${env.SHORT_SHA}_${env.JENKINS_BUILD_NUMBER}" + } + } + } stage('Stage 1') { parallel { stage('Build') { diff --git a/services/contacts/Jenkinsfile b/services/contacts/Jenkinsfile index cc2e0bbd9b..c7f89b5d15 100644 --- a/services/contacts/Jenkinsfile +++ b/services/contacts/Jenkinsfile @@ -14,12 +14,39 @@ pipeline { environment { BRANCH_NAME = "${env.CHANGE_BRANCH ? env.CHANGE_BRANCH : env.BRANCH_NAME}" JENKINS_BUILD_NUMBER = "${BUILD_NUMBER}" - BUILD_NUMBER = "${SHORT_SHA}_${BUILD_NUMBER}" DOCKER_COMPOSE_FLAGS = '-f docker-compose.ci.yml' - COMMIT_SHA = "${GIT_COMMIT}" - SHORT_SHA = "${GIT_COMMIT.take(7)}" } stages { + stage('Set Build Variables') { + steps { + script { + def relevantCommitHash + if (env.CHANGE_BRANCH) { + def commitExistsOnRemote = sh(script: "git branch --remotes --contains ${GIT_COMMIT}", returnStdout: true).trim() + if (commitExistsOnRemote) { + echo "PR build detected, but commit exists on remote. Using ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } else { + def parentCommits = sh(script: 'git rev-parse HEAD^@', returnStdout: true).trim().split('\n') + if (parentCommits.size() >= 2) { + echo "PR build detected. Jenkins checked out a merge commit: ${GIT_COMMIT} (parents: ${parentCommits.join(', ')})" + relevantCommitHash = parentCommits[0] + echo "Using first parent (branch commit): ${relevantCommitHash}" + } else { + echo "WARN: PR build detected, but ${GIT_COMMIT} is neither a merge commit, nor does it exist on the remote." + relevantCommitHash = "${GIT_COMMIT}" + } + } + } else { + echo "Branch build detected. Using commit: ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } + env.COMMIT_SHA = relevantCommitHash + env.SHORT_SHA = relevantCommitHash.take(7) + env.BUILD_NUMBER = "${env.SHORT_SHA}_${env.JENKINS_BUILD_NUMBER}" + } + } + } stage('Stage 1') { parallel { stage('Build') { diff --git a/services/docstore/Jenkinsfile b/services/docstore/Jenkinsfile index d65b9ea01c..37c678f777 100644 --- a/services/docstore/Jenkinsfile +++ b/services/docstore/Jenkinsfile @@ -14,12 +14,39 @@ pipeline { environment { BRANCH_NAME = "${env.CHANGE_BRANCH ? env.CHANGE_BRANCH : env.BRANCH_NAME}" JENKINS_BUILD_NUMBER = "${BUILD_NUMBER}" - BUILD_NUMBER = "${SHORT_SHA}_${BUILD_NUMBER}" DOCKER_COMPOSE_FLAGS = '-f docker-compose.ci.yml' - COMMIT_SHA = "${GIT_COMMIT}" - SHORT_SHA = "${GIT_COMMIT.take(7)}" } stages { + stage('Set Build Variables') { + steps { + script { + def relevantCommitHash + if (env.CHANGE_BRANCH) { + def commitExistsOnRemote = sh(script: "git branch --remotes --contains ${GIT_COMMIT}", returnStdout: true).trim() + if (commitExistsOnRemote) { + echo "PR build detected, but commit exists on remote. Using ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } else { + def parentCommits = sh(script: 'git rev-parse HEAD^@', returnStdout: true).trim().split('\n') + if (parentCommits.size() >= 2) { + echo "PR build detected. Jenkins checked out a merge commit: ${GIT_COMMIT} (parents: ${parentCommits.join(', ')})" + relevantCommitHash = parentCommits[0] + echo "Using first parent (branch commit): ${relevantCommitHash}" + } else { + echo "WARN: PR build detected, but ${GIT_COMMIT} is neither a merge commit, nor does it exist on the remote." + relevantCommitHash = "${GIT_COMMIT}" + } + } + } else { + echo "Branch build detected. Using commit: ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } + env.COMMIT_SHA = relevantCommitHash + env.SHORT_SHA = relevantCommitHash.take(7) + env.BUILD_NUMBER = "${env.SHORT_SHA}_${env.JENKINS_BUILD_NUMBER}" + } + } + } stage('Stage 1') { parallel { stage('Build') { diff --git a/services/document-updater/Jenkinsfile b/services/document-updater/Jenkinsfile index 4fc65a036a..d111d9e1ef 100644 --- a/services/document-updater/Jenkinsfile +++ b/services/document-updater/Jenkinsfile @@ -14,12 +14,39 @@ pipeline { environment { BRANCH_NAME = "${env.CHANGE_BRANCH ? env.CHANGE_BRANCH : env.BRANCH_NAME}" JENKINS_BUILD_NUMBER = "${BUILD_NUMBER}" - BUILD_NUMBER = "${SHORT_SHA}_${BUILD_NUMBER}" DOCKER_COMPOSE_FLAGS = '-f docker-compose.ci.yml' - COMMIT_SHA = "${GIT_COMMIT}" - SHORT_SHA = "${GIT_COMMIT.take(7)}" } stages { + stage('Set Build Variables') { + steps { + script { + def relevantCommitHash + if (env.CHANGE_BRANCH) { + def commitExistsOnRemote = sh(script: "git branch --remotes --contains ${GIT_COMMIT}", returnStdout: true).trim() + if (commitExistsOnRemote) { + echo "PR build detected, but commit exists on remote. Using ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } else { + def parentCommits = sh(script: 'git rev-parse HEAD^@', returnStdout: true).trim().split('\n') + if (parentCommits.size() >= 2) { + echo "PR build detected. Jenkins checked out a merge commit: ${GIT_COMMIT} (parents: ${parentCommits.join(', ')})" + relevantCommitHash = parentCommits[0] + echo "Using first parent (branch commit): ${relevantCommitHash}" + } else { + echo "WARN: PR build detected, but ${GIT_COMMIT} is neither a merge commit, nor does it exist on the remote." + relevantCommitHash = "${GIT_COMMIT}" + } + } + } else { + echo "Branch build detected. Using commit: ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } + env.COMMIT_SHA = relevantCommitHash + env.SHORT_SHA = relevantCommitHash.take(7) + env.BUILD_NUMBER = "${env.SHORT_SHA}_${env.JENKINS_BUILD_NUMBER}" + } + } + } stage('Stage 1') { parallel { stage('Build') { diff --git a/services/filestore/Jenkinsfile b/services/filestore/Jenkinsfile index 566164075c..4de0eeaf80 100644 --- a/services/filestore/Jenkinsfile +++ b/services/filestore/Jenkinsfile @@ -14,12 +14,39 @@ pipeline { environment { BRANCH_NAME = "${env.CHANGE_BRANCH ? env.CHANGE_BRANCH : env.BRANCH_NAME}" JENKINS_BUILD_NUMBER = "${BUILD_NUMBER}" - BUILD_NUMBER = "${SHORT_SHA}_${BUILD_NUMBER}" DOCKER_COMPOSE_FLAGS = '-f docker-compose.ci.yml' - COMMIT_SHA = "${GIT_COMMIT}" - SHORT_SHA = "${GIT_COMMIT.take(7)}" } stages { + stage('Set Build Variables') { + steps { + script { + def relevantCommitHash + if (env.CHANGE_BRANCH) { + def commitExistsOnRemote = sh(script: "git branch --remotes --contains ${GIT_COMMIT}", returnStdout: true).trim() + if (commitExistsOnRemote) { + echo "PR build detected, but commit exists on remote. Using ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } else { + def parentCommits = sh(script: 'git rev-parse HEAD^@', returnStdout: true).trim().split('\n') + if (parentCommits.size() >= 2) { + echo "PR build detected. Jenkins checked out a merge commit: ${GIT_COMMIT} (parents: ${parentCommits.join(', ')})" + relevantCommitHash = parentCommits[0] + echo "Using first parent (branch commit): ${relevantCommitHash}" + } else { + echo "WARN: PR build detected, but ${GIT_COMMIT} is neither a merge commit, nor does it exist on the remote." + relevantCommitHash = "${GIT_COMMIT}" + } + } + } else { + echo "Branch build detected. Using commit: ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } + env.COMMIT_SHA = relevantCommitHash + env.SHORT_SHA = relevantCommitHash.take(7) + env.BUILD_NUMBER = "${env.SHORT_SHA}_${env.JENKINS_BUILD_NUMBER}" + } + } + } stage('Stage 1') { parallel { stage('Build') { diff --git a/services/history-v1/Jenkinsfile b/services/history-v1/Jenkinsfile index f68389ea1c..c2f6587dd0 100644 --- a/services/history-v1/Jenkinsfile +++ b/services/history-v1/Jenkinsfile @@ -14,12 +14,39 @@ pipeline { environment { BRANCH_NAME = "${env.CHANGE_BRANCH ? env.CHANGE_BRANCH : env.BRANCH_NAME}" JENKINS_BUILD_NUMBER = "${BUILD_NUMBER}" - BUILD_NUMBER = "${SHORT_SHA}_${BUILD_NUMBER}" DOCKER_COMPOSE_FLAGS = '-f docker-compose.ci.yml' - COMMIT_SHA = "${GIT_COMMIT}" - SHORT_SHA = "${GIT_COMMIT.take(7)}" } stages { + stage('Set Build Variables') { + steps { + script { + def relevantCommitHash + if (env.CHANGE_BRANCH) { + def commitExistsOnRemote = sh(script: "git branch --remotes --contains ${GIT_COMMIT}", returnStdout: true).trim() + if (commitExistsOnRemote) { + echo "PR build detected, but commit exists on remote. Using ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } else { + def parentCommits = sh(script: 'git rev-parse HEAD^@', returnStdout: true).trim().split('\n') + if (parentCommits.size() >= 2) { + echo "PR build detected. Jenkins checked out a merge commit: ${GIT_COMMIT} (parents: ${parentCommits.join(', ')})" + relevantCommitHash = parentCommits[0] + echo "Using first parent (branch commit): ${relevantCommitHash}" + } else { + echo "WARN: PR build detected, but ${GIT_COMMIT} is neither a merge commit, nor does it exist on the remote." + relevantCommitHash = "${GIT_COMMIT}" + } + } + } else { + echo "Branch build detected. Using commit: ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } + env.COMMIT_SHA = relevantCommitHash + env.SHORT_SHA = relevantCommitHash.take(7) + env.BUILD_NUMBER = "${env.SHORT_SHA}_${env.JENKINS_BUILD_NUMBER}" + } + } + } stage('Stage 1') { parallel { stage('Build') { diff --git a/services/notifications/Jenkinsfile b/services/notifications/Jenkinsfile index 4b3c354243..f75c799bd4 100644 --- a/services/notifications/Jenkinsfile +++ b/services/notifications/Jenkinsfile @@ -14,12 +14,39 @@ pipeline { environment { BRANCH_NAME = "${env.CHANGE_BRANCH ? env.CHANGE_BRANCH : env.BRANCH_NAME}" JENKINS_BUILD_NUMBER = "${BUILD_NUMBER}" - BUILD_NUMBER = "${SHORT_SHA}_${BUILD_NUMBER}" DOCKER_COMPOSE_FLAGS = '-f docker-compose.ci.yml' - COMMIT_SHA = "${GIT_COMMIT}" - SHORT_SHA = "${GIT_COMMIT.take(7)}" } stages { + stage('Set Build Variables') { + steps { + script { + def relevantCommitHash + if (env.CHANGE_BRANCH) { + def commitExistsOnRemote = sh(script: "git branch --remotes --contains ${GIT_COMMIT}", returnStdout: true).trim() + if (commitExistsOnRemote) { + echo "PR build detected, but commit exists on remote. Using ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } else { + def parentCommits = sh(script: 'git rev-parse HEAD^@', returnStdout: true).trim().split('\n') + if (parentCommits.size() >= 2) { + echo "PR build detected. Jenkins checked out a merge commit: ${GIT_COMMIT} (parents: ${parentCommits.join(', ')})" + relevantCommitHash = parentCommits[0] + echo "Using first parent (branch commit): ${relevantCommitHash}" + } else { + echo "WARN: PR build detected, but ${GIT_COMMIT} is neither a merge commit, nor does it exist on the remote." + relevantCommitHash = "${GIT_COMMIT}" + } + } + } else { + echo "Branch build detected. Using commit: ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } + env.COMMIT_SHA = relevantCommitHash + env.SHORT_SHA = relevantCommitHash.take(7) + env.BUILD_NUMBER = "${env.SHORT_SHA}_${env.JENKINS_BUILD_NUMBER}" + } + } + } stage('Stage 1') { parallel { stage('Build') { diff --git a/services/project-history/Jenkinsfile b/services/project-history/Jenkinsfile index 0f701650cd..cbc41b2b02 100644 --- a/services/project-history/Jenkinsfile +++ b/services/project-history/Jenkinsfile @@ -14,12 +14,39 @@ pipeline { environment { BRANCH_NAME = "${env.CHANGE_BRANCH ? env.CHANGE_BRANCH : env.BRANCH_NAME}" JENKINS_BUILD_NUMBER = "${BUILD_NUMBER}" - BUILD_NUMBER = "${SHORT_SHA}_${BUILD_NUMBER}" DOCKER_COMPOSE_FLAGS = '-f docker-compose.ci.yml' - COMMIT_SHA = "${GIT_COMMIT}" - SHORT_SHA = "${GIT_COMMIT.take(7)}" } stages { + stage('Set Build Variables') { + steps { + script { + def relevantCommitHash + if (env.CHANGE_BRANCH) { + def commitExistsOnRemote = sh(script: "git branch --remotes --contains ${GIT_COMMIT}", returnStdout: true).trim() + if (commitExistsOnRemote) { + echo "PR build detected, but commit exists on remote. Using ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } else { + def parentCommits = sh(script: 'git rev-parse HEAD^@', returnStdout: true).trim().split('\n') + if (parentCommits.size() >= 2) { + echo "PR build detected. Jenkins checked out a merge commit: ${GIT_COMMIT} (parents: ${parentCommits.join(', ')})" + relevantCommitHash = parentCommits[0] + echo "Using first parent (branch commit): ${relevantCommitHash}" + } else { + echo "WARN: PR build detected, but ${GIT_COMMIT} is neither a merge commit, nor does it exist on the remote." + relevantCommitHash = "${GIT_COMMIT}" + } + } + } else { + echo "Branch build detected. Using commit: ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } + env.COMMIT_SHA = relevantCommitHash + env.SHORT_SHA = relevantCommitHash.take(7) + env.BUILD_NUMBER = "${env.SHORT_SHA}_${env.JENKINS_BUILD_NUMBER}" + } + } + } stage('Stage 1') { parallel { stage('Build') { diff --git a/services/real-time/Jenkinsfile b/services/real-time/Jenkinsfile index 7e8c3f5b16..33b0b41e29 100644 --- a/services/real-time/Jenkinsfile +++ b/services/real-time/Jenkinsfile @@ -14,12 +14,39 @@ pipeline { environment { BRANCH_NAME = "${env.CHANGE_BRANCH ? env.CHANGE_BRANCH : env.BRANCH_NAME}" JENKINS_BUILD_NUMBER = "${BUILD_NUMBER}" - BUILD_NUMBER = "${SHORT_SHA}_${BUILD_NUMBER}" DOCKER_COMPOSE_FLAGS = '-f docker-compose.ci.yml' - COMMIT_SHA = "${GIT_COMMIT}" - SHORT_SHA = "${GIT_COMMIT.take(7)}" } stages { + stage('Set Build Variables') { + steps { + script { + def relevantCommitHash + if (env.CHANGE_BRANCH) { + def commitExistsOnRemote = sh(script: "git branch --remotes --contains ${GIT_COMMIT}", returnStdout: true).trim() + if (commitExistsOnRemote) { + echo "PR build detected, but commit exists on remote. Using ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } else { + def parentCommits = sh(script: 'git rev-parse HEAD^@', returnStdout: true).trim().split('\n') + if (parentCommits.size() >= 2) { + echo "PR build detected. Jenkins checked out a merge commit: ${GIT_COMMIT} (parents: ${parentCommits.join(', ')})" + relevantCommitHash = parentCommits[0] + echo "Using first parent (branch commit): ${relevantCommitHash}" + } else { + echo "WARN: PR build detected, but ${GIT_COMMIT} is neither a merge commit, nor does it exist on the remote." + relevantCommitHash = "${GIT_COMMIT}" + } + } + } else { + echo "Branch build detected. Using commit: ${GIT_COMMIT}" + relevantCommitHash = "${GIT_COMMIT}" + } + env.COMMIT_SHA = relevantCommitHash + env.SHORT_SHA = relevantCommitHash.take(7) + env.BUILD_NUMBER = "${env.SHORT_SHA}_${env.JENKINS_BUILD_NUMBER}" + } + } + } stage('Stage 1') { parallel { stage('Build') {