Scripted Pipelines
(this post if part of the material I cover in my devops course)
Scripted pipelines are using
Groovy language syntax to achieve part of the requirements for a pipeline.
The other option (that we'll cover in another post) is Declarative syntax.
Scripted Pipeline example
- Create a pipeline as explained in pipelines-primer-1 but select Scripted Pipeline from the dropdown selection:
1node {
2 def mvnHome
3 stage('Preparation') { // for display purposes
4 // Get some code from a GitHub repository
5 git 'https://github.com/jglick/simple-maven-project-with-tests.git'
6 // Get the Maven tool.
7 // ** NOTE: This 'M3' Maven tool must be configured
8 // ** in the global configuration.
9 mvnHome = tool 'M3'
10 }
11 stage('Build') {
12 // Run the maven build
13 withEnv(["MVN_HOME=$mvnHome"]) {
14 if (isUnix()) {
15 sh '"$MVN_HOME/bin/mvn" -Dmaven.test.failure.ignore clean package'
16 } else {
17 bat(/"%MVN_HOME%\bin\mvn" -Dmaven.test.failure.ignore clean package/)
18 }
19 }
20 }
21 stage('Results') {
22 junit '**/target/surefire-reports/TEST-*.xml'
23 archiveArtifacts 'target/*.jar'
24 }
25}
Run the scripted pipeline example
- this pipeline is not going to run until you create a maven instalation (tool) called M3
- Go to Dashboard--> Manage Jenkins --> Tools
- Scroll to the bottom of this window
- Add maven, call it M3
- Go ahead and run the pipeline
Scripted Pipeline characteristics
- the main component is node (vs. pipeline for declarative pipelines)
- There are stages, simmilar to those used in declarative syntax
- There are no steps, and these are actually implemented as Groovy lines
- There us a global declaration of a variable (mvnHome) later used to create an environment variable
- There are Groovy constructs (if clauses, function calls) used throughout the pipeline.