Bird
Raised Fist0
Jenkinsdevops~20 mins

Stage block structure in Jenkins - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Stage Block Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:30remaining
What is the output of this Jenkins pipeline stage?

Consider this Jenkins pipeline snippet:

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

What will Jenkins print during the 'Build' stage execution?

Jenkins
pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        echo 'Building project'
      }
    }
  }
}
A
pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        echo 'Building project'
      }
    }
  }
}
BBuild
CBuilding project
DError: Missing agent declaration
Attempts:
2 left
💡 Hint

Look at the echo command inside the steps block.

❓ Configuration
intermediate
2:00remaining
Identify the correct stage block structure for parallel execution

Which Jenkins pipeline snippet correctly defines two parallel stages named 'Test1' and 'Test2' inside a 'Test' stage?

A
stage('Test') {
  parallel {
    stage('Test1') {
      steps { echo 'Running Test1' }
    }
    stage('Test2') {
      steps { echo 'Running Test2' }
    }
  }
}
B
stage('Test') {
  stages {
    parallel {
      stage('Test1') {
        steps { echo 'Running Test1' }
      }
      stage('Test2') {
        steps { echo 'Running Test2' }
      }
    }
  }
}
C
stage('Test') {
  steps {
    parallel {
      stage('Test1') {
        steps { echo 'Running Test1' }
      }
      stage('Test2') {
        steps { echo 'Running Test2' }
      }
    }
  }
}
D
stage('Test') {
  parallel {
    steps {
      stage('Test1') {
        echo 'Running Test1'
      }
      stage('Test2') {
        echo 'Running Test2'
      }
    }
  }
}
Attempts:
2 left
💡 Hint

Parallel stages are defined inside a parallel block directly under a stage.

❓ Troubleshoot
advanced
2:30remaining
Why does this Jenkins pipeline fail to run stages sequentially?

Given this Jenkins pipeline snippet:

pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        echo 'Building'
      }
    }
    parallel {
      stage('Test1') {
        steps {
          echo 'Testing 1'
        }
      }
      stage('Test2') {
        steps {
          echo 'Testing 2'
        }
      }
    }
  }
}

Why does Jenkins report a syntax error?

Jenkins
pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        echo 'Building'
      }
    }
    parallel {
      stage('Test1') {
        steps {
          echo 'Testing 1'
        }
      }
      stage('Test2') {
        steps {
          echo 'Testing 2'
        }
      }
    }
  }
}
ABecause 'agent' is missing inside 'parallel'
BBecause 'steps' block is missing inside 'parallel'
CBecause 'stage' blocks cannot be nested inside 'stages'
DBecause 'parallel' cannot be a direct child of 'stages'; it must be inside a 'stage' block
Attempts:
2 left
💡 Hint

Check the Jenkins pipeline syntax rules for where parallel can be placed.

🔀 Workflow
advanced
2:30remaining
What is the order of execution for these stages in a Jenkins pipeline?

Consider this Jenkins pipeline snippet:

pipeline {
  agent any
  stages {
    stage('Build') {
      steps { echo 'Build' }
    }
    stage('Test') {
      parallel {
        stage('Unit') {
          steps { echo 'Unit Test' }
        }
        stage('Integration') {
          steps { echo 'Integration Test' }
        }
      }
    }
    stage('Deploy') {
      steps { echo 'Deploy' }
    }
  }
}

What is the correct order of printed messages in the Jenkins console?

Jenkins
pipeline {
  agent any
  stages {
    stage('Build') {
      steps { echo 'Build' }
    }
    stage('Test') {
      parallel {
        stage('Unit') {
          steps { echo 'Unit Test' }
        }
        stage('Integration') {
          steps { echo 'Integration Test' }
        }
      }
    }
    stage('Deploy') {
      steps { echo 'Deploy' }
    }
  }
}
ABuild, Unit Test and Integration Test (in any order), Deploy
BUnit Test, Integration Test, Build, Deploy
CBuild, Deploy, Unit Test, Integration Test
DBuild, Unit Test, Deploy, Integration Test
Attempts:
2 left
💡 Hint

Remember that parallel stages run simultaneously after the previous stage completes.

✅ Best Practice
expert
3:00remaining
Which stage block structure ensures proper error handling and cleanup?

In Jenkins declarative pipelines, which stage block structure correctly uses post blocks to handle success and cleanup after a stage?

A
stage('Deploy') {
  post {
    success {
      echo 'Deployment succeeded'
    }
    always {
      echo 'Cleanup'
    }
  }
  steps {
    echo 'Deploying'
  }
}
B
stage('Deploy') {
  steps {
    echo 'Deploying'
  }
  post {
    success {
      echo 'Deployment succeeded'
    }
    always {
      echo 'Cleanup'
    }
  }
}
C
stage('Deploy') {
  steps {
    echo 'Deploying'
  }
  post {
    always {
      echo 'Cleanup'
    }
    success {
      echo 'Deployment succeeded'
    }
  }
}
D
stage('Deploy') {
  steps {
    echo 'Deploying'
  }
  post {
    failure {
      echo 'Deployment failed'
    }
  }
}
Attempts:
2 left
💡 Hint

The order of post conditions matters for readability and correctness.

Practice

(1/5)
1. What is the main purpose of a stage block in a Jenkins pipeline?
easy
A. To define environment variables for the pipeline
B. To divide the pipeline into named steps for better organization and tracking
C. To specify the agent where the pipeline runs
D. To declare global variables accessible in all stages

Solution

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

    The stage block is used to split the pipeline into named sections, making it easier to follow and debug.
  2. Step 2: Compare with other pipeline blocks

    Other blocks like environment or agent serve different purposes, not organizing steps.
  3. Final Answer:

    To divide the pipeline into named steps for better organization and tracking -> Option B
  4. Quick Check:

    Stage block purpose = organization [OK]
Hint: Stages name steps to track progress clearly [OK]
Common Mistakes:
  • Confusing stage with environment block
  • Thinking stage sets agents
  • Assuming stage declares variables
2. Which of the following is the correct syntax for defining a stage named Build with a simple shell command in Jenkins declarative pipeline?
easy
A. stage('Build') { step { sh 'make' } }
B. stage: 'Build' { steps { sh 'make' } }
C. stage('Build') { steps { sh 'make' } }
D. stage('Build') steps { sh 'make' }

Solution

  1. Step 1: Recall correct stage block syntax

    The correct syntax uses stage('Name') { steps { ... } } with curly braces and plural steps.
  2. Step 2: Identify syntax errors in other options

    The other options contain these errors: singular step instead of plural steps, colon : instead of parentheses for the stage name, and missing opening brace before steps.
  3. Final Answer:

    stage('Build') { steps { sh 'make' } } -> Option C
  4. Quick Check:

    Correct stage syntax = stage('Build') { steps { sh 'make' } } [OK]
Hint: Remember: stage('name') { steps { ... } } [OK]
Common Mistakes:
  • Using colon instead of braces
  • Writing singular 'step' instead of 'steps'
  • Omitting braces around steps block
3. Given this Jenkins pipeline snippet, what will be the output in the console for the Test stage?
pipeline {
  agent any
  stages {
    stage('Test') {
      steps {
        echo 'Running tests'
      }
    }
  }
}
medium
A. No output
B. Test stage skipped
C. Syntax error in pipeline
D. Running tests

Solution

  1. Step 1: Analyze the pipeline snippet

    The pipeline defines a single stage named 'Test' with a step that echoes 'Running tests'.
  2. Step 2: Understand echo behavior in Jenkins

    The echo command prints the given string to the console output during the stage execution.
  3. Final Answer:

    Running tests -> Option D
  4. Quick Check:

    Echo prints message = Running tests [OK]
Hint: Echo prints text inside steps block [OK]
Common Mistakes:
  • Assuming echo does not print
  • Thinking stage is skipped
  • Confusing syntax errors
4. Identify the error in this Jenkins pipeline stage block:
stage('Deploy') {
  step {
    sh 'deploy.sh'
  }
}
medium
A. The block should use steps instead of step
B. The stage name must be lowercase
C. The sh command is invalid inside a stage
D. Missing agent declaration inside the stage

Solution

  1. Step 1: Check the block keyword inside stage

    The correct keyword to wrap commands inside a stage is steps (plural), not step.
  2. Step 2: Verify other options

    Stage names can be any case, sh is valid inside steps, and agent is optional if defined globally.
  3. Final Answer:

    The block should use steps instead of step -> Option A
  4. Quick Check:

    Use 'steps' block inside stage [OK]
Hint: Inside stage, use 'steps' plural for commands [OK]
Common Mistakes:
  • Using singular 'step' instead of 'steps'
  • Thinking stage name case matters
  • Assuming sh is invalid inside stage
5. You want to create a Jenkins pipeline with two stages: Build and Test. The Test stage should only run if the Build stage succeeds. Which structure correctly enforces this using stage blocks?
hard
A. pipeline { agent any; stages { stage('Build') { steps { sh 'make' } } stage('Test') { when { expression { currentBuild.result == null || currentBuild.result == 'SUCCESS' } } steps { sh 'make test' } } } }
B. pipeline { agent any; stages { stage('Build') { steps { sh 'make' } } stage('Test') { steps { sh 'make test' } } } }
C. pipeline { agent any; stages { stage('Build') { steps { sh 'make' } } stage('Test') { when { branch 'main' } steps { sh 'make test' } } } }
D. pipeline { agent any; stages { stage('Build') { steps { sh 'make' } } stage('Test') { steps { sh 'make test' } post { always { echo 'Test done' } } } } }

Solution

  1. Step 1: Understand stage execution conditions

    By default, stages run sequentially but may run even if previous stages fail. To run Test only if Build succeeds, use a when condition checking build result.
  2. Step 2: Analyze options for conditional execution

    pipeline { agent any; stages { stage('Build') { steps { sh 'make' } } stage('Test') { when { expression { currentBuild.result == null || currentBuild.result == 'SUCCESS' } } steps { sh 'make test' } } } } uses when with an expression checking if the current build result is success or null (meaning no failure yet), ensuring Test runs only after successful Build.
  3. Step 3: Verify other options

    pipeline { agent any; stages { stage('Build') { steps { sh 'make' } } stage('Test') { steps { sh 'make test' } } } } runs both stages unconditionally. pipeline { agent any; stages { stage('Build') { steps { sh 'make' } } stage('Test') { when { branch 'main' } steps { sh 'make test' } } } } runs Test only on branch 'main' but not based on build success. pipeline { agent any; stages { stage('Build') { steps { sh 'make' } } stage('Test') { steps { sh 'make test' } post { always { echo 'Test done' } } } } } runs Test always and adds a post action.
  4. Final Answer:

    pipeline { agent any; stages { stage('Build') { steps { sh 'make' } } stage('Test') { when { expression { currentBuild.result == null || currentBuild.result == 'SUCCESS' } } steps { sh 'make test' } } } } -> Option A
  5. Quick Check:

    Use 'when' with build result for conditional stages [OK]
Hint: Use 'when' block to run stage only on success [OK]
Common Mistakes:
  • Assuming stages run conditionally by default
  • Using branch condition instead of build result
  • Missing 'when' block for conditional execution
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