Parcourir la source

Merge pull request #29537 from overleaf/gs-jenkins-pr-build-tags-templates

Tag images with branch head instead of merge commit (templates)

GitOrigin-RevId: 53e616aeec058863e25b356841a59f3ebe13c87f
Gernot Schulz il y a 11 mois
Parent
commit
b834b8bcb7

+ 30 - 3
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') {

+ 30 - 3
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') {

+ 30 - 3
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') {

+ 30 - 3
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') {

+ 30 - 3
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') {

+ 30 - 3
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') {

+ 30 - 3
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') {

+ 30 - 3
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') {

+ 30 - 3
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') {

+ 30 - 3
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') {

+ 30 - 3
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') {

+ 30 - 3
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') {

+ 30 - 3
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') {

+ 30 - 3
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') {

+ 30 - 3
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') {

+ 30 - 3
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') {

+ 30 - 3
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') {

+ 30 - 3
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') {

+ 30 - 3
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') {

+ 30 - 3
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') {

+ 30 - 3
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') {

+ 30 - 3
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') {

+ 30 - 3
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') {

+ 30 - 3
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') {