Skip to content

Instantly share code, notes, and snippets.

@uaarkoti
Last active September 9, 2016 07:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save uaarkoti/277802a6ee29b18debaa to your computer and use it in GitHub Desktop.
Save uaarkoti/277802a6ee29b18debaa to your computer and use it in GitHub Desktop.
Sample Workflow
// Run this part of the job on a node with a name or label of 'linux-64bit'
node('linux-64bit') {
// Checkout my source
git url: '/var/lib/jenkins/workflow-plugin-pipeline-demo'
// Set a particular maven install to use for this build
env.PATH="${tool 'Maven 3.x'}/bin:${env.PATH}"
// Start 'Dev' stage
stage 'Dev'
// Execute maven build
sh 'mvn -o clean package'
// Archive the artifacts from the above build
archive 'target/x.war'
stage 'QA'
// Start two tests (longerTests, quickerTests) in parallel
parallel(longerTests: {
runWithServer {url ->
sh "mvn -o -f sometests/pom.xml test -Durl=${url} -Dduration=30"
}
}, quickerTests: {
runWithServer {url ->
sh "mvn -o -f sometests/pom.xml test -Durl=${url} -Dduration=20"
}
})
// Start a new stage and make sure only one build can enter this stage
stage name: 'Staging', concurrency: 1
// Call deploy function defined below
deploy 'target/x.war', 'staging'
}
// Pause this workflow for human interaction
input message: "Does http://localhost:8080/staging/ look good?"
// Start a try catch block
try {
/**
* Assuming significant time was spent up until this point, establish a
* safe point so this build can be run from here if this build fails after
* this.
*/
checkpoint('Before production')
} catch (NoSuchMethodError _) {
// Since Checkpoints is a feature of CloudBees jenkins enterprise, log it
// and continue with the rest of the workflow
echo 'Checkpoint feature available in Jenkins Enterprise by CloudBees.'
}
stage name: 'Production', concurrency: 1
// Run this part of the job on a node with name or label of 'windows-32bit'
node('windows-32bit') {
// Exceute a command
sh 'wget -O - -S http://localhost:8080/staging/'
unarchive mapping: ['target/x.war' : 'x.war']
deploy 'x.war', 'production'
echo 'Deployed to http://localhost:8080/production/'
}
/**
* Function to deploy artifacts
*/
def deploy(war, id) {
sh "cp ${war} /tmp/webapps/${id}.war"
}
/**
* Function to undeploy artifacts
*/
def undeploy(id) {
sh "rm /tmp/webapps/${id}.war"
}
/**
* Function to create a random Id for testing
*/
def runWithServer(body) {
def id = UUID.randomUUID().toString()
deploy 'target/x.war', id
try {
body.call "http://localhost:8080/${id}/"
} finally {
undeploy id
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment