Skip to content

Commit 094fa9f

Browse files
author
R. Tyler Croy
authored
Merge pull request #77 from HRMPW/master
Adding Declarative Examples
2 parents b894be2 + ad1332b commit 094fa9f

25 files changed

+721
-0
lines changed

declarative-examples/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Declarative Examples
2+
3+
This folder is a home for snippets, tips and tricks and examples for writing Declarative Pipelines with the
4+
[Jenkins Pipeline plugin](https://github.com/jenkinsci/workflow-plugin/blob/master/README.md).
5+
6+
# Layout
7+
8+
The repository is broken up into two directories currently:
9+
10+
* *simple-examples* - simple pipelines with one or two stages that illustrate or test a single part of Declarative Pipeline
11+
* *jenkinsfile-examples* - for examples of using `Jenkinsfile`s checked into repositories.
12+
13+
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.
15+
16+
# License
17+
18+
All contributions are under the MIT license, like Jenkins itself.
19+
20+
# Pull requests
21+
22+
We want them!
23+
24+
# External resources
25+
26+
* [Pipeline scripts collection of the Docker team](https://github.com/docker/jenkins-pipeline-scripts)
27+
* [Pipeline scripts collection of the Fabric8 team](https://github.com/fabric8io/jenkins-pipeline-library)
28+
* [Pipeline scripts collection of the Funkwerk](https://github.com/funkwerk/jenkins-workflow)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Jenkinsfile examples
2+
3+
This directory contains example Jenkinsfiles written in Declarative Pipeline Syntax.
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
pipeline {
2+
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+
*/
7+
agent {
8+
node {
9+
label 'docker'
10+
}
11+
}
12+
13+
// using the Timestamper plugin we can add timestamps to the console log
14+
options {
15+
timestamps()
16+
}
17+
18+
environment {
19+
//Use Pipeline Utility Steps plugin to read information from pom.xml into env variables
20+
IMAGE = readMavenPom().getArtifactId()
21+
VERSION = readMavenPom().getVersion()
22+
}
23+
24+
stages {
25+
stage('Build') {
26+
agent {
27+
docker {
28+
/*
29+
* Reuse the workspace on the agent defined at top-level of Pipeline but run inside a container.
30+
* In this case we are running a container with maven so we don't have to install specific versions
31+
* of maven directly on the agent
32+
*/
33+
reuseNode true
34+
image 'maven:3.5.0-jdk-8'
35+
}
36+
}
37+
steps {
38+
// using the Pipeline Maven plugin we can set maven configuration settings, publish test results, and annotate the Jenkins console
39+
withMaven(options: [findbugsPublisher(), junitPublisher(ignoreAttachments: false)]) {
40+
sh 'mvn clean findbugs:findbugs package'
41+
}
42+
}
43+
post {
44+
success {
45+
// we only worry about archiving the jar file if the build steps are successful
46+
archiveArtifacts(artifacts: '**/target/*.jar', allowEmptyArchive: true)
47+
}
48+
}
49+
}
50+
51+
stage('Quality Analysis') {
52+
parallel {
53+
// run Sonar Scan and Integration tests in parallel. This syntax requires Declarative Pipeline 1.2 or higher
54+
stage ('Integration Test') {
55+
agent any //run this stage on any available agent
56+
steps {
57+
echo 'Run integration tests here...'
58+
}
59+
}
60+
stage('Sonar Scan') {
61+
agent {
62+
docker {
63+
// we can use the same image and workspace as we did previously
64+
reuseNode true
65+
image 'maven:3.5.0-jdk-8'
66+
}
67+
}
68+
environment {
69+
//use 'sonar' credentials scoped only to this stage
70+
SONAR = credentials('sonar')
71+
}
72+
steps {
73+
sh 'mvn sonar:sonar -Dsonar.login=$SONAR_PSW'
74+
}
75+
}
76+
}
77+
}
78+
79+
stage('Build and Publish Image') {
80+
when {
81+
branch 'master' //only run these steps on the master branch
82+
}
83+
steps {
84+
/*
85+
* Multiline strings can be used for larger scripts. It is also possible to put scripts in your shared library
86+
* and load them with 'libaryResource'
87+
*/
88+
sh """
89+
docker build -t ${IMAGE} .
90+
docker tag ${IMAGE} ${IMAGE}:${VERSION}
91+
docker push ${IMAGE}:${VERSION}
92+
"""
93+
}
94+
}
95+
}
96+
97+
post {
98+
failure {
99+
// notify users when the Pipeline fails
100+
mail to: '[email protected]',
101+
subject: "Failed Pipeline: ${currentBuild.fullDisplayName}",
102+
body: "Something is wrong with ${env.BUILD_URL}"
103+
}
104+
}
105+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Simple Examples
2+
3+
This folder contains simple Declarative Pipelines with one or two stages.
4+
The pipelines in this folder are examples of specific features or useful in testing.
5+
6+
The naming convention is based on the primary feature being exhibited.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
pipeline {
2+
environment {
3+
// environment variables and credential retrieval can be interspersed
4+
SOME_VAR = "SOME VALUE"
5+
// this assumes that "cred1" has been created on Jenkins Credentials
6+
CRED1 = credentials("cred1")
7+
INBETWEEN = "Something in between"
8+
// this assumes that "cred2" has been created in Jenkins Credentials
9+
CRED2 = credentials("cred2")
10+
// Env variables can refer to other variables as well
11+
OTHER_VAR = "${SOME_VAR}"
12+
}
13+
14+
agent any
15+
16+
stages {
17+
stage("foo") {
18+
steps {
19+
// environment variables are not masked
20+
sh 'echo "SOME_VAR is $SOME_VAR"'
21+
sh 'echo "INBETWEEN is $INBETWEEN"'
22+
sh 'echo "OTHER_VAR is $OTHER_VAR"'
23+
24+
// credential variables will be masked in console log but not in archived file
25+
sh 'echo $CRED1 > cred1.txt'
26+
sh 'echo $CRED2 > cred2.txt'
27+
archive "**/*.txt"
28+
}
29+
}
30+
}
31+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
pipeline {
2+
environment {
3+
/*
4+
* Uses a Jenkins credential called "FOOCredentials" and creates environment variables:
5+
* "$FOO" will contain string "USR:PSW"
6+
* "$FOO_USR" will contain string for Username
7+
* "$FOO_PSW" will contain string for Password
8+
*/
9+
FOO = credentials("FOOcredentials")
10+
}
11+
12+
agent any
13+
14+
stages {
15+
stage("foo") {
16+
steps {
17+
// all credential values are available for use but will be masked in console log
18+
sh 'echo "FOO is $FOO"'
19+
sh 'echo "FOO_USR is $FOO_USR"'
20+
sh 'echo "FOO_PSW is $FOO_PSW"'
21+
22+
//Write to file
23+
dir("combined") {
24+
sh 'echo $FOO > foo.txt'
25+
}
26+
sh 'echo $FOO_PSW > foo_psw.txt'
27+
sh 'echo $FOO_USR > foo_usr.txt'
28+
archive "**/*.txt"
29+
}
30+
}
31+
}
32+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
pipeline {
2+
agent {
3+
dockerfile {
4+
/*
5+
* The Default is "Dockerfile" but this can be changed.
6+
* This will build a new container based on the contents of "Dockerfile.alternate"
7+
* and run the pipline inside this container
8+
*/
9+
filename "Dockerfile.alternate"
10+
args "-v /tmp:/tmp -p 8000:8000"
11+
}
12+
}
13+
stages {
14+
stage("foo") {
15+
steps {
16+
sh 'cat /hi-there'
17+
sh 'echo "The answer is 42"'
18+
}
19+
}
20+
}
21+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
pipeline {
2+
agent {
3+
dockerfile {
4+
/*
5+
* This assumes that a "Dockerfile" is in the current workspace
6+
* A new container will be build with the args below and the pipeline will run inside that container.
7+
*/
8+
args "-v /tmp:/tmp -p 8000:8000"
9+
}
10+
}
11+
stages {
12+
stage("foo") {
13+
steps {
14+
sh 'cat /hi-there'
15+
sh 'echo "The answer is 42"'
16+
}
17+
}
18+
}
19+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
pipeline {
2+
agent any
3+
4+
environment {
5+
// FOO will be available in entire pipeline
6+
FOO = "PIPELINE"
7+
}
8+
9+
stages {
10+
stage("local") {
11+
environment {
12+
// BAR will only be available in this stage
13+
BAR = "STAGE"
14+
}
15+
steps {
16+
sh 'echo "FOO is $FOO and BAR is $BAR"'
17+
}
18+
}
19+
stage("global") {
20+
steps {
21+
sh 'echo "FOO is $FOO and BAR is $BAR"'
22+
}
23+
}
24+
}
25+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
pipeline {
2+
/*
3+
* 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.
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
8+
*/
9+
environment {
10+
FOO = "BAR"
11+
BUILD_NUM_ENV = currentBuild.getNumber()
12+
ANOTHER_ENV = "${currentBuild.getNumber()}"
13+
INHERITED_ENV = "\${BUILD_NUM_ENV} is inherited"
14+
ACME_FUNC = readMavenPom().getArtifactId()
15+
}
16+
17+
agent any
18+
19+
stages {
20+
stage("Environment") {
21+
steps {
22+
sh 'echo "FOO is $FOO"'
23+
// returns 'FOO is BAR'
24+
25+
sh 'echo "BUILD_NUM_ENV is $BUILD_NUM_ENV"'
26+
// returns 'BUILD_NUM_ENV is 4' depending on the build number
27+
28+
sh 'echo "ANOTHER_ENV is $ANOTHER_ENV"'
29+
// returns 'ANOTHER_ENV is 4' like the previous depending on the build number
30+
31+
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+
35+
sh 'echo "ACME_FUNC is $ACME_FUNC"'
36+
// returns 'ACME_FUNC is spring-petclinic' or the name of the artifact in the pom.xml
37+
}
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)