How to select the correct agent to build my job?

Article ID:115000056651
2 minute readKnowledge base
On this page

Issue

How can we specify the agent to use to build a job?

Resolution

When you have multiple agent available, whether you are using a classic job type (Freestyle, Maven, etc) or a Pipeline (even Multibranch Pipeline), you can select a specific agent to build your project.

In a classic job type, you will find a Restrict where this project can be run checkbox and text field in the properties of the job:

classic job restriction

In a (Multibranch) Pipeline, you can specify this in the parameter of the node block:

node('foo') {}

The value of both the text field and the node parameter are the same: it can be the agent name, or a Groovy boolean expression.

Using the agent name, you make the job tied to this specific agent. This can be problematic when the build load grow, your agent becomes less and less available.

The best solution is to use the labels. When you configure your agents, you can put a space-separated list of labels on your agent. Those can be used to describe what the agent can be used for: maven, npm, etc. It can also describe what the agent is running on: windows, linux, x64, etc. This will create pools of agents based on the labels. Then, on your job configuration, you can use those label in a Groovy boolean expression to select the correct agent for your job:

  • "I need an agent running on linux": linux

  • "I need an agent running on windows with maven": windows && maven

  • "I need an agent with a jdk-8 and running either on windows x32 or linux x64": jdk-8 && windows && x32) || (linux && x64

The expression can be as complex as you need. However, the expression cannot be a regexp. It can only be a boolean expression.