Skip to content

Instantly share code, notes, and snippets.

@initcron
Created October 15, 2024 07:26
Show Gist options
  • Save initcron/431d6aa5da627ab36a39c96710b3320d to your computer and use it in GitHub Desktop.
Save initcron/431d6aa5da627ab36a39c96710b3320d to your computer and use it in GitHub Desktop.

Approach to Avoid Redundant Jenkins Builds

  1. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment