Bird
Raised Fist0
Jenkinsdevops~10 mins

Agent directive in Jenkins - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Agent directive
Start Pipeline
↓
Check Agent Directive
↓
Agent Specified
↓
Allocate Agent
↓
Execute Steps
↓
Pipeline Ends
The pipeline starts, checks if an agent is specified, allocates it if yes, or runs on master if no, then executes steps and ends.
Execution Sample
Jenkins
pipeline {
  agent any
  stages {
    stage('Build') {
      steps { echo 'Building...' }
    }
  }
}
This Jenkins pipeline uses the agent directive to run on any available agent and executes a build stage.
Process Table
StepActionAgent DirectiveAgent AllocatedPipeline Step ExecutedOutput
1Start pipelineagent anyNot allocated yetNoPipeline started
2Check agent directiveagent anyAllocating any available agentNoAgent allocation in progress
3Agent allocatedagent anyAgent #5 allocatedNoAgent ready
4Execute stage 'Build'agent anyAgent #5 allocatedYesBuilding...
5Pipeline endsagent anyAgent #5 releasedNoPipeline completed
💡 Pipeline ends after all stages complete and agent is released
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
Agent Directiveagent anyagent anyagent anyagent anyagent any
Agent AllocatedNoneAllocatingAgent #5Agent #5Released
Pipeline Step ExecutedNoNoNoYesNo
Key Moments - 2 Insights
Why does the pipeline allocate an agent before running steps?
Because the 'agent any' directive tells Jenkins to find any available agent before executing steps, as shown in execution_table row 3.
What happens if no agent directive is specified?
The pipeline runs on the master node by default, skipping agent allocation, which differs from the flow shown here.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the agent actually allocated?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Check the 'Agent Allocated' column in execution_table row 3
According to variable_tracker, what is the state of 'Pipeline Step Executed' after step 3?
ANo
BYes
CAllocating
DReleased
💡 Hint
Look at the 'Pipeline Step Executed' row, column 'After Step 3'
If the agent directive was removed, how would the 'Agent Allocated' column change?
AIt would show 'Agent #5 allocated' as before
BIt would show 'No agent allocated, runs on master'
CIt would show 'Allocating any available agent'
DIt would show 'Agent released immediately'
💡 Hint
Recall the key moment about default behavior without agent directive
Concept Snapshot
Jenkins 'agent' directive tells where to run pipeline steps.
Syntax: agent any | none | label 'name'
If agent specified, Jenkins allocates it before running steps.
Without agent, runs on master node by default.
Agent is released after pipeline completes.
Full Transcript
This visual execution shows how Jenkins pipeline uses the agent directive. The pipeline starts and checks if an agent is specified. If yes, it allocates an agent before running the steps. Here, 'agent any' means any available agent is allocated. The build stage runs on that agent. After all stages finish, the agent is released and the pipeline ends. If no agent directive is given, the pipeline runs on the master node by default. Variables like 'Agent Allocated' and 'Pipeline Step Executed' track the state changes during execution.

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