Preventing builds from getting triggered when creating a new multibranch Pipeline or Organization Folder

Article ID:360026953651
1 minute readKnowledge base

Issue

  • When a new GitHub Organization job is created, all repos are scanned and builds are triggered right away. Is there any way to prevent these automatic builds?

  • I want to control whether a stage should be executed or not based on the triggered events.

Resolution

The best way is to install Branch API 2.3.0 and Basic Branch Build Strategies 1.3.0. A new option is added to skip initial build on branch indexing:

How to prevent builds from getting triggered from all branches 2

If you can’t install the plugins for some reason, go to the GitHub Org configuration, clear the field Branch names to build automatically.

How to prevent builds from getting triggered from all branches

After saving the job, you can add .* to re-enable builds.

Workaround

You can also control when a stage should be executed. In the following example, the deployment stage is skipped unless the build is triggered by a GitHub push event.

pipeline {
    agent none
    stages {
        stage('Example Build') {
            steps {
                echo 'Hello World'
            }
        }
        stage('Example Deploy') {
            when {
                expression {
                    currentBuild.buildCauses.toString().contains("Push event to branch")
                }
            }
            steps {
                echo 'Deploying'
            }
        }
    }
}

Tested product/plugin versions