Skip to content

Instantly share code, notes, and snippets.

@rodrigc
Last active August 26, 2021 23:50
Show Gist options
  • Save rodrigc/1298c4249b8be19a9d6cfab5008ce301 to your computer and use it in GitHub Desktop.
Save rodrigc/1298c4249b8be19a9d6cfab5008ce301 to your computer and use it in GitHub Desktop.
#!groovy
pipeline {
agent {
label 'CRAIG-1'
}
options {
disableConcurrentBuilds()
timeout(time: 10, unit: 'HOURS')
}
parameters {
booleanParam(name: 'UPDATE_PARAMETERS',
defaultValue: false,
description: 'Update the parameters from this pipeline script')
string(defaultValue: 'master',
description: 'branch',
name: 'BRANCH')
}
stages {
stage("Display build parameters") {
steps {
script {
/*
* Print out the build parameters
*/
def all_params = ""
for ( k in params ) {
all_params = all_params + "${k.key}=${k.value}\n"
}
print("These parameters were passed to this build:\n" + all_params)
writeFile(file: "env-vars.txt", text: "$all_params")
}
}
}
/*
* Jenkins needs to parse the entire pipeline before it can
* parse the parameters, if the parameters are specified in this file.
*/
stage("Updating Parameters") {
when {
expression {
params.UPDATE_PARAMETERS == true
}
}
steps {
script {
currentBuild.result = 'ABORTED'
error('DRY RUN COMPLETED. JOB PARAMETERIZED.')
}
}
}
stage("First") {
steps {
dir("dir1") {
git(url: 'https://github.com/twisted/twisted')
sh("""
echo do some stuff
""")
}
}
}
stage("Second") {
steps {
dir("dir2") {
git(url: 'https://github.com/twisted/twisted')
sh("""
echo do more stuff
""")
}
}
}
stage("Third: Takes a long time, over 1.5 hours") {
steps {
sh("""
echo this operation takes a long time
""")
}
post {
always {
junit "report.xml"
}
}
}
}
post {
failure {
slackSend (channel: '#channel-alerts', color: '#FF0000', message: "FAILED: Job '${env.JOB_NAME} started by ${env.CAUSEDBY} [${env.BUILD_NUMBER}]' (${env.RUN_DISPLAY_URL})");
}
changed {
script {
/*
* Only send e-mails on failures, or when status changes from failure
* to success, or success to failure.
* This requires currentBuild.result to be set.
*
* See: https://baptiste-wicht.com/posts/2017/06/jenkins-tip-send-notifications-fixed-builds-declarative-pipeline.html
*/
def prevBuild = currentBuild.getPreviousBuild()
/*
* If this pipeline has never run before, then prevBuild will be null.
*/
if (prevBuild == null) {
return
}
def prevResult = prevBuild.getResult()
def result = currentBuild.getResult()
if ("${prevResult}" != "${result}" && "${result}" != "FAILURE") {
if ("${prevResult}" == "FAILURE") {
slackSend(channel: '#smoketest-alerts', color: 'good', message: "SUCCEEDED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.RUN_DISPLAY_URL})")
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment