-
-
Save loverde/0c21c5446fc571128619 to your computer and use it in GitHub Desktop.
Best of Jenkinsfile, a collection of useful workflow scripts ready to be copied into your Jenkinsfile on a per use basis.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Example of a common docker build script | |
*/ | |
def call(body) { | |
def config = [:] | |
body.resolveStrategy = Closure.DELEGATE_FIRST | |
body.delegate = config | |
body() | |
stage 'checkout' | |
node { | |
checkout scm | |
stage 'main' | |
docker.image(config.environment).inside { | |
sh config.mainScript | |
} | |
stage 'post' | |
sh config.postScript | |
} | |
} | |
/* | |
And this is how to invoke it from a Jenkinsfile that includes it | |
dockerBuild { | |
environment = 'golang:1.5.0' | |
mainScript = ''' | |
go version | |
go build -v hello-world.go | |
''' | |
postScript = ''' | |
ls -l | |
./hello-world | |
''' | |
} | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!groovy | |
node { | |
stage 'checkout' | |
checkout scm | |
stage 'build' | |
if (isUnix()) { | |
sh './gradlew clean build' | |
} else { | |
bat './gradlew.bat clean build' | |
} | |
stage 'test' | |
if (isUnix()) { | |
sh './gradlew test' | |
} else { | |
bat './gradlew.bat test' | |
} | |
step([$class: 'JUnitResultArchiver', testResults: '**/target/test-results/TEST-*.xml']) | |
stage 'package' | |
if (isUnix()) { | |
sh './gradlew bootRepackage' | |
} else { | |
bat './gradlew.bat bootRepackage' | |
} | |
step([$class: 'ArtifactArchiver', artifacts: '**/build/libs/*.war', fingerprint: true]) | |
stage: 'postbuild' | |
if (isUnix()) { | |
sh 'ls -la' | |
} else { | |
bat 'dir' | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!groovy | |
# Best of Jenkinsfile | |
# `Jenkinsfile` is a groovy script DSL for defining CI/CD workflows for Jenkins | |
node { | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Jenkinsfile | |
# Build and test a Maven project | |
node { | |
git url: 'https://github.com/user/repo.git' | |
def mvnHome = tool 'M3' | |
sh "${mvnHome}/bin/mvn -B -Dmaven.test.failure.ignore verify" | |
step([$class: 'JUnitResultArchiver', testResults: | |
'**/target/foobar/TEST-*.xml']) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Jenkinsfile | |
# Verify a Maven project | |
node { | |
git url: 'https://github.com/user/repo.git' | |
def mvnHome = tool 'M3' | |
sh "${mvnHome}/bin/mvn -B verify" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
stage "unit test" | |
node { | |
git "[email protected]:michaelneale/oaks-trail-ride.git" | |
sh "echo unit test app" | |
} | |
stage "test on supported OSes" | |
parallel ( | |
windows: { node { | |
sh "echo building on windows now" | |
}}, | |
mac: { node { | |
sh "echo building on mac now" | |
}} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
node { | |
for (int i=0; i< 2; ++i) { | |
stage "Stage #"+i | |
print 'Hello, world $i!' | |
} | |
stage "Stage Parallel" | |
def branches = [:] | |
for (int i = 0; i < numHelloMessages.toInteger(); i++) { | |
branches["split${i}"] = { | |
stage "Stage parallel- #"+i | |
node('remote') { | |
echo 'Starting sleep' | |
sleep 10 | |
echo 'Finished sleep' | |
} | |
} | |
} | |
parallel branches | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment