pipeline { agent any environment { NEXUS = credentials('jenkins_nexus') MAIL = credentials('robots_mail') NCLAZZ = credentials('nclazz_api_token') DB = credentials('sql01_service') DOCKER_REGISTRY = "docker.nclazz.de" DOCKER_GROUP = 'nclazz-service' DOCKER_IMAGE = 'mail-relay' DOCKER_TAG = "${ BRANCH_NAME == "master" ? "latest" : "develop" }" DOCKER_VERSION = "1.0.0" DEPLOY_ENV = "${ BRANCH_NAME == "master" ? "production" : "staging" }" } options { timeout(time: 30, unit: "MINUTES") } parameters { booleanParam(name: 'RUN_INTEGRATION', defaultValue: false, description: 'Run integration tests.') booleanParam(name: 'RUN_DEPLOYMENT', defaultValue: false, description: 'Run deployment to staging or production environment.') } stages { stage('Run Maven') { agent { docker 'maven:3.8.3-openjdk-11' } stages { stage('Run Maven Build') { steps { sh 'mvn install -DskipTests -B -s settings.xml' stash includes: '**/target/*.jar', name: 'BUILD_ARTIFACTS' } } stage('Run All Tests') { steps { sh 'mvn test -P test -B -s settings.xml' } } } post { always { junit( allowEmptyResults: true, testResults: '**/target/surefire-reports/*.xml, **/target/failsafe-reports/*.xml' ) jacoco( changeBuildStatus: true, exclusionPattern: '**/*Application.class, ' + '**/*Configuration.class', minimumInstructionCoverage: env.MIN_INSTRUCTION_COVERAGE, maximumInstructionCoverage: env.MAX_INSTRUCTION_COVERAGE, minimumClassCoverage: env.MIN_CLASS_COVERAGE, maximumClassCoverage: env.MAX_CLASS_COVERAGE ) recordIssues( enabledForFailure: true, aggregatingResults: true, tools: [java(), checkStyle(), findBugs(pattern: '**/target/spotbugsXml.xml')] ) } } } stage('Run Docker Builds') { agent { label 'docker' } when { anyOf { branch 'master' branch 'develop' } } steps { unstash 'BUILD_ARTIFACTS' script { docker.withRegistry("https://$DOCKER_REGISTRY", "jenkins_nexus") { def apiImage = docker.build("$DOCKER_GROUP/$DOCKER_IMAGE") apiImage.push("${env.DOCKER_VERSION}") apiImage.push("${env.DOCKER_TAG}") } } } } stage('Run Deployment') { agent { label 'provision' } when { allOf { anyOf { branch 'master' expression { return params.RUN_DEPLOYMENT } } } } steps { deployToSwarm( name: 'nclazz-mail-relay', file: "${env.WORKSPACE}/docker-compose.yml", forceUpdate: true, askApproval: !params.RUN_DEPLOYMENT, slackChannel: 'deployment', env: [ PORT: 7006, VERSION: env.DOCKER_VERSION, MAIL_USERNAME: env.MAIL_USR, MAIL_PASSWORD: env.MAIL_PSW, DB_USERNAME: env.DB_USR, DB_PASSWORD: env.DB_PSW, AUTH_TOKEN: env.NCLAZZ ] ) exposeService( name: 'nclazz-email-relay', fqdn: 'api.nclazz.de', description: 'nclazz email relay service', location: '/mail/', backendPort: 7006 ) } } } post { always { cleanWs() } failure { slackSend( channel: "notifications", color: "danger", message: "There is a *build failure* in ${env.JOB_NAME}.\nBuild: ${env.RUN_DISPLAY_URL} " ) } unstable { slackSend( channel: "notifications", color: "warning", message: "Some tests have failed in ${env.JOB_NAME}.\nBuild: ${env.RUN_DISPLAY_URL} " ) } fixed { slackSend( channel: "notifications", color: "good", message: "The build ${env.JOB_NAME} completed successfully and is back to normal.\nBuild: ${env.RUN_DISPLAY_URL} " ) } } }