Skip to content

Instantly share code, notes, and snippets.

@peerapach
Created August 6, 2020 11:52
Show Gist options
  • Save peerapach/02c898fb5865233bffa14158b235b7ce to your computer and use it in GitHub Desktop.
Save peerapach/02c898fb5865233bffa14158b235b7ce to your computer and use it in GitHub Desktop.
blue-green
def gitURL="[email protected]/Account.git"
def appName="acc-prod-bluegreen"
def svcName="acc-prod"
def project="cloud-prod"
def tag="blue"
def altTag="green"
def verbose="false"
try {
timeout(time: 500, unit: 'MINUTES') {
node {
stage("Initialize") {
sh "oc get virtualservice ${svcName} -n ${project} -o jsonpath='{ .spec.http[1].route[0].destination.subset }' --loglevel=4 > activeservice"
activeService = readFile('activeservice').trim()
if (activeService == "blue") {
tag = "green"
altTag = "blue"
}
sh "oc get virtualservice ${svcName} -n ${project} -o jsonpath='{ .spec.hosts[0] }' --loglevel=4 > routehost"
routeHost = readFile('routehost').trim()
}
stage("Tag image") {
openshiftTag srcStream: appName, namespace: project, srcTag: 'latest', destinationStream: appName, destinationTag: tag, verbose: verbose
}
stage("Deploy") {
openshiftDeploy depCfg: "${svcName}-${tag}", namespace: "${project}", verbose: 'false', waitTime: '', waitUnit: 'sec'
openshiftVerifyDeployment depCfg: "${svcName}-${tag}", namespace: "${project}", verbose: 'false'
}
stage("Scale") {
sh "oc get dc ${svcName}-${altTag} -n ${project} -o jsonpath='{ .status.availableReplicas }' --loglevel=4 > availableReplicas"
availableReplicas = readFile('availableReplicas').trim()
openshiftScale depCfg: "${svcName}-${tag}", namespace: "${project}", replicaCount: "${availableReplicas}"
}
stage("Test") {
sh "oc patch virtualservice ${svcName} -n ${project} --type='json' -p='[{\"op\":\"replace\", \"path\": \"/spec/http/0/route/0/destination/subset\", \"value\": \"${tag}\" }]'"
input message: "Test deployment: http://${routeHost}. Approve?", id: "approval"
}
stage("Go Live") {
sh "oc set -n ${project} route-backends ${appName} ${svcName}-${tag}=100 ${svcName}-${altTag}=0 --loglevel=4"
sh "oc patch virtualservice ${svcName} -n ${project} --type='json' -p='[{\"op\":\"replace\", \"path\": \"/spec/http/1/route/0/destination/subset\", \"value\": \"${tag}\" }]'"
openshiftScale depCfg: "${svcName}-${altTag}", namespace: "${project}", replicaCount: "1"
}
}
}
} catch (err) {
node {
sh "oc patch virtualservice ${svcName} -n ${project} --type='json' -p='[{\"op\":\"replace\", \"path\": \"/spec/http/0/route/0/destination/subset\", \"value\": \"${altTag}\" }]'"
openshiftScale depCfg: "${svcName}-${tag}", namespace: "${project}", replicaCount: "1"
}
echo "in catch block"
echo "Caught: ${err}"
currentBuild.result = 'FAILURE'
throw err
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment