Bird
Raised Fist0
Jenkinsdevops~20 mins

Agent directive in Jenkins - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Agent Directive Mastery
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 declarative pipeline snippet using the agent directive:

pipeline {
  agent any
  stages {
    stage('Example') {
      steps {
        echo 'Running on any available agent'
      }
    }
  }
}

What will Jenkins do when this pipeline runs?

Jenkins
pipeline {
  agent any
  stages {
    stage('Example') {
      steps {
        echo 'Running on any available agent'
      }
    }
  }
}
AJenkins will run the pipeline on any available agent node and print 'Running on any available agent'.
BJenkins will run the pipeline only on the master node and print 'Running on any available agent'.
CJenkins will fail to run because 'agent any' is invalid syntax.
DJenkins will run the pipeline on a docker container by default.
Attempts:
2 left
💡 Hint

Think about what agent any means in Jenkins pipelines.

🧠 Conceptual
intermediate
1:30remaining
Which agent directive syntax specifies running on a specific label?

In Jenkins declarative pipelines, how do you specify that the pipeline should run only on agents with a specific label?

Aagent none
Bagent any
Cagent { label 'my-label' }
Dagent { docker 'my-label' }
Attempts:
2 left
💡 Hint

Labels help Jenkins pick specific nodes.

❓ Troubleshoot
advanced
2:00remaining
Why does this pipeline fail to run on any agent?

Given this Jenkins pipeline snippet:

pipeline {
  agent none
  stages {
    stage('Build') {
      steps {
        echo 'Building...'
      }
    }
  }
}

Why does Jenkins fail to run this pipeline?

Jenkins
pipeline {
  agent none
  stages {
    stage('Build') {
      steps {
        echo 'Building...'
      }
    }
  }
}
ABecause <code>agent none</code> disables automatic agent allocation and no agent is specified at stage level.
BBecause <code>agent none</code> is invalid syntax and causes a syntax error.
CBecause Jenkins requires <code>agent any</code> at the pipeline level always.
DBecause the <code>echo</code> step is not allowed without a docker agent.
Attempts:
2 left
💡 Hint

Think about what agent none means and where agents can be specified.

🔀 Workflow
advanced
2:30remaining
How to run different stages on different agents in one pipeline?

You want to run the 'Build' stage on an agent labeled 'linux' and the 'Test' stage on an agent labeled 'windows'. Which pipeline snippet achieves this?

A
pipeline {
  agent { label 'linux' }
  stages {
    stage('Build') {
      steps { echo 'Building on Linux' }
    }
    stage('Test') {
      agent { label 'windows' }
      steps { echo 'Testing on Windows' }
    }
  }
}
B
pipeline {
  agent any
  stages {
    stage('Build') {
      steps { echo 'Building on Linux' }
    }
    stage('Test') {
      steps { echo 'Testing on Windows' }
    }
  }
}
C
pipeline {
  agent none
  stages {
    stage('Build') {
      steps { echo 'Building on Linux' }
    }
    stage('Test') {
      steps { echo 'Testing on Windows' }
    }
  }
}
D
pipeline {
  agent none
  stages {
    stage('Build') {
      agent { label 'linux' }
      steps { echo 'Building on Linux' }
    }
    stage('Test') {
      agent { label 'windows' }
      steps { echo 'Testing on Windows' }
    }
  }
}
Attempts:
2 left
💡 Hint

Remember that agent none disables global agent and you can specify agents per stage.

✅ Best Practice
expert
3:00remaining
What is the best practice for using the agent directive in multi-stage pipelines?

In a Jenkins declarative pipeline with multiple stages requiring different environments, what is the best practice for using the agent directive?

AUse <code>agent any</code> at the pipeline level and avoid specifying agents at stage level to keep it simple.
BUse <code>agent none</code> at the pipeline level and specify <code>agent</code> per stage to optimize resource usage.
CUse <code>agent none</code> everywhere and run all steps on the master node manually.
DUse <code>agent docker</code> globally to run all stages inside the same container.
Attempts:
2 left
💡 Hint

Think about flexibility and resource optimization in pipelines.

Practice

(1/5)
1. What does the agent directive specify in a Jenkins pipeline?
easy
A. The build triggers for the pipeline
B. The version of Jenkins to use
C. The environment variables for the pipeline
D. Where the pipeline or stage should run

Solution

  1. Step 1: Understand the role of agent

    The agent directive tells Jenkins where to run the pipeline or a specific stage.
  2. Step 2: Compare other options

    Options A, B, and C relate to Jenkins configuration but not the agent directive.
  3. Final Answer:

    Where the pipeline or stage should run -> Option D
  4. Quick Check:

    Agent directive = run location [OK]
Hint: Agent means where to run your pipeline or stage [OK]
Common Mistakes:
  • Confusing agent with environment variables
  • Thinking agent sets build triggers
  • Assuming agent defines Jenkins version
2. Which of the following is the correct syntax to specify that a Jenkins pipeline should run on any available agent?
easy
A. agent any
B. agent: any
C. agent = any
D. agent: 'any'

Solution

  1. Step 1: Recall correct Jenkins pipeline syntax

    The correct syntax to specify any available agent is agent any without colons or equals.
  2. Step 2: Analyze other options

    Options A, B, and D use incorrect punctuation or quotes not valid in declarative pipeline syntax.
  3. Final Answer:

    agent any -> Option A
  4. Quick Check:

    Correct syntax for any agent = agent any [OK]
Hint: Use 'agent any' without punctuation for any agent [OK]
Common Mistakes:
  • Adding colons or equals signs incorrectly
  • Using quotes around any
  • Confusing declarative syntax with scripted syntax
3. Given this Jenkins pipeline snippet, what agent will the Build stage run on?
pipeline {
  agent none
  stages {
    stage('Build') {
      agent { label 'linux' }
      steps {
        echo 'Building on Linux agent'
      }
    }
  }
}
medium
A. Agent with label 'linux'
B. Any available agent
C. No agent, pipeline will fail
D. Agent with label 'windows'

Solution

  1. Step 1: Understand agent none at pipeline level

    The pipeline disables default agent allocation with agent none.
  2. Step 2: Check stage-level agent directive

    The Build stage specifies agent { label 'linux' }, so it runs on an agent labeled 'linux'.
  3. Final Answer:

    Agent with label 'linux' -> Option A
  4. Quick Check:

    Stage agent label 'linux' overrides pipeline none [OK]
Hint: Stage agent label overrides pipeline agent none [OK]
Common Mistakes:
  • Assuming pipeline agent none means no agent anywhere
  • Confusing label names
  • Thinking default agent applies despite agent none
4. Identify the error in this Jenkins pipeline snippet:
pipeline {
  agent {
    label docker
  }
  stages {
    stage('Test') {
      steps {
        echo 'Testing...'
      }
    }
  }
}
medium
A. Incorrect agent block syntax, should be agent { label 'docker' }
B. Missing quotes around label name
C. Agent directive cannot use label
D. Pipeline missing stages block

Solution

  1. Step 1: Review agent block syntax

    The label value must be enclosed in quotes, like label 'docker'.
  2. Step 2: Identify syntax error

    The snippet has label docker without quotes, causing a syntax error as docker is treated as an undefined variable.
  3. Final Answer:

    Missing quotes around label name -> Option B
  4. Quick Check:

    Agent block syntax must be exact [OK]
Hint: Agent block must be exactly agent { label 'name' } [OK]
Common Mistakes:
  • Misplacing braces or indentation
  • Using agent label without braces
  • Confusing agent label with docker agent syntax
5. You want a Jenkins pipeline that does not allocate any agent by default but runs the Deploy stage on a Docker container with image nginx:latest. Which pipeline snippet correctly achieves this?
hard
A. pipeline { agent any stages { stage('Deploy') { agent none steps { echo 'Deploying with nginx container' } } } }
B. pipeline { agent { docker { image 'nginx:latest' } } stages { stage('Deploy') { steps { echo 'Deploying with nginx container' } } } }
C. pipeline { agent none stages { stage('Deploy') { agent { docker { image 'nginx:latest' } } steps { echo 'Deploying with nginx container' } } } }
D. pipeline { agent none stages { stage('Deploy') { agent { label 'nginx:latest' } steps { echo 'Deploying with nginx container' } } } }

Solution

  1. Step 1: Disable default agent allocation

    Using agent none at pipeline level disables default agent allocation.
  2. Step 2: Specify Docker agent for Deploy stage

    The Deploy stage uses agent { docker { image 'nginx:latest' } } to run inside the nginx Docker container.
  3. Step 3: Verify other options

    pipeline { agent { docker { image 'nginx:latest' } } stages { stage('Deploy') { steps { echo 'Deploying with nginx container' } } } } runs entire pipeline in Docker, not just Deploy stage. pipeline { agent any stages { stage('Deploy') { agent none steps { echo 'Deploying with nginx container' } } } } wrongly uses agent none inside stage with agent any pipeline. pipeline { agent none stages { stage('Deploy') { agent { label 'nginx:latest' } steps { echo 'Deploying with nginx container' } } } } uses label instead of docker image.
  4. Final Answer:

    Pipeline with agent none and stage agent docker image nginx:latest -> Option C
  5. Quick Check:

    agent none + stage docker agent = correct [OK]
Hint: Use agent none globally, docker agent per stage [OK]
Common Mistakes:
  • Using agent any globally when none is needed
  • Misusing label instead of docker image
  • Placing agent none inside stage incorrectly
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