Skip to content

Commit

Permalink
adjusted Jenkinsfile for branch deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaroslav Bobrowski committed May 21, 2024
1 parent 8c16a72 commit a6932ef
Showing 1 changed file with 34 additions and 8 deletions.
42 changes: 34 additions & 8 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
@Library('campudus-jenkins-shared-lib') _

final String BRANCH = params.BRANCH
final boolean NOTIFY_SLACK_ON_FAILURE = params.NOTIFY_SLACK_ON_FAILURE
final boolean NOTIFY_SLACK_ON_SUCCESS = params.NOTIFY_SLACK_ON_SUCCESS

final String BRANCH_NAME = BRANCH ? BRANCH.tokenize('/').last() : ""

IMAGE_NAME = "campudus/grud-backend"
DEPLOY_DIR = 'build/libs'
LEGACY_ARCHIVE_FILENAME="grud-backend-docker.tar.gz"
DOCKER_BASE_IMAGE_TAG = "build-${BUILD_NUMBER}"

SLACK_CHANNEL = "#grud"

final boolean isBranch = BRANCH_NAME && BRANCH_NAME != "master"

// flag deactivate tests for fast redeployment from jenkins frontend
shouldTest = true

Expand All @@ -21,14 +29,19 @@ pipeline {
}

triggers {
pollSCM('H/5 * * * *')
githubPush()
}

options {
timestamps()
copyArtifactPermission('*');
}

parameters {
booleanParam(name: 'NOTIFY_SLACK_ON_FAILURE', defaultValue: true, description: '')
booleanParam(name: 'NOTIFY_SLACK_ON_SUCCESS', defaultValue: false, description: '')
}

stages {
stage('Cleanup & Build base docker image') {
steps {
Expand All @@ -45,7 +58,7 @@ pipeline {
groovyVars = [:] << getBinding().getVariables()
groovyVars.each {k,v -> print "$k = $v"}
}

sh "docker build -t ${IMAGE_NAME}-cacher --target=cacher ."
}
}
Expand Down Expand Up @@ -85,10 +98,12 @@ pipeline {
--label "GIT_COMMIT_DATE=${GIT_COMMIT_DATE}" \
--label "BUILD_DATE=${BUILD_DATE}" \
-t ${IMAGE_NAME}:${DOCKER_BASE_IMAGE_TAG}-${GIT_COMMIT} \
-t ${IMAGE_NAME}:latest \
${isBranch ? "-t ${IMAGE_NAME}:${BRANCH_NAME}" : ""} \
${!isBranch ? "-t ${IMAGE_NAME}:latest" : ""} \
-f Dockerfile \
--rm --target=prod .
"""

// Legacy, but needed for some project deployments
sh "docker save ${IMAGE_NAME}:latest | gzip -c > ${DEPLOY_DIR}/${LEGACY_ARCHIVE_FILENAME}"
}
Expand All @@ -105,7 +120,16 @@ pipeline {
steps {
withDockerRegistry([ credentialsId: "dockerhub", url: "" ]) {
sh "docker push ${IMAGE_NAME}:${DOCKER_BASE_IMAGE_TAG}-${GIT_COMMIT}"
sh "docker push ${IMAGE_NAME}:latest"

script {
if (BRANCH_NAME) {
sh "docker push ${IMAGE_NAME}:${BRANCH_NAME}"
}

if (!BRANCH_NAME || BRANCH_NAME == 'master') {
sh "docker push ${IMAGE_NAME}:latest"
}
}
}
}
}
Expand All @@ -115,17 +139,19 @@ pipeline {
success {
wrap([$class: 'BuildUser']) {
script {
sh "echo successful"
slackOk(channel: SLACK_CHANNEL, message: "Image pushed to docker registry: ${IMAGE_NAME}:${DOCKER_BASE_IMAGE_TAG}-${GIT_COMMIT}")
if (NOTIFY_SLACK_ON_SUCCESS) {
slackOk(channel: SLACK_CHANNEL, message: BRANCH ? "BRANCH=${BRANCH}" : "")
}
}
}
}

failure {
wrap([$class: 'BuildUser']) {
script {
sh "echo failed"
slackError(channel: SLACK_CHANNEL)
if (NOTIFY_SLACK_ON_FAILURE) {
slackError(channel: SLACK_CHANNEL, message: BRANCH ? "BRANCH=${BRANCH}" : "")
}
}
}
}
Expand Down

0 comments on commit a6932ef

Please sign in to comment.