Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins Pipeline Jenkinsfile: 'node' and 'pipeline' directives

I am getting started with Jenkins declarative Pipeline. From some of the examples I have seen, I notice that the Jenkinsfile is setup with the Pipeline directive:

pipeline {     agent any       stages {         stage('Build') {              steps {                  sh 'make'              }         }         stage('Test'){             steps {                 sh 'make check'                 junit 'reports/**/*.xml'              }         }         stage('Deploy') {             steps {                 sh 'make publish'             }         }     } } 

In other examples, I notice that the Jenkinsfile is setup with a node directive:

node {     stage 'Checkout'         checkout scm      stage 'Build'         bat 'nuget restore SolutionName.sln'         bat "\"${tool 'MSBuild'}\" SolutionName.sln /p:Configuration=Release /p:Platform=\"Any CPU\" /p:ProductVersion=1.0.0.${env.BUILD_NUMBER}"      stage 'Archive'         archive 'ProjectName/bin/Release/**'  } 

I haven't been able to find solid documentation on exactly when / why to use each of these. Does anybody have any information on why these differ and when it is appropriate to use either of them?

I'm not sure but I belive the 'node' directive is used in scripted pipeline as opposed to declarative pipeline.

Thanks in advance for any guidance.

like image 894
J0991 Avatar asked Jun 20 '17 16:06

J0991


People also ask

What is the difference between node and pipeline in Jenkinsfile?

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.

What are the 3 types of pipelines in Jenkins?

Declarative versus Scripted Pipeline syntax Declarative and Scripted Pipelines are constructed fundamentally differently. Declarative Pipeline is a more recent feature of Jenkins Pipeline which: provides richer syntactical features over Scripted Pipeline syntax, and.

What is node and stage in Jenkins pipeline?

node specifies where something shall happen. You give a name or a label, and Jenkins runs the block there. stage structures your script into a high-level sequence. Stages show up as columns in the Pipeline Stage view with average stage times and colours for the stage result.

How does Jenkinsfile create Jenkins pipeline?

To get started quickly with Pipeline: Copy one of the examples below into your repository and name it Jenkinsfile. Click the New Item menu within Jenkins. Provide a name for your new item (e.g. My-Pipeline) and select Multibranch Pipeline.


1 Answers

yes, a top-level node implies scripted pipeline, and a top-level pipeline implies declarative pipeline.

declarative appears to be the more future-proof option and the one that people recommend, like in this jenkins user list post where a core contributor says "go declarative." it's the only one the Visual Pipeline Editor can support. it supports validation. and it ends up having most of the power of scripted since you can fall back to scripted in most contexts. occasionally someone comes up with a use case where they can't quite do what they want to do with declarative, but this is generally people who have been using scripted for some time, and these feature gaps are likely to close in time. and finally, if you really have to bail on one or the other, writing a programmatic translator from declarative to scripted would be easier than the other way around (sort of by definition, since the grammar is more tightly constrained).

more context on pros of declarative from the general availability blog post: https://jenkins.io/blog/2017/02/03/declarative-pipeline-ga/

the most official docs i could find that mention both (as of June 21, 2017): https://jenkins.io/doc/book/pipeline/syntax/

like image 113
burnettk Avatar answered Sep 25 '22 17:09

burnettk