From bed63fd954c4698768c455034630ca1df2ff6580 Mon Sep 17 00:00:00 2001 From: Gernot Schulz Date: Fri, 7 Nov 2025 11:26:54 +0100 Subject: [PATCH] Merge pull request #29586 from overleaf/gs-jenkins-pr-build-tags-git-br Include commit look-up stage in git-bridge Jenkinsfile GitOrigin-RevId: 993cabcafd057dc6767a2780c2dd11708482d536 --- services/git-bridge/Jenkinsfile | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/services/git-bridge/Jenkinsfile b/services/git-bridge/Jenkinsfile index c8fa9ff7c5..6bd19360a3 100644 --- a/services/git-bridge/Jenkinsfile +++ b/services/git-bridge/Jenkinsfile @@ -12,11 +12,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('Build') {