194

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?

np2807
  • 1,050
  • 15
  • 30
Matthias M
  • 12,906
  • 17
  • 87
  • 116

1 Answers1

211

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.
Jon S
  • 15,846
  • 4
  • 44
  • 45
  • Is it right, that declarative and scripted are two different syntaxes where declarative is the new and recommended one? https://jenkins.io/blog/2017/02/03/declarative-pipeline-ga: This syntax .. is now referred to as "Scripted Pipeline Syntax" to distinguish it from "Declarative Pipeline Syntax." – Matthias M Feb 05 '17 at 11:10
  • 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 Feb 05 '17 at 11:19
  • Thanks, I added your further explanations to your answer, because It think these are usefull, too. – Matthias M Feb 06 '17 at 12:06
  • 9
    Now there are 3 names with `node`, `agent`, and `slave`! – mkobit Feb 08 '17 at 00: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 Feb 08 '17 at 06:17
  • @JonS Great answer. But as far as I understand it's not useful to use a node inside a step in a declarative pipeline? Use agent if you want to run it somewhere else? – DenCowboy Nov 29 '17 at 10:16
  • @DenCowboy No, don't think it is possible to do a node step in declarative, haven't tested though... You use the agent directive to tell Jenkins which agent/slave to run the declarative pipeline on. – Jon S Nov 29 '17 at 10:33
  • @JonS Thanks, because I'm a bit confused with the 4th best practice here but it's maybe for scripted pipelines: https://dzone.com/articles/top-10-best-practices-for-jenkins-pipeline – DenCowboy Nov 29 '17 at 10:35
  • 1
    @DenCowboy Aha, yes,I would say that the 4th best practice there only apply to scripted pipeline since declarative always allocate a node, the agent directive only restricts which node is allocated. – Jon S Nov 29 '17 at 10:40
  • @JonS Now I understand, but when I have 4 agents with the same label it's possible my stages will run on different slaves? (all with the same label?) (it does not seem to happen) – DenCowboy Nov 29 '17 at 10:43
  • 1
    @DenCowboy No it should use the same, it just use the agent directive to initially determine which agent/slave to use. – Jon S Nov 29 '17 at 12:22
  • @mkobit, no still 2, Slave is a type of Node, the other is Master. – Iqra. Jul 27 '18 at 07:05
  • From completeness (you may add this in the answer) this link is the documentation for Declaritive pipelines: https://jenkins.io/doc/book/pipeline/syntax/ and this is for Scripted pipelines: https://jenkins.io/doc/book/pipeline/syntax/#scripted-pipeline Note that I am new to this as well so feel free to correct me if I am wrong – George Pligoropoulos Sep 04 '19 at 09:15
  • 1
    `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 Sep 14 '19 at 00:22
  • 2
    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). – Peter Moore Feb 12 '20 at 19:44