Created
July 3, 2017 21:06
-
-
Save vfarcic/aeb332b2ab889a81377833f904148d10 to your computer and use it in GitHub Desktop.
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
pipeline { | |
agent { | |
label "prod" | |
} | |
parameters { | |
string( | |
name: "service", | |
defaultValue: "", | |
description: "The name of the service that should be scaled" | |
) | |
string( | |
name: "scale", | |
defaultValue: "", | |
description: "Number of replicas to add or remove." | |
) | |
} | |
stages { | |
stage("Scale") { | |
steps { | |
script { | |
def inspectOut = sh( | |
script: "docker service inspect $service", | |
returnStdout: true | |
) | |
def inspectJson = readJSON text: inspectOut.trim() | |
def currentReplicas = inspectJson[0].Spec.Mode.Replicated.Replicas | |
def newReplicas = currentReplicas + scale.toInteger() | |
def minReplicas = inspectJson[0].Spec.Labels["com.df.scaleMin"].toInteger() | |
def maxReplicas = inspectJson[0].Spec.Labels["com.df.scaleMax"].toInteger() | |
if (newReplicas > maxReplicas) { | |
error "$service is already scaled to the maximum number of $maxReplicas replicas" | |
} else if (newReplicas < minReplicas) { | |
error "$service is already descaled to the minimum number of $minReplicas replicas" | |
} else { | |
sh "docker service scale $service=$newReplicas" | |
echo "$service was scaled from $currentReplicas to $newReplicas replicas" | |
} | |
} | |
} | |
} | |
} | |
post { | |
failure { | |
slackSend( | |
color: "danger", | |
message: """$service could not be scaled. | |
Please check Jenkins logs for the 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