This repository has been archived by the owner on Feb 1, 2024. It is now read-only.
forked from mbenabda/k8s-grafana-dashboards-controller
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Jenkinsfile
100 lines (84 loc) · 3.48 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
node {
ws("workspace/${env.JOB_NAME}/${env.BUILD_NUMBER}".replace('%2F', '_')) {
def mvn = tool("M3") + "/bin/mvn"
def ARTIFACT_VERSION;
def DOCKER_IMAGE_NAME;
def DOCKER_IMAGE;
def GIT_REPOSITORY;
def GIT_BRANCH_NAME;
def GIT_COMMIT_ID;
def TIMESTAMP;
try {
stage("Checkout source") {
def scmVars = checkout scm
GIT_REPOSITORY = scmVars.GIT_URL
GIT_REPOSITORY_NAME = gitRepositoryName(scmVars)
GIT_BRANCH_NAME = scmVars.GIT_BRANCH
GIT_COMMIT_ID = scmVars.GIT_COMMIT
TIMESTAMP = new java.text.SimpleDateFormat("yyMMddHHmmss").format(new Date())
}
stage("Create version number") {
def version = readMavenPom().version.replaceAll("-SNAPSHOT", "").trim()
ARTIFACT_VERSION = "${version}-${env.BUILD_NUMBER}-${TIMESTAMP}".trim()
if (GIT_BRANCH_NAME == "master") {
ARTIFACT_VERSION = "release-${ARTIFACT_VERSION}"
} else {
ARTIFACT_VERSION = "pr-${GIT_BRANCH_NAME.replaceAll('/', '_')}-${ARTIFACT_VERSION}"
}
sh """
git branch ${ARTIFACT_VERSION}
${mvn} --errors \\
versions:set \\
-DnewVersion=\"${ARTIFACT_VERSION}\" \\
-DupdateMatchingVersions=true \\
-DgenerateBackupPoms=false \\
-DartifactId='*' \\
-DgroupId='*'
"""
}
stage("Build and test") {
withCredentials([file(credentialsId: 'MVN_SETTINGS_FILE', variable: 'settingsFile')]) {
sh """
${mvn} -X --batch-mode \\
--show-version \\
--errors \\
--update-snapshots \\
--settings ${settingsFile} \\
verify \\
"""
}
}
stage("Build docker image") {
DOCKER_IMAGE_NAME = "949398260329.dkr.ecr.eu-west-1.amazonaws.com/${GIT_REPOSITORY_NAME}:${ARTIFACT_VERSION}";
DOCKER_IMAGE = docker.build(
DOCKER_IMAGE_NAME,
[
[
"GIT_REPOSITORY=${GIT_REPOSITORY}",
"GIT_BRANCH_NAME=${GIT_BRANCH_NAME}",
"GIT_COMMIT_ID=${GIT_COMMIT_ID}",
"ARTIFACT_VERSION=${ARTIFACT_VERSION}"
].collect { "--build-arg ${it.toString()}" }.join(" "),
"-f Dockerfile",
"."
].join(" ")
)
}
stage("Publish version") {
DOCKER_IMAGE.push(ARTIFACT_VERSION)
dockerFingerprintFrom dockerfile: 'Dockerfile', image: DOCKER_IMAGE_NAME
// jenkins
currentBuild.displayName = ARTIFACT_VERSION
}
} finally {
stage("Clean workspace") {
deleteDir()
}
}
}
}
def gitRepositoryName(scmVars) {
tokens = scmVars.GIT_URL.tokenize('/')
lastToken = tokens.get(tokens.size() - 1)
return lastToken.substring(0, lastToken.length() - 4)
}