Approach to Avoid Redundant Jenkins Builds
- Use GitHub Merge Commit Validation When PR1 is merged into the base branch, instead of manually merging it into PR2, you can let Jenkins handle it through merge commit validation. This ensures that Jenkins checks are only triggered if the combination of PR2 + base branch requires validation. Solution:
Configure Jenkins to trigger builds only on the merge of the PR branch with the latest base branch. Example: PR2 will be validated only for changes introduced in its branch + any new changes from the main branch after PR1's merge. Jenkinsfile Example:
pipeline {
agent any
triggers {
pollSCM('* * * * *') // Optional, triggers on merge or new commits
}
stages {
stage('Check Changes') {
steps {
script {
def diff = sh(returnStdout: true, script: "git diff --name-only origin/main").trim()
if (!diff) {
currentBuild.result = 'SUCCESS'
echo "No relevant changes detected, skipping tests."
return
}
}
}
}
stage('Run Tests') {
when {
not { equals expected: 'SUCCESS', actual: currentBuild.result }
}
steps {
echo "Running tests as relevant changes are found."
// Add your test commands here
}
}
}
}
How it works:
This script checks for file changes between the current PR branch and the latest base branch. If there are no relevant changes, the build is marked as successful without running the tests.