yalegria / devops-git-actions

0 stars 0 forks source link

Conditional Events That Can Trigger a Jenkins Build #23

Open yuriaru opened 2 years ago

Voxelghiest commented 2 years ago

A Jenkins pipeline is broken up into various stages. Each stage can be triggered conditionally using the when {} directive and listing some conditions. For example, to trigger a stage only on the main branch, the pipeline would look something like this:

pipeline {
    agent any
    stages {
        stage('Build') {
            when {
                branch 'main'
            }
            steps {
                echo 'Building project'
            }
        }
    }
}

Multiple conditions can be added by creating new lines inside the when {} block. All conditions must evaluate to true for the stage to run. A few further possible conditions include:

There are many others beyond this. The Jenkins Pipeline Syntax Docs are a good resource for more detailed info.