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.
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.
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.
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.
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.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With