diff --git a/Jenkinsfile b/Jenkinsfile index cfb80755..912d5961 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -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 { @@ -37,7 +31,7 @@ pipeline { } stages { - stage('Cleanup') { + stage('Cleanup & Setup') { steps { sh './gradlew clean' @@ -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? } } } @@ -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}" @@ -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" } } @@ -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}") } } } @@ -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) } } } diff --git a/docker-compose.run-tests.yml b/docker-compose.run-tests.yml index 17427b23..e1ca8571 100644 --- a/docker-compose.run-tests.yml +++ b/docker-compose.run-tests.yml @@ -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" ]