@Library('atlas-pipeline-library@2.16.2') _ def operations = [ bygg : 'Bygg', release: 'Release', ] pipeline { agent { label 'java-17' } options { timeout(time: 10, unit: 'MINUTES') } parameters { choice(name: 'operations', choices: operations.values() as List, description: 'Bygg lager et nytt artefakt og pusher det opp til Atlas. Release oppdaterer også versjonen til ny semantisk versjon.') choice(name: 'releaseType', choices: ['snapshot', 'major', 'minor', 'patch'], description: 'Gjelder kun når valgt \'operations\' er \'release\'. Oppdaterer versjonsnummer i pom.xml til ny versjon.') string(name: 'commitMessage', defaultValue: '', description: 'Angi commit melding for release commit. Gjelder kun når valgt \'operations\' er \'release\'.') } environment { HOME = "${env.WORKSPACE}" APP_NAME = 'mf-common' IKTLOSNING = 'melding' CONTINUE_PIPELINE = 'true' } stages { stage('Stop pipeline if commit by tjenestebruker') { when { expression { isCommitByTjenestebruker() && params.operations == operations.bygg } } steps { script { echo "Commit was made by user 'tjemfbitbucket'. Skipping remaining pipeline stages." CONTINUE_PIPELINE = 'false' } } } stage('Set responsible user') { when { expression { CONTINUE_PIPELINE == 'true' } } steps { script { env.ATLAS_RESPONSIBLE_USER = getAtlasResponsibleUser() } } } stage('Print selected parameters') { when { expression { return CONTINUE_PIPELINE == 'true' } } steps { echo "Operasjon: ${params.operations}" echo "Version number for deploy: ${params.versionToDeploy}" echo "Release type: ${params.releaseType}" } } stage('Download Maven Cache') { when { expression { CONTINUE_PIPELINE == 'true' } } steps { downloadMavenCache() } } stage('Get App Version') { when { expression { CONTINUE_PIPELINE == 'true' } } steps { script { echo 'Getting version from pom.xml...' env.VERSJON_FOR_DEPLOY = getMavenProperty('project.version') echo "VERSJON_FOR_DEPLOY = ${env.VERSJON_FOR_DEPLOY}" } } } stage('Version Bump and Commit') { when { expression { CONTINUE_PIPELINE == 'true' && params.operations == operations.release } } steps { script { env.newVersion = bumpVersion(VERSJON_FOR_DEPLOY, params.releaseType).trim() } echo "Starting to do git commiting ... Variables needed are RELEASE_TYPE='${params.releaseType}', " + "currentVersion='${VERSJON_FOR_DEPLOY}', newVersion='${env.newVersion}'" + "and commit message: '${params.commitMessage}'" commitBumpedVersion(params.releaseType, VERSJON_FOR_DEPLOY, env.newVersion, params.commitMessage, env.BRANCH_NAME) echo "Done doing git commiting." script { env.VERSJON_FOR_DEPLOY = env.newVersion } } } stage('Build Application') { when { expression { CONTINUE_PIPELINE == 'true' && params.operations in [operations.bygg, operations.release] } } steps { sh 'mvn --batch-mode install -DskipDatabaseTests=true' } post { always { junit '**/target/*-reports/TEST-*.xml' } } } stage('Stop if PR branch') { when { expression { CONTINUE_PIPELINE == 'true' } } steps { script { def isPRBranch = env.BRANCH_NAME ==~ /.*PR-.*/ if (isPRBranch) { echo "Detected PR branch: ${env.BRANCH_NAME}. Skipping remaining stages." CONTINUE_PIPELINE = 'false' } } } } stage('Push to Artrepo') { when { expression { CONTINUE_PIPELINE == 'true' && params.operations in [operations.bygg, operations.release] } } steps { rtMavenDeployer id: 'jfrog-maven-deployer', serverId: 'svv-artrepo', releaseRepo: 'libs-release-local', snapshotRepo: 'libs-snapshot-local' rtMavenRun deployerId: 'jfrog-maven-deployer', pom: 'pom.xml', goals: '-B deploy -DskipTests' } } stage('Upload Maven Cache') { when { expression { CONTINUE_PIPELINE == 'true' } } steps { uploadMavenCache() } } } post { always { script { atlasCleanConfig() } } } } def isCommitByTjenestebruker() { def author = sh(script: "git log -1 --pretty=format:'%an'", returnStdout: true).trim() return author == 'tjemfbitbucket' } def bumpVersion(String currentVersion, String releaseType) { return sh(script: """ baseVersion=\$(echo "${currentVersion}" | sed 's/-SNAPSHOT//') major=\$(echo "\$baseVersion" | cut -d. -f1) minor=\$(echo "\$baseVersion" | cut -d. -f2) patch=\$(echo "\$baseVersion" | cut -d. -f3) case "${releaseType}" in snapshot) isSnapshot=true ;; *) isSnapshot=false ;; esac case "\$isSnapshot" in true) newVersion="\${major}.\${minor}.\$((patch + 1))-SNAPSHOT" ;; false) case "${releaseType}" in major) major=\$((major + 1)); minor=0; patch=0;; minor) minor=\$((minor + 1)); patch=0;; patch) if [[ ! "${currentVersion}" == *-SNAPSHOT ]]; then patch=\$patch else patch=\$((patch + 1)) fi ;; esac newVersion="\${major}.\${minor}.\${patch}" ;; esac echo "\$newVersion" """, returnStdout: true).trim() } def commitBumpedVersion(String releaseType, String currentVersion, String newVersion, String commitMessage, String branchName) { withCredentials([ usernamePassword(credentialsId: 'mf_git', usernameVariable: 'GIT_USER', passwordVariable: 'GIT_PASS'), ]) { sh """ #!/usr/bin/env bash set -eux RELEASE_TYPE=${releaseType} currentVersion=${currentVersion} newVersion="${newVersion}" mvn versions:set -DnewVersion="\$newVersion" -DprocessAllModules -DgenerateBackupPoms=false mvn versions:commit git config user.email "no-reply@vegvesen.no" git config user.name "${GIT_USER}" git add . case "\$newVersion" in *-SNAPSHOT) git commit -m "CI: \$currentVersion -> \$newVersion" ;; *) if [ "\$RELEASE_TYPE" = "major" ]; then git commit -m "CI!: ${commitMessage}, \$currentVersion -> \$newVersion" else git commit -m "CI: ${commitMessage}, \$currentVersion -> \$newVersion" fi ;; esac git add . git commit --amend --no-edit if [[ ! "\$newVersion" == *-SNAPSHOT ]]; then git tag "v\$newVersion" fi echo "Pushing to branch: ${branchName}" git config --global credential.helper cache """ sh ''' echo "protocol=https" >> ~/.git-credentials echo "host=git.vegvesen.no" >> ~/.git-credentials echo "username=${GIT_USER}" >> ~/.git-credentials echo "password=${GIT_PASS}" >> ~/.git-credentials git credential approve < ~/.git-credentials rm ~/.git-credentials ''' sh "git push https://git.vegvesen.no/scm/mf/${APP_NAME}.git HEAD:${branchName} --tags" } }