Skip to content

Commit

Permalink
adapt jenkinsfile
Browse files Browse the repository at this point in the history
  • Loading branch information
zingmane committed Nov 29, 2023
1 parent 0b2857f commit c9bbfd9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 34 deletions.
61 changes: 29 additions & 32 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
@Library('campudus-jenkins-shared-lib') _

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

def slackParams = { GString message, String color ->
[
tokenCredentialId : "${env.SLACK_GRUD_INTEGRATION_ID}",
channel : "#grud",
color : color,
message : message
]
}
// SLACK_CHANNEL = "#grud"
SLACK_CHANNEL = "D0657HFLCM9"

def getTriggeringUser = env.BUILD_USER ? env.BUILD_USER : { sh (
script: 'git --no-pager show -s --format=%an',
returnStdout: true
).trim()
}
// flag deactivate tests for fast redeployment from jenkins frontend
shouldTest = true

pipeline {
agent any

environment {
GIT_HASH = sh (returnStdout: true, script: 'git log -1 --pretty=%h').trim()
BUILD_DATE = sh(returnStdout: true, script: 'date \"+%Y-%m-%d %H:%M:%S\"').trim()
GIT_COMMIT_DATE = sh(returnStdout: true, script: "git show -s --format=%ci").trim()
CLEAN_GIT_BRANCH = sh(returnStdout: true, script: "echo $GIT_BRANCH | sed 's/[\\.\\/\\_\\#]/-/g'").trim()
COMPOSE_PROJECT_NAME = "${IMAGE_NAME}-${CLEAN_GIT_BRANCH}"
}

triggers {
Expand All @@ -37,7 +31,7 @@ pipeline {
}

stages {
stage('Cleanup') {
stage('Cleanup & Setup') {
steps {
sh './gradlew clean'

Expand All @@ -50,27 +44,31 @@ pipeline {
}
}

stage('Assemble test classes') {
steps {
sh './gradlew testClasses'
}
}

stage('Assemble') {
stage('Assemble classes & Assemble test classes') {
steps {
sh './gradlew assemble'
sh "docker build -t ${IMAGE_NAME}-builder --target=builder ."
}
}

stage('Test') {
when {
expression { shouldTest }
}
steps {
script {
try {
configFileProvider([configFile(fileId: 'grud-backend-build', targetLocation: 'conf-test.json')]) {
sh './gradlew test'
sh """
TEST_IMAGE=${IMAGE_NAME}-builder \
CLEAN_GIT_BRANCH=${CLEAN_GIT_BRANCH}-build-${BUILD_NUMBER} \
docker-compose -f ./docker-compose.run-tests.yml up \
--abort-on-container-exit --exit-code-from grud-backend
"""
}
} finally {
junit '**/build/test-results/test/TEST-*.xml' //make the junit test results available in any case (success & failure)
sh "docker cp grud-backend-branch-${CLEAN_GIT_BRANCH}-build-${BUILD_NUMBER}:/usr/src/app/build ./build/"
junit './build/test-results/test/TEST-*.xml' //make the junit test results available in any case (success & failure)
// TODO coverage?
}
}
}
Expand All @@ -83,9 +81,10 @@ pipeline {
--label "GIT_COMMIT=${GIT_COMMIT}" \
--label "GIT_COMMIT_DATE=${GIT_COMMIT_DATE}" \
--label "BUILD_DATE=${BUILD_DATE}" \
-t ${IMAGE_NAME}:${DOCKER_BASE_IMAGE_TAG}-${GIT_HASH} \
-t ${IMAGE_NAME}:${DOCKER_BASE_IMAGE_TAG}-${GIT_COMMIT} \
-t ${IMAGE_NAME}:latest \
-f Dockerfile --rm .
-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 @@ -102,7 +101,7 @@ pipeline {
stage('Push to docker registry') {
steps {
withDockerRegistry([ credentialsId: "dockerhub", url: "" ]) {
sh "docker push ${IMAGE_NAME}:${DOCKER_BASE_IMAGE_TAG}-${GIT_HASH}"
sh "docker push ${IMAGE_NAME}:${DOCKER_BASE_IMAGE_TAG}-${GIT_COMMIT}"
sh "docker push ${IMAGE_NAME}:latest"
}
}
Expand All @@ -114,8 +113,7 @@ pipeline {
wrap([$class: 'BuildUser']) {
script {
sh "echo successful"
slackSend(slackParams("""Build successful: <${BUILD_URL}|${env.JOB_NAME} @ \
${env.BUILD_NUMBER}> (${getTriggeringUser()})""", "good"))
slackOk(channel: SLACK_CHANNEL, message: "Image pushed to docker registry: ${IMAGE_NAME}:${DOCKER_BASE_IMAGE_TAG}-${GIT_COMMIT}")
}
}
}
Expand All @@ -124,8 +122,7 @@ pipeline {
wrap([$class: 'BuildUser']) {
script {
sh "echo failed"
slackSend(slackParams("""Build failed: <${BUILD_URL}|${env.JOB_NAME} @ \
${env.BUILD_NUMBER}> (${getTriggeringUser()})""", "danger"))
slackError(channel: SLACK_CHANNEL)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ services:
depends_on:
- postgres
volumes:
# - ./conf-test.json:/usr/src/app/conf-test.json # as provided by Jenkins config file provider
- ./conf-jenkins.json:/usr/src/app/conf-test.json
- ./conf-test.json:/usr/src/app/conf-test.json # as provided by Jenkins config file provider
# - ./conf-jenkins.json:/usr/src/app/conf-test.json
# command: ["ls", "-rtla"]
command: ["gradle", "test", "--tests", "*changeSimpleValue_booleanInitHistoryWithSameValue*", "--info" ]
# command: ["gradle", "test" ]
Expand Down

0 comments on commit c9bbfd9

Please sign in to comment.