Bird
Raised Fist0
Jenkinsdevops~20 mins

Node and stage blocks in Jenkins - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Node and Stage Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of Jenkins Pipeline with nested node blocks
What will be the output of this Jenkins pipeline snippet when executed?
Jenkins
pipeline {
  agent none
  stages {
    stage('Build') {
      steps {
        script {
          node('linux') {
            echo 'Inside first node'
            node('windows') {
              echo 'Inside nested node'
            }
          }
        }
      }
    }
  }
}
ASyntax error due to nested node blocks
BInside first node\nInside nested node
CInside nested node\nInside first node
DOnly 'Inside first node' is printed
Attempts:
2 left
💡 Hint
Think about how nested node blocks execute in Jenkins pipelines.
🧠 Conceptual
intermediate
1:30remaining
Purpose of 'agent none' in Jenkins pipeline
What is the main purpose of using agent none at the top level of a Jenkins declarative pipeline?
ATo disable automatic agent allocation so stages can specify their own agents
BTo force the pipeline to run on the master node only
CTo automatically allocate an agent for all stages
DTo skip all stages in the pipeline
Attempts:
2 left
💡 Hint
Consider how agent allocation works when you want different stages on different nodes.
❓ Troubleshoot
advanced
2:30remaining
Why does this Jenkins pipeline fail with 'No such DSL method node' error?
Given this pipeline snippet, why does Jenkins report 'No such DSL method node' error?
pipeline {
  agent any
  stages {
    stage('Test') {
      steps {
        node('linux') {
          echo 'Running tests'
        }
      }
    }
  }
}
ABecause 'node' requires a label but 'linux' is undefined
BBecause the 'agent any' conflicts with the 'node' block
CBecause 'node' block is not allowed inside 'steps' in declarative pipelines
DBecause 'echo' is not allowed inside 'node' blocks
Attempts:
2 left
💡 Hint
Check the allowed syntax inside declarative pipeline 'steps' blocks.
🔀 Workflow
advanced
3:00remaining
Correct stage and node usage for parallel builds
You want to run two builds in parallel on different nodes: one on 'linux' and one on 'windows'. Which pipeline snippet correctly achieves this?
Jenkins
pipeline {
  agent none
  stages {
    stage('Parallel Builds') {
      parallel {
        'Linux Build': {
          node('linux') {
            echo 'Building on Linux'
          }
        },
        'Windows Build': {
          node('windows') {
            echo 'Building on Windows'
          }
        }
      }
    }
  }
}
AThe pipeline runs sequentially because 'parallel' is not allowed inside 'stages'.
BThe pipeline runs only on the master node ignoring labels.
CThe pipeline will fail because 'agent' cannot be used inside parallel stages.
DThe given pipeline snippet runs both builds in parallel on specified nodes.
Attempts:
2 left
💡 Hint
Think about how parallel stages and agents work in declarative pipelines.
✅ Best Practice
expert
3:00remaining
Best practice for using node blocks in Jenkins scripted pipeline
In a scripted Jenkins pipeline, what is the best practice for using node blocks to optimize resource usage and avoid unnecessary workspace allocation?
AUse multiple <code>node</code> blocks for each step to ensure clean workspace every time
BUse a single <code>node</code> block wrapping all build steps to minimize workspace allocation overhead
CAvoid <code>node</code> blocks entirely and run all steps on the master node
DUse nested <code>node</code> blocks with different labels inside each other for flexibility
Attempts:
2 left
💡 Hint
Consider workspace allocation and agent usage costs.

Practice

(1/5)
1. What is the main purpose of a node block in a Jenkins pipeline?
easy
A. To declare environment variables for the pipeline
B. To define a named step grouping in the pipeline
C. To specify the machine or agent where the pipeline steps run
D. To send notifications after the pipeline finishes

Solution

  1. Step 1: Understand the role of node block

    The node block tells Jenkins which machine or agent will run the enclosed commands.
  2. Step 2: Differentiate from stage block

    The stage block is for grouping steps with a name, not for selecting the machine.
  3. Final Answer:

    To specify the machine or agent where the pipeline steps run -> Option C
  4. Quick Check:

    Node block = machine selection [OK]
Hint: Node block = where commands run, stage block = what step runs [OK]
Common Mistakes:
  • Confusing node with stage blocks
  • Thinking node defines step names
  • Assuming node sets environment variables
2. Which of the following is the correct syntax to define a stage inside a Jenkins pipeline?
easy
A. stage: Build { echo 'Building...' }
B. node('Build') { echo 'Building...' }
C. steps { stage('Build') { echo 'Building...' } }
D. stage('Build') { steps { echo 'Building...' } }

Solution

  1. Step 1: Recall correct stage syntax

    The correct syntax uses stage('name') { steps { ... } } to define a stage with steps inside.
  2. Step 2: Check each option

    stage('Build') { steps { echo 'Building...' } } matches the correct syntax. Options A, B, and D misuse keywords or structure.
  3. Final Answer:

    stage('Build') { steps { echo 'Building...' } } -> Option D
  4. Quick Check:

    Stage syntax = stage('name') { steps { ... } } [OK]
Hint: Stage blocks use stage('name') with steps inside [OK]
Common Mistakes:
  • Using node instead of stage for grouping steps
  • Missing steps block inside stage
  • Incorrect colon or braces placement
3. Given the Jenkins pipeline snippet below, what will be the output?
pipeline {
  agent any
  stages {
    stage('Example') {
      steps {
        node('linux') {
          echo 'Running on Linux node'
        }
      }
    }
  }
}
medium
A. Running on Linux node
B. Syntax error due to nested node inside steps
C. Pipeline runs but no output is shown
D. Error: agent and node blocks cannot be combined

Solution

  1. Step 1: Analyze pipeline structure

    The pipeline uses agent any globally and inside the stage steps calls node('linux') to run commands on a Linux node.
  2. Step 2: Understand nested node usage

    Nested node blocks inside steps are allowed in scripted pipelines; here it will run the echo command on the specified node.
  3. Final Answer:

    Running on Linux node -> Option A
  4. Quick Check:

    Nested node runs commands on specified agent [OK]
Hint: Nested node inside steps runs commands on chosen agent [OK]
Common Mistakes:
  • Assuming nested node causes syntax error
  • Thinking agent any forbids node blocks
  • Expecting no output from echo
4. Identify the error in this Jenkins pipeline snippet:
pipeline {
  agent any
  stages {
    stage('Test') {
      node {
        steps {
          echo 'Testing...'
        }
      }
    }
  }
}
medium
A. The node block is incorrectly placed inside stage
B. Missing agent inside the stage block
C. The steps block should be outside node
D. The echo command is invalid inside steps

Solution

  1. Step 1: Review pipeline block structure

    Declarative pipelines require agent to specify where stages run; node is not valid inside stage in declarative syntax.
  2. Step 2: Identify correct usage

    Instead of node, the stage should have an agent block or rely on global agent any.
  3. Final Answer:

    The node block is incorrectly placed inside stage -> Option A
  4. Quick Check:

    Declarative stage needs agent, not node [OK]
Hint: Use agent, not node, inside declarative stage blocks [OK]
Common Mistakes:
  • Using node block inside declarative stage
  • Confusing steps placement
  • Thinking echo is invalid inside steps
5. You want to run two stages in your Jenkins pipeline: 'Build' on a Linux node and 'Test' on a Windows node. Which pipeline snippet correctly uses node and stage blocks to achieve this?
hard
A. pipeline { agent none stages { stage('Build') { steps { node('linux') { echo 'Building on Linux' } } } stage('Test') { steps { node('windows') { echo 'Testing on Windows' } } } } }
B. pipeline { agent any stages { stage('Build') { agent { label 'linux' } steps { echo 'Building on Linux' } } stage('Test') { agent { label 'windows' } steps { echo 'Testing on Windows' } } } }
C. pipeline { agent any stages { stage('Build') { node('linux') { steps { echo 'Building on Linux' } } } stage('Test') { node('windows') { steps { echo 'Testing on Windows' } } } } }
D. pipeline { agent none stages { stage('Build') { agent { label 'windows' } steps { echo 'Building on Linux' } } stage('Test') { agent { label 'linux' } steps { echo 'Testing on Windows' } } } }

Solution

  1. Step 1: Understand declarative pipeline agent usage

    In declarative pipelines, use agent { label 'name' } inside stages to specify the node label.
  2. Step 2: Evaluate each option

    pipeline { agent any stages { stage('Build') { agent { label 'linux' } steps { echo 'Building on Linux' } } stage('Test') { agent { label 'windows' } steps { echo 'Testing on Windows' } } } } correctly uses agent inside each stage with labels 'linux' and 'windows'. pipeline { agent none stages { stage('Build') { steps { node('linux') { echo 'Building on Linux' } } } stage('Test') { steps { node('windows') { echo 'Testing on Windows' } } } } } misuses node inside steps in declarative style. pipeline { agent any stages { stage('Build') { node('linux') { steps { echo 'Building on Linux' } } } stage('Test') { node('windows') { steps { echo 'Testing on Windows' } } } } } uses node blocks incorrectly inside stage. pipeline { agent none stages { stage('Build') { agent { label 'windows' } steps { echo 'Building on Linux' } } stage('Test') { agent { label 'linux' } steps { echo 'Testing on Windows' } } } } swaps labels incorrectly.
  3. Final Answer:

    Option B uses agent labels correctly inside stages -> Option B
  4. Quick Check:

    Use agent { label } inside stages for node selection [OK]
Hint: Use agent { label 'name' } inside stages, not node blocks [OK]
Common Mistakes:
  • Using node blocks inside declarative stages
  • Swapping node labels by mistake
  • Not setting agent none when using stage agents
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