200

What is the difference between an agent and a node in a jenkins pipeline?

I've found those definitions:

  • Node: A Pipeline performs most of the work in the context of one or more declared node steps.
  • Agent: The agent directive specifies where the entire Pipeline, or a specific stage, will execute in the Jenkins environment depending on where the agent directive is placed.

So both are used for executing pipeline steps. But when to use which one?

1 Answer 1

216

The simple answer is, Agent is for declarative pipelines and node is for scripted pipelines.

In declarative pipelines the agent directive is used for specifying which agent/slave the job/task is to be executed on. This directive only allows you to specify where the task is to be executed, which agent, slave, label or docker image.

On the other hand, in scripted pipelines the node step can be used for executing a script/step on a specific agent, label, slave. The node step optionally takes the agent or label name and then a closure with code that is to be executed on that node.

declarative and scripted pipelines (edit based on the comment):

  • declarative pipelines is a new extension of the pipeline DSL (it is basically a pipeline script with only one step, a pipeline step with arguments (called directives), these directives should follow a specific syntax. The point of this new format is that it is more strict and therefore should be easier for those new to pipelines, allow for graphical editing and much more.
  • scripted pipelines is the fallback for advanced requirements.
15
  • 6
    Yes and no declarative pipelines is a new extension of the pipeline DSL (it is basically a pipeline script with only one step, a pipeline step with arguments (called directives), these directives should follow a specific syntax. The point of this new format is that it is more strict and therefor should be easier for those new to pipelines, allow for graphical editing and much more, see the feature list on your link above. So for simple task I would say that it's the recommended approach, but for more advanced cases, scripted is the fallback.
    – Jon S
    Commented Feb 5, 2017 at 11:19
  • 9
    Now there are 3 names with node, agent, and slave!
    – mkobit
    Commented Feb 8, 2017 at 0:10
  • 2
    @mkobit Yes, the names are quite confusing, I read somewhere (can't find the reference right now, either it was on the Jenkins blog or on Jenkins JIRA), that they are trying to rename and use agent instead of slave since it's not as a "negative" word. Then I guess declarative pipelines adopted that and called it agent. I think, node originates from scripted pipelines.
    – Jon S
    Commented Feb 8, 2017 at 6:17
  • 2
    Agent is for declarative pipelines and node is for scripted... This seems incorrect. What about a declarative Jenkinsfile that uses the syntax agent { node { label "abc" } }? Clearly node is also for declarative. So what is the difference, really?
    – cowlinator
    Commented Sep 14, 2019 at 0:22
  • 3
    This answer would be better if it showed an example of executing an a stage or node on a remote agent. im still trying to find the syntax to actually use a scripted pipeline with a remote agent. And the official doc does not provide the answer (as usual). Commented Feb 12, 2020 at 19:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.