Skip to content

Instantly share code, notes, and snippets.

@abdullahainun
Created November 19, 2021 09:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abdullahainun/8aaf9cf2699dc4e722a2dbf545e52b50 to your computer and use it in GitHub Desktop.
Save abdullahainun/8aaf9cf2699dc4e722a2dbf545e52b50 to your computer and use it in GitHub Desktop.
node {
env.WORKSPACE = pwd()
def now = new Date()
def major = "1"
def month = now.format("M", TimeZone.getTimeZone('UTC'))
def day = now.format("d", TimeZone.getTimeZone('UTC'))
def week = (int)((Integer.parseInt(day,10) - 1) / 7 + 1)
def version = "v"+major+"."+month+"."+week
def name = "microservice-transaksi"
def branch = "master"
def namespace = "production"
try {
stage('Checking'){
sh "echo ${version}"
}
stage('Cloning Source code') { // for display purposes
notifyBuild('Cloning Source code','STARTED')
git branch: "${branch}", credentialsId: 'abdullah.ainun4', url: "git@gitlab.com:umrahaj/${name}.git"
}
stage('Releasing Repository'){
sh "sed -i 's/{{ VERSION }}/${version}/g' .helm/${name}/Chart.yaml"
sh "sed -i 's/{{ VERSION }}/${version}/g' .helm/${name}/values.yaml"
sh "helm lint .helm/${name}"
sh "git add ."
sh "git commit -m 'Release Version ${version}'"
sh "git tag -a ${version} -m 'Release Version ${version}' || true"
sh "git push origin --delete ${version} || true"
sh "git push origin ${version}"
}
stage('Build and push image'){
sh "docker image rm registry.gitlab.com/umrahaj/${name}:${version} || true"
sh "docker build -t registry.gitlab.com/umrahaj/${name}:${version} ."
sh "docker push registry.gitlab.com/umrahaj/${name}:${version}"
currentBuild.result = "SUCCESS"
notifyBuild('Build and push image', 'SUCCESS')
}
stage('Deploy Prodution'){
// sh "helm delete --purge backend-microservice-master"
// sh "helm install --name backend-microservice-master backend/microservice-master --namespace ${namespace}"
sh "kubectl delete deploy ${name}-${version} -n ${namespace} || true"
sh "helm upgrade backend-${name} .helm/${name}"
currentBuild.result = "SUCCESS"
notifyBuild('Deploy Production', 'SUCCESS')
}
} catch (e) {
// If there was an exception thrown, the build failed
currentBuild.result = "FAILED"
throw e
} finally {
// Success or failure, always send notifications
notifyBuild('finaly',currentBuild.result)
}
}
// define the slack notify build function
def notifyBuild(def stage, def buildStatus){
// set default of build status
buildStatus = buildStatus ?: 'STARTED'
env.JOB_DISPLAYNAME = Jenkins.instance.getJob("${env.JOB_NAME}").displayName
env.PREVIOUS_BUILD_RESULT = currentBuild.rawBuild.getPreviousBuild()?.getResult().toString()
def colorMap = [ 'STARTED': '#F0FFFF', 'SUCCESS': '#008B00', 'FAILURE': '#FF0000' ]
// Define messages contents
def subject = "Pipeline: ${env.JOB_DISPLAYNAME} - on stage ${stage} -#${env.BUILD_NUMBER} ${buildStatus}"
def summary = "${subject} (${env.BUILD_URL})"
def colorName = colorMap[buildStatus]
slackSend (color: colorName, message: summary)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment