0
0
Jenkinsdevops~10 mins

Branch-specific pipeline behavior in Jenkins - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to specify the branch name in the Jenkins pipeline.

Jenkins
pipeline {
    agent any
    stages {
        stage('Build') {
            when {
                branch [1]
            }
            steps {
                echo 'Building branch'
            }
        }
    }
}
Drag options to blanks, or click blank then click option'
A'feature'
B'develop'
C'main'
D'release'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to use quotes around the branch name
Using the wrong branch name
2fill in blank
medium

Complete the code to run the stage only on the 'develop' branch.

Jenkins
stage('Test') {
    when {
        branch [1]
    }
    steps {
        echo 'Running tests on develop branch'
    }
}
Drag options to blanks, or click blank then click option'
A'develop'
B'main'
C'feature'
D'hotfix'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong branch name
Omitting quotes around the branch name
3fill in blank
hard

Fix the error in the branch condition to correctly check for the 'feature' branch.

Jenkins
when {
    branch [1]
}
Drag options to blanks, or click blank then click option'
A'feature'
Bfeature
C"feature"
Dbranch == 'feature'
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes around the branch name
Using an expression instead of a string
4fill in blank
hard

Fill both blanks to run the stage only on branches starting with 'release' and skip others.

Jenkins
stage('Deploy') {
    when {
        expression {
            return env.BRANCH_NAME.[1]('release') [2] true
        }
    }
    steps {
        echo 'Deploying release branch'
    }
}
Drag options to blanks, or click blank then click option'
AstartsWith
B==
C!=
Dcontains
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'contains' instead of 'startsWith'
Not comparing the result to true
5fill in blank
hard

Fill all three blanks to create a pipeline that runs different stages for 'main', 'develop', and other branches.

Jenkins
pipeline {
    agent any
    stages {
        stage('Build') {
            when {
                branch [1]
            }
            steps {
                echo 'Building main branch'
            }
        }
        stage('Test') {
            when {
                branch [2]
            }
            steps {
                echo 'Testing develop branch'
            }
        }
        stage('Deploy') {
            when {
                not {
                    branch [3]
                }
            }
            steps {
                echo 'Deploying other branches'
            }
        }
    }
}
Drag options to blanks, or click blank then click option'
A'main'
B'develop'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up branch names in stages
Not using quotes around branch names