Declarative Pipelines
(this post if part of the material I cover in my devops course)
Declarative pipelines were created to allow users create pipelines without learning Groovy programming.
Declarative pipeline example
- Create a new pipeline, as we've done in
pipelines-primer-1 post
...but... - Choose GitHub + Maven example, which is the same as the one we have covered in scripted-pipelines, but using a declarative syntax.
- Here's the code:
1pipeline {
2 agent any
3
4 tools {
5 // Install the Maven version configured as "M3" and add it to the path.
6 maven "M3"
7 }
8
9 stages {
10 stage('Build') {
11 steps {
12 // Get some code from a GitHub repository
13 git 'https://github.com/jglick/simple-maven-project-with-tests.git'
14
15 // Run Maven on a Unix agent.
16 sh "mvn -Dmaven.test.failure.ignore=true clean package"
17
18 // To run Maven on a Windows agent, use
19 // bat "mvn -Dmaven.test.failure.ignore=true clean package"
20 }
21
22 post {
23 // If Maven was able to run the tests, even if some of the test
24 // failed, record the test results and archive the jar file.
25 success {
26 junit '**/target/surefire-reports/TEST-*.xml'
27 archiveArtifacts 'target/*.jar'
28 }
29 }
30 }
31 }
32}
Scripted Pipeline characteristics
- there is a pipeline block at the beginning (which tells us that this pipline uses declarative syntax)
- The agent component defines where we can run our pipeline
- The tools sections is used to auto-install a tool and put it on the PATH.
- There is a stages, stage and step components
- There is no option to ask if this is Linux (as was possible in the scripted version)
so: if agent is any, then you must make sure that verything is really OK.
Otherwise, create two version of the pipeline, and use the correct agent and commands in each. - There is a
post section, that defines one or more additional steps that are run upon the completion of a Pipeline’s or stage’s run.
post can support any of the following post-condition blocks: always, changed, fixed, regression, aborted, failure, success, unstable, unsuccessful, and cleanup.
We don't see the post in the graphic view of the stages, because it is not one of the stages.