0
0
Jenkinsdevops~20 mins

Agent directive in Jenkins - Practice Problems & Coding Challenges

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