0
0
Jenkinsdevops~20 mins

Conditional deployment logic in Jenkins - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Conditional Deployment Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this Jenkins pipeline snippet?

Consider this Jenkins pipeline script snippet that decides deployment based on branch name:

pipeline {
  agent any
  stages {
    stage('Deploy') {
      when {
        expression { env.BRANCH_NAME == 'main' }
      }
      steps {
        echo 'Deploying to production!'
      }
    }
  }
}

If the pipeline runs on branch feature-xyz, what will be the output?

Jenkins
pipeline {
  agent any
  stages {
    stage('Deploy') {
      when {
        expression { env.BRANCH_NAME == 'main' }
      }
      steps {
        echo 'Deploying to production!'
      }
    }
  }
}
AThe pipeline prints 'Deploying to production!'
BThe pipeline runs the 'Deploy' stage but prints an empty line
CThe pipeline fails with a syntax error
DThe pipeline skips the 'Deploy' stage and prints nothing
Attempts:
2 left
💡 Hint

Think about the when condition and the branch name.

Configuration
intermediate
2:00remaining
Which Jenkinsfile snippet deploys only on tags starting with 'v'?

You want to deploy your application only when a Git tag starts with the letter 'v' (e.g., 'v1.0'). Which when condition snippet achieves this?

Awhen { tag pattern: '^v.*', comparator: 'REGEXP' }
Bwhen { branch 'v*' }
Cwhen { expression { env.TAG_NAME.startsWith('v') } }
Dwhen { tag 'v*' }
Attempts:
2 left
💡 Hint

Tags can be matched with regex patterns in Jenkins when conditions.

🔀 Workflow
advanced
2:30remaining
Identify the correct conditional deployment flow in a Jenkins pipeline

Given a Jenkins pipeline with stages for build, test, and deploy, you want to deploy only if tests pass and the branch is 'release'. Which snippet correctly implements this logic?

A
stage('Deploy') {
  when {
    expression { currentBuild.result == 'SUCCESS' && env.BRANCH_NAME == 'release' }
  }
  steps {
    echo 'Deploying release'
  }
}
B
stage('Deploy') {
  when {
    anyOf {
      expression { currentBuild.result == 'SUCCESS' }
      branch 'release'
    }
  }
  steps {
    echo 'Deploying release'
  }
}
C
stage('Deploy') {
  when {
    not {
      expression { currentBuild.result != 'SUCCESS' }
    }
    branch 'release'
  }
  steps {
    echo 'Deploying release'
  }
}
D
stage('Deploy') {
  when {
    allOf {
      expression { currentBuild.result == 'SUCCESS' }
      branch 'release'
    }
  }
  steps {
    echo 'Deploying release'
  }
}
Attempts:
2 left
💡 Hint

Use a single expression combining both conditions for clarity.

Troubleshoot
advanced
2:00remaining
Why does this Jenkins pipeline always skip the deploy stage?

Look at this Jenkins pipeline snippet:

pipeline {
  agent any
  stages {
    stage('Deploy') {
      when {
        branch 'main'
      }
      steps {
        echo 'Deploying to production'
      }
    }
  }
}

Even when running on the 'main' branch, the 'Deploy' stage is skipped. What is the most likely cause?

Jenkins
pipeline {
  agent any
  stages {
    stage('Deploy') {
      when {
        branch 'main'
      }
      steps {
        echo 'Deploying to production'
      }
    }
  }
}
AThe pipeline agent is not compatible with the <code>branch</code> condition
BThe syntax for <code>branch</code> condition is incorrect; it needs to be <code>branch name: 'main'</code>
CThe <code>branch</code> condition requires the environment variable <code>BRANCH_NAME</code> to be set, which is missing
DThe <code>when</code> block must be inside <code>steps</code> to work
Attempts:
2 left
💡 Hint

Check if the pipeline environment has the branch name variable set.

Best Practice
expert
3:00remaining
Which approach best ensures safe conditional deployment in Jenkins pipelines?

To avoid accidental deployments, you want to deploy only when all tests pass, the branch is 'main', and a manual approval is given. Which Jenkins pipeline snippet best implements this?

A
stage('Deploy') {
  when {
    allOf {
      expression { currentBuild.result == 'SUCCESS' }
      branch 'main'
      expression { input message: 'Approve deployment?' }
    }
  }
  steps {
    echo 'Deploying to production'
  }
}
B
stage('Deploy') {
  when {
    expression { currentBuild.result == 'SUCCESS' &amp;&amp; env.BRANCH_NAME == 'main' }
  }
  steps {
    input 'Approve deployment?'
    echo 'Deploying to production'
  }
}
C
stage('Deploy') {
  steps {
    input 'Approve deployment?'
    when {
      expression { currentBuild.result == 'SUCCESS' &amp;&amp; env.BRANCH_NAME == 'main' }
    }
    echo 'Deploying to production'
  }
}
D
stage('Deploy') {
  when {
    expression { currentBuild.result == 'SUCCESS' &amp;&amp; env.BRANCH_NAME == 'main' }
  }
  steps {
    echo 'Deploying to production'
  }
  post {
    always {
      input 'Approve deployment?'
    }
  }
}
Attempts:
2 left
💡 Hint

Manual approval should be a step, not part of the when condition.