Bird
Raised Fist0
Jenkinsdevops~20 mins

Conditional deployment logic in Jenkins - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using a when block in a Jenkins pipeline?
easy
A. To define environment variables for the pipeline
B. To run a stage only if certain conditions are met
C. To specify the agent where the pipeline runs
D. To declare pipeline parameters

Solution

  1. Step 1: Understand the role of when in Jenkins

    The when block controls whether a stage runs based on conditions.
  2. Step 2: Compare options with when usage

    Only To run a stage only if certain conditions are met describes conditional execution, which matches when functionality.
  3. Final Answer:

    To run a stage only if certain conditions are met -> Option B
  4. Quick Check:

    when controls stage execution = A [OK]
Hint: Remember: when means "only if" for stages [OK]
Common Mistakes:
  • Confusing when with environment setup
  • Thinking when sets agents
  • Assuming when declares parameters
2. Which of the following is the correct syntax to run a stage only on the 'main' branch in a declarative Jenkins pipeline?
easy
A. when { branch is 'main' }
B. when { branch == 'main' }
C. when { branch 'main' }
D. when { branch equals 'main' }

Solution

  1. Step 1: Recall correct when syntax for branch condition

    The correct syntax uses branch 'main' inside when block.
  2. Step 2: Check each option's syntax

    Only when { branch 'main' } matches Jenkins declarative syntax; others use invalid expressions.
  3. Final Answer:

    when { branch 'main' } -> Option C
  4. Quick Check:

    Correct branch syntax = C [OK]
Hint: Use branch 'name' exactly inside when [OK]
Common Mistakes:
  • Using comparison operators inside when
  • Writing invalid expressions like 'branch =='
  • Confusing Groovy syntax with declarative syntax
3. Given this Jenkins pipeline snippet, what will happen if the branch is 'develop'?
stage('Deploy') {
  when {
    branch 'main'
  }
  steps {
    echo 'Deploying to production'
  }
}
medium
A. The stage will be skipped and nothing prints
B. The stage will run and print 'Deploying to production'
C. The pipeline will fail with a syntax error
D. The stage will run but print nothing

Solution

  1. Step 1: Analyze the when condition

    The stage runs only if branch is 'main'. Here branch is 'develop', so condition fails.
  2. Step 2: Determine stage behavior on condition failure

    When when condition fails, the stage is skipped and no steps run.
  3. Final Answer:

    The stage will be skipped and nothing prints -> Option A
  4. Quick Check:

    Branch mismatch skips stage = A [OK]
Hint: If branch doesn't match when, stage skips silently [OK]
Common Mistakes:
  • Assuming stage runs anyway
  • Expecting error on branch mismatch
  • Thinking stage runs but prints nothing
4. Identify the error in this Jenkins pipeline snippet that tries to deploy only if environment variable DEPLOY is 'true':
stage('Deploy') {
  when {
    environment DEPLOY == 'true'
  }
  steps {
    echo 'Deploying application'
  }
}
medium
A. The stage name cannot be 'Deploy'
B. The when block must be inside steps
C. The echo command is not allowed in steps
D. The environment condition is invalid syntax

Solution

  1. Step 1: Check when syntax for environment variable

    The correct syntax uses environment name: 'DEPLOY', value: 'true' or expression blocks, not direct comparison.
  2. Step 2: Identify invalid usage

    Using environment DEPLOY == 'true' is invalid and causes syntax error.
  3. Final Answer:

    The environment condition is invalid syntax -> Option D
  4. Quick Check:

    Invalid environment condition syntax = D [OK]
Hint: Use correct when { environment name: 'VAR', value: 'val' } syntax [OK]
Common Mistakes:
  • Writing direct comparisons inside when
  • Placing when inside steps
  • Misusing stage names
5. You want to deploy only if the branch is 'release' and the environment variable DEPLOY is 'yes'. Which when block correctly implements this in a declarative Jenkins pipeline?
hard
A. when { allOf { branch 'release'; environment name: 'DEPLOY', value: 'yes' } }
B. when { branch 'release' && environment DEPLOY == 'yes' }
C. when { branch 'release' environment DEPLOY == 'yes' }
D. when { anyOf { branch 'release'; environment name: 'DEPLOY', value: 'yes' } }

Solution

  1. Step 1: Understand combining conditions in when

    Use allOf to require both conditions true simultaneously.
  2. Step 2: Check syntax for branch and environment conditions

    when { allOf { branch 'release'; environment name: 'DEPLOY', value: 'yes' } } correctly uses allOf with branch 'release' and environment name: 'DEPLOY', value: 'yes'.
  3. Step 3: Identify errors in other options

    Options A and C use invalid syntax; D uses anyOf which means either condition, not both.
  4. Final Answer:

    when { allOf { branch 'release'; environment name: 'DEPLOY', value: 'yes' } } -> Option A
  5. Quick Check:

    Use allOf for AND conditions = B [OK]
Hint: Use allOf to combine multiple when conditions [OK]
Common Mistakes:
  • Using anyOf instead of allOf
  • Writing invalid combined expressions
  • Misusing environment condition syntax
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard