Skip to content

Instantly share code, notes, and snippets.

@J00MZ
Created January 29, 2019 16:32
Show Gist options
  • Save J00MZ/82a3789dbf5e0773badfd4b113d91b40 to your computer and use it in GitHub Desktop.
Save J00MZ/82a3789dbf5e0773badfd4b113d91b40 to your computer and use it in GitHub Desktop.
Jenkinsfile wait example
stage ('Copy jar to cluster') {
steps {
script {
node ('AWS_AGENT') {
the_env = "${env.Environment}".toLowerCase()
attempt = 0
if (the_env == 'prd'){
// set wait before copy for maximum 20 minutes
max_attempts = 20
}
else{ // Dev or QA have shorter jobs so 10 minutes may be enough to copy jar to cluster
max_attempts = 10
}
retry(max_attempts){
attempt = attempt+1
println "[INFO] Attempting copy jar to cluster #${attempt}, will try for a maximum of ${max_attempts} times and sleep for a minute in between"
sleep(time: 1, unit: 'MINUTES')
build job: 'AWS-EMR-MASTER-CMD',
parameters: [string (name: 'environment', value: "${env.Environment}"),
string (name: 'cluster_id', value: "${cluster_id}"),
string (name: 'CMD', value: "COPY_JAR")]
}
}
}
}
}
stage ('Wait for FINITO') {
steps {
script {
node ('Linux') {
withCredentials([string(credentialsId: '6dc330df-5eca-46f3-97bb-61c09798ad04', variable: 'VAULT_TOKEN')]) {
checkout changelog: false, poll: false, scm: [$class: 'GitSCM', branches: [[name: 'refs/heads/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '6636951d-b5a9-4e26-955a-651015d0b0dc', url: 'ssh://[email protected]:22/DefaultCollection/ControlUp.2.0/_ssh/cudevops']]]
the_env = "${env.Environment}".toLowerCase()
is_finito = 'UNKNOWN'
if (the_env == 'prd'){
// set wait time in minutes for current Digest to complete
max_attempts = 120
}
else{ // Dev or QA have shorter jobs
max_attempts = 15
}
for( i = 1; i <= max_attempts; i++) {
println "[INFO] Attempting check #${i}, will try for a maximum of ${max_attempts} times and sleep for a minute in between"
sleep(time: 1, unit: 'MINUTES')
is_finito = sh(returnStdout: true, script: """
set +x
export VAULT_URL='http://localhost:8200'
export VAULT_TOKEN=\"${VAULT_TOKEN}\"
cd Infra/AWS/EMR-Creation/EMR-Creation/EMR-Recycle/DB
FINITO=\$(python ./emr_db_manage.py -m check -e \"${the_env}\")
echo \$FINITO
""").replaceAll("\\s",'') as Integer
if (is_finito == 1 ){
println "[INFO] FINITO value is ${is_finito}"
println '[INFO] Job FINITO!'
break
}
}
if (is_finito == 0){
println "[ERROR] FINITO value is ${is_finito}, job not stopped"
println '[INFO] Job still failed to update DB it stopped!'
println "[INFO] Make sure to Terminate new cluster ${cluster_id}"
currentBuild.result = 'FAILURE'
sh "exit 1"
}
if (is_finito == 'UNKNOWN'){
println "[ERROR] FINITO value ${is_finito} is UNKNOWN"
println "[INFO] Make sure to Terminate new cluster ${cluster_id}"
currentBuild.result = 'FAILURE'
sh "exit 1"
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment