Update Jenkinsfile

This commit is contained in:
Marija Stopa 2025-01-17 13:08:19 +01:00 committed by GitHub
parent ce77562a4e
commit 4cccb3af36
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

84
Jenkinsfile vendored
View file

@ -1,58 +1,64 @@
pipeline { pipeline {
agent any agent any
tools {
maven 'Maven 3' // Global Tool Configuration parameters {
string(name: "DOCKERHUB_CREDENTIALS_ID", description: "DockerHub credentials ID")
} }
environment { environment {
DOCKERHUB_USERNAME = credentials('dockerhub') DOCKERHUB_REPO_MR = "mr-jenkins"
DOCKERHUB_PASSWORD = credentials('dockerhub') DOCKERHUB_REPO_MAIN = "main-jenkins"
DOCKER_IMAGE_MR = "marijastopa/mr-jenkins"
DOCKER_IMAGE_MAIN = "marijastopa/main-jenkins"
} }
stages { stages {
stage('Checkstyle') { stage("Checkstyle") {
steps {
echo 'Running Checkstyle...'
sh 'mvn checkstyle:checkstyle'
archiveArtifacts artifacts: '**/target/site/checkstyle.html', fingerprint: true
}
}
stage('Test') {
steps {
echo 'Running Tests...'
sh 'mvn test'
}
}
stage('Build') {
steps {
echo 'Building application without tests...'
sh 'mvn clean package -DskipTests'
}
}
stage('Build and Push Docker Image (Merge Request)') {
when { when {
branch 'develop' expression { env.BRANCH_NAME != 'main' }
} }
steps { steps {
script { withMaven(maven: 'maven-3.9.8') {
def commitHash = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim() sh './mvnw checkstyle:checkstyle -Dcheckstyle.output.file=target/checkstyle-result.xml'
withDockerRegistry([credentialsId: 'dockerhub', url: '']) { }
def image = docker.build("${DOCKER_IMAGE_MR}:${commitHash}") }
image.push() post {
} always {
archiveArtifacts artifacts: 'target/checkstyle-result.xml', allowEmptyArchive: false
} }
} }
} }
stage('Build and Push Docker Image (Main)') {
stage("Test") {
when { when {
branch 'main' expression { env.BRANCH_NAME != 'main' }
} }
steps {
withMaven(maven: 'maven-3.9.8') {
sh './mvnw clean test'
}
}
}
stage("Build") {
when {
expression { env.BRANCH_NAME != 'main' }
}
steps {
withMaven(maven: 'maven-3.9.8') {
sh './mvnw clean package -DskipTests'
}
}
}
stage("Create Docker Image") {
steps { steps {
script { script {
def commitHash = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim() def imageName = "${env.DOCKERHUB_REPO_MR}:${env.GIT_COMMIT.substring(0, 7)}"
withDockerRegistry([credentialsId: 'dockerhub', url: '']) { if (env.BRANCH_NAME == 'main') {
def image = docker.build("${DOCKER_IMAGE_MAIN}:${commitHash}") imageName = "${env.DOCKERHUB_REPO_MAIN}:${env.GIT_COMMIT.substring(0, 7)}"
image.push() }
docker.build(imageName)
docker.withRegistry('https://index.docker.io/v1/', "${params.DOCKERHUB_CREDENTIALS_ID}") {
docker.image(imageName).push()
} }
} }
} }