Skip to content

Commit ed42666

Browse files
author
Patrick Wolf
committed
Adding changes based on review from Daniel Beck.
1 parent d26ffa7 commit ed42666

14 files changed

+86
-83
lines changed

declarative-examples/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
# Declarartive Examples
1+
# Declarative Examples
22

33
This folder is a home for snippets, tips and tricks and examples for writing Declarative Pipelines with the
44
[Jenkins Pipeline plugin](https://github.com/jenkinsci/workflow-plugin/blob/master/README.md).
55

66
# Layout
77

8-
The repository is broken up into four directories currently:
8+
The repository is broken up into two directories currently:
99

1010
* *simple-examples* - simple pipelines with one or two stages that illustrate or test a single part of Declarative Pipeline
1111
* *jenkinsfile-examples* - for examples of using `Jenkinsfile`s checked into repositories.
1212

1313

14-
Please put your script into its own directory under the appropriate directory above, with a README.md file included explaining what your script does or shows. Make sure your script is commented so that others can understand how it works, why it works, etc.
14+
When contributing new examples please put them into the appropriate directory above. Make sure your script is commented so that others can understand how it works, why it works, etc.
1515

1616
# License
1717

declarative-examples/jenkinsfile-examples/mavenDocker.groovy

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
pipeline {
22

3-
//Run everything on an agent that has docker daemon and git installed
3+
/*
4+
* Run everything on an existing agent configured with a label 'docker'.
5+
* This agent will need docker, git and a jdk installed at a minimum.
6+
*/
47
agent {
58
node {
69
label 'docker'

declarative-examples/simple-examples/credentialsMixedEnvironmentand.groovy renamed to declarative-examples/simple-examples/credentialsMixedEnvironment.groovy

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ pipeline {
22
environment {
33
// environment variables and credential retrieval can be interspersed
44
SOME_VAR = "SOME VALUE"
5+
// this assumes that "cred1" has been created on Jenkins Credentials
56
CRED1 = credentials("cred1")
67
INBETWEEN = "Something in between"
8+
// this assumes that "cred2" has been created in Jenkins Credentials
79
CRED2 = credentials("cred2")
8-
OTHER_VAR = "OTHER VALUE"
10+
// Env variables can refer to other variables as well
11+
OTHER_VAR = "${SOME_VAR}"
912
}
1013

1114
agent any

declarative-examples/simple-examples/environmentNonLiteral.groovy

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
pipeline {
22
/*
33
* The environment section is used for setting environment variables and will allow for
4-
* expanding variable and other methods to set those values as long as the return type is a String
4+
* expanding variable and other methods to set those values as long as the return type is a String.
5+
* String escaping in Groovy can affect the behavior here. Please refer here for detailed explainations
6+
* http://docs.groovy-lang.org/latest/html/documentation/#all-strings
7+
* The 'readMavenPom()' method is provided by the Pipeline Utility Steps plugin
58
*/
69
environment {
710
FOO = "BAR"
811
BUILD_NUM_ENV = currentBuild.getNumber()
912
ANOTHER_ENV = "${currentBuild.getNumber()}"
1013
INHERITED_ENV = "\${BUILD_NUM_ENV} is inherited"
11-
ACME_FUNC = returnAThing("banana")
14+
ACME_FUNC = readMavenPom().getArtifactId()
1215
}
1316

1417
agent any
@@ -17,10 +20,20 @@ pipeline {
1720
stage("Environment") {
1821
steps {
1922
sh 'echo "FOO is $FOO"'
23+
// returns 'FOO is BAR'
24+
2025
sh 'echo "BUILD_NUM_ENV is $BUILD_NUM_ENV"'
26+
// returns 'BUILD_NUM_ENV is 4' depending on the build number
27+
2128
sh 'echo "ANOTHER_ENV is $ANOTHER_ENV"'
29+
// returns 'ANOTHER_ENV is 4' like the previous depending on the build number
30+
2231
sh 'echo "INHERITED_ENV is $INHERITED_ENV"'
32+
// returns 'INHERITED_ENV is ${BUILD_NUM_ENV} is inherited'
33+
// The \ escapes the $ so the variable is not expanded but becomes a literal
34+
2335
sh 'echo "ACME_FUNC is $ACME_FUNC"'
36+
// returns 'ACME_FUNC is spring-petclinic' or the name of the artifact in the pom.xml
2437
}
2538
}
2639
}

declarative-examples/simple-examples/legacyMetaStepSyntax.groovy

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ pipeline {
1111
* This syntax works fine in Declarative
1212
*/
1313
step([$class: 'ArtifactArchiver', artifacts: 'msg.out', fingerprint: true])
14-
sh('echo ONSLAVE=$ONSLAVE')
14+
15+
// Parenthesis are optional when a single parameter is used
16+
sh('echo $PATH')
17+
sh 'echo $PATH'
1518
}
1619
}
1720
}

declarative-examples/simple-examples/postConditionOrder.groovy

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,30 @@ pipeline {
1010
}
1111
}
1212
post {
13-
// these steps will run at the end of the pipeline based on the condition.
14-
// The conditions run in this order regardless of their arrangement
13+
/*
14+
* These steps will run at the end of the pipeline based on the condition.
15+
* Post conditions run in order regardless of their place in pipeline
16+
* 1. always - always run
17+
* 2. changed - run if something changed from last run
18+
* 3. aborted, success, unstable or failure - depending on status
19+
*/
1520
always {
1621
echo "I AM ALWAYS first"
1722
}
1823
changed {
1924
echo "CHANGED is run second"
2025
}
26+
aborted {
27+
echo "SUCCESS, FAILURE, UNSTABLE, or ABORTED are exclusive of each other"
28+
}
2129
success {
22-
echo "SUCCESS or FAILURE run third"
30+
echo "SUCCESS, FAILURE, UNSTABLE, or ABORTED runs last"
31+
}
32+
unstable {
33+
echo "SUCCESS, FAILURE, UNSTABLE, or ABORTED runs last"
2334
}
2435
failure {
25-
echo "SUCCESS or FAILURE run third"
36+
echo "SUCCESS, FAILURE, UNSTABLE, or ABORTED runs last"
2637
}
2738
}
2839
}

declarative-examples/simple-examples/toolsBuildPluginParentPOM.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
pipeline {
2-
// use the 'tools' section to use specific tool versions already defined in Jenkins config and installed on Agent
2+
// use the 'tools' section to use specific tool versions already defined in Jenkins config
33
tools {
44
maven "apache-maven-3.1.0"
55
jdk "default"

declarative-examples/simple-examples/toolsInStage.groovy

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
pipeline {
2-
agent any
2+
agent none
33
stages {
44
stage("foo") {
5-
// Tools configured in Jenkins can be included at the Pipeline level or at the Stage Level.
5+
6+
agent any
7+
8+
/*
9+
* Tools configured in Jenkins can be included at the Pipeline level or at the Stage Level.
10+
* This allows you to use different tools on different agents.
11+
* In this case the pipeline does not have a agent assigned, 'agent none', but
12+
* the stage does have an agent, 'agent any'. If I use a different agent in a
13+
* subsequent stage I can use a different version of the tool or different tools.
14+
*/
615
tools {
716
maven "apache-maven-3.0.1"
817
}

declarative-examples/simple-examples/whenEnvFalse.groovy

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)