This repository has been archived by the owner on Jul 18, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathJenkinsfile
100 lines (94 loc) · 4.09 KB
/
Jenkinsfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
pipeline {
agent any
options {
ansiColor('xterm')
buildDiscarder(logRotator(numToKeepStr: '5', artifactNumToKeepStr: '5'))
}
environment {
GIT_COMMIT_SHORT = sh(
script: "printf \$(git rev-parse --short ${GIT_COMMIT})",
returnStdout: true
)
POM_VERSION = sh(
script: "cat pom.xml | yq -p xml '.project.version'",
returnStdout: true
).trim()
BRANCH_NAME_NORMALIZED = sh(
script: "echo '${BRANCH_NAME}' | sed s#/#-#g",
returnStdout: true
).trim()
DISPLAY_VERSION = sh(
script: "echo '${POM_VERSION}' | sed 's/SNAPSHOT/$BRANCH_NAME_NORMALIZED/'",
returnStdout: true
).trim()
}
stages {
stage('Log variables') {
steps {
echo "Maven project version: $POM_VERSION"
echo "Git branch: $BRANCH_NAME"
echo "Git branch (normalized): $BRANCH_NAME_NORMALIZED"
echo "Git commit: $GIT_COMMIT"
echo "Git commit (abbreviated): $GIT_COMMIT_SHORT"
echo "displayed version: $DISPLAY_VERSION"
}
}
stage('Maven test') {
steps {
sh 'mvn clean test'
}
post {
success {
junit 'target/surefire-reports/**/*.xml'
}
}
}
// for all builds: a Docker image tagged with the abbreviated Git commit # is built and pushed
stage('Build and push Docker image') {
steps {
withCredentials([usernamePassword(credentialsId: 'DOCKER_PUBLISHER', passwordVariable: 'DOCKER_PASSWORD', usernameVariable: 'DOCKER_USER')]) {
sh '''
T2_DISPLAY_VERSION="$DISPLAY_VERSION" mvn clean package -DskipTests
docker login -u "$DOCKER_USER" -p "$DOCKER_PASSWORD" docker.stackable.tech
docker push docker.stackable.tech/t2:"$GIT_COMMIT_SHORT"
docker logout docker.stackable.tech
'''
}
}
}
// The Docker image is tagged with a version-label for ...
// - ... all SNAPSHOT builds
// - ... RELEASE-builds ONLY on main branch (to prevent releasing from other branches)
stage('Docker: tag with version/branch label') {
when {
expression { env.POM_VERSION.contains('-SNAPSHOT') || env.BRANCH_NAME=='main' }
}
steps {
withCredentials([usernamePassword(credentialsId: 'DOCKER_PUBLISHER', passwordVariable: 'DOCKER_PASSWORD', usernameVariable: 'DOCKER_USER')]) {
sh '''
docker tag docker.stackable.tech/t2:"$GIT_COMMIT_SHORT" docker.stackable.tech/t2:"$DISPLAY_VERSION"
docker login -u "$DOCKER_USER" -p "$DOCKER_PASSWORD" docker.stackable.tech
docker push docker.stackable.tech/t2:"$DISPLAY_VERSION"
docker logout docker.stackable.tech
'''
}
}
}
// The Docker image is tagged as 'latest' ONLY IF the version is not a SNAPSHOT version AND we build the main branch
stage('Docker: tag latest') {
when {
expression { !env.POM_VERSION.contains('-SNAPSHOT') && env.BRANCH_NAME=='main' }
}
steps {
withCredentials([usernamePassword(credentialsId: 'DOCKER_PUBLISHER', passwordVariable: 'DOCKER_PASSWORD', usernameVariable: 'DOCKER_USER')]) {
sh '''
docker login -u "$DOCKER_USER" -p "$DOCKER_PASSWORD" docker.stackable.tech
docker tag docker.stackable.tech/t2:$GIT_COMMIT_SHORT docker.stackable.tech/t2:latest
docker push docker.stackable.tech/t2:latest
docker logout docker.stackable.tech
'''
}
}
}
}
}