Update Jenkinsfile

This commit is contained in:
Marija Stopa 2025-01-08 18:16:07 +01:00 committed by GitHub
parent aab630d6a3
commit 522d1c380e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

27
Jenkinsfile vendored
View file

@ -1,3 +1,5 @@
#!/usr/bin/env groovy
pipeline {
agent any
@ -10,18 +12,15 @@ pipeline {
stage('Prepare') {
steps {
script {
// Grab branch name
env.GIT_BRANCH_NAME = sh(script: "git rev-parse --abbrev-ref HEAD", returnStdout: true).trim()
// Grab short commit
// Use Jenkins environment variable or fallback to git command
env.GIT_BRANCH_NAME = env.BRANCH_NAME ?: sh(script: "git rev-parse --abbrev-ref HEAD", returnStdout: true).trim()
env.GIT_COMMIT_SHORT = sh(script: "git rev-parse --short HEAD", returnStdout: true).trim()
// Add this line for branch name confirmation
echo "Detected Branch: ${env.GIT_BRANCH_NAME}"
echo "Branch: ${env.GIT_BRANCH_NAME}"
echo "Commit: ${env.GIT_COMMIT_SHORT}"
}
}
}
stage('Checkstyle') {
when {
expression { return env.GIT_BRANCH_NAME != 'main' }
@ -29,10 +28,11 @@ pipeline {
steps {
echo 'Running Checkstyle...'
sh "./gradlew checkstyleMain checkstyleTest"
// Archive checkstyle reports as artifacts
archiveArtifacts artifacts: 'build/reports/checkstyle/*.xml', fingerprint: true
}
}
stage('Test') {
when {
expression { return env.GIT_BRANCH_NAME != 'main' }
@ -42,7 +42,7 @@ pipeline {
sh "./gradlew test"
}
}
stage('Build without Tests') {
when {
expression { return env.GIT_BRANCH_NAME != 'main' }
@ -52,14 +52,17 @@ pipeline {
sh "./gradlew clean build -x test"
}
}
stage('Docker Build & Push') {
steps {
script {
// Decide which repo to push to based on the branch
if (env.GIT_BRANCH_NAME == 'main') {
// Main branch
echo "Building Docker image for main-jenkins repo..."
sh "docker build -t ${DOCKERHUB_USERNAME}/main-jenkins:${GIT_COMMIT_SHORT} ."
// Login & push
withCredentials([usernamePassword(
credentialsId: "${DOCKERHUB_CREDENTIALS}",
usernameVariable: 'DOCKER_USER',
@ -67,11 +70,13 @@ pipeline {
)]) {
sh "echo $DOCKER_PASS | docker login -u $DOCKER_USER --password-stdin"
}
sh "docker push ${DOCKERHUB_USERNAME}/main-jenkins:${GIT_COMMIT_SHORT}"
sh "docker tag ${DOCKERHUB_USERNAME}/main-jenkins:${GIT_COMMIT_SHORT} ${DOCKERHUB_USERNAME}/main-jenkins:latest"
sh "docker push ${DOCKERHUB_USERNAME}/main-jenkins:latest"
} else {
// Feature or merge-request branch
echo "Building Docker image for mr-jenkins repo..."
sh "docker build -t ${DOCKERHUB_USERNAME}/mr-jenkins:${GIT_COMMIT_SHORT} ."