-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
222 lines (201 loc) · 8.94 KB
/
Copy pathJenkinsfile
File metadata and controls
222 lines (201 loc) · 8.94 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
pipeline {
agent any
triggers{ cron( getCronParams() ) }
tools {
jdk 'jdk21'
}
options {
buildDiscarder(logRotator(numToKeepStr: '30'))
}
stages {
stage('Build Image') {
when { expression { return shouldBuildDevOrRelease() } }
steps {
sh './gradlew clean build cyclonedxBom -x test -x check'
}
}
stage ('Base-Image Update') {
when {
allOf {
buildingTag()
expression { return currentBuild.number > 1 && shouldBuildDockerImage()}
}
}
steps {
sh './gradlew --no-daemon -Djib.console=plain build -x test -x check'
}
}
stage('Deploy Image') {
// do not run when building a release from a branch-Jenkins-Job
// In Jenkins there's a special Tag-Job, that handles the release
when {
anyOf {
expression { return shouldBuildDevOrRelease() && shouldBuildDockerImage() }
allOf {
buildingTag()
expression { return currentBuild.number > 1 && shouldBuildDockerImage() }
}
}
}
environment {
DOCKER_REGISTRY_CREDS = credentials('docker-registry-wemove')
}
steps {
sh './gradlew -Djib.console=plain publishImage'
}
}
stage('Tests') {
when { expression { return shouldBuildDevOrRelease() } }
steps {
sh './gradlew test'
}
}
stage('Build RPM') {
when { expression { return shouldBuildDevOrRelease() } }
agent {
docker {
image 'docker-registry.wemove.com/ingrid-rpmbuilder-jdk21-improved'
reuseNode true
}
}
steps {
script {
sh "sed -i 's/^Version:.*/Version: ${determineVersion()}/' rpm/ingrid-api.spec"
sh "sed -i 's/^Release:.*/Release: ${determineRpmReleasePart()}/' rpm/ingrid-api.spec"
// Prepare build
sh "mkdir -p ./build/rpms /root/rpmbuild/SPECS"
sh """
cp ${WORKSPACE}/rpm/ingrid-api.spec /root/rpmbuild/SPECS/ingrid-api.spec &&
rpmbuild -bb /root/rpmbuild/SPECS/ingrid-api.spec
"""
withCredentials([
file(credentialsId: 'ingrid-rpm-public', variable: 'RPM_PUBLIC_KEY'),
file(credentialsId: 'ingrid-rpm-private', variable: 'RPM_PRIVATE_KEY'),
string(credentialsId: 'ingrid-rpm-passphrase', variable: 'RPM_SIGN_PASSPHRASE')
]) {
sh 'gpg --batch --import $RPM_PUBLIC_KEY'
sh 'gpg --batch --import $RPM_PRIVATE_KEY'
sh "mkdir -p ./build/rpms/ingrid"
sh "cp -r /root/rpmbuild/RPMS/noarch/* ${WORKSPACE}/build/rpms/ingrid/"
sh "expect /rpm-sign.exp ${WORKSPACE}/build/rpms/ingrid/*.rpm"
archiveArtifacts artifacts: 'build/rpms/ingrid/ingrid-api-*.rpm', fingerprint: true
}
withCredentials([
file(credentialsId: 'itzbund-ingrid-rpm-public', variable: 'RPM_PUBLIC_KEY'),
file(credentialsId: 'itzbund-ingrid-rpm-private', variable: 'RPM_PRIVATE_KEY'),
string(credentialsId: 'itzbund-ingrid-rpm-passphrase', variable: 'RPM_SIGN_PASSPHRASE')
]) {
sh 'rm -f ~/.gnupg/*.kbx'
sh 'rm -f ~/.gnupg/*.gpg'
sh 'gpg --batch --import $RPM_PUBLIC_KEY'
sh 'gpg --batch --import $RPM_PRIVATE_KEY'
sh "mkdir -p ./build/rpms/itzbund"
sh "cp -r /root/rpmbuild/RPMS/noarch/* ${WORKSPACE}/build/rpms/itzbund/"
sh "expect /rpm-sign.exp ${WORKSPACE}/build/rpms/itzbund/*.rpm"
archiveArtifacts artifacts: 'build/rpms/itzbund/ingrid-api-*.rpm', fingerprint: true
}
}
}
}
stage('Deploy RPM') {
when { expression { return shouldBuildDevOrRelease() } }
steps {
script {
def repoType = env.TAG_NAME ? "rpm-ingrid-releases" : "rpm-ingrid-snapshots"
sh "mv build/reports/bom.json build/reports/ingrid-api-${determineVersion()}.bom.json"
archiveArtifacts artifacts: "build/reports/*.bom.json", fingerprint: true
withCredentials([usernamePassword(credentialsId: '9623a365-d592-47eb-9029-a2de40453f68', passwordVariable: 'PASSWORD', usernameVariable: 'USERNAME')]) {
sh '''
curl -f --user $USERNAME:$PASSWORD --upload-file build/rpms/ingrid/*.rpm https://nexus.informationgrid.eu/repository/''' + repoType + '''/
curl -f --user $USERNAME:$PASSWORD --upload-file build/reports/*.bom.json https://nexus.informationgrid.eu/repository/''' + repoType + '''/
'''
}
if (repoType == 'rpm-ingrid-releases') {
withCredentials([usernamePassword(credentialsId: '9623a365-d592-47eb-9029-a2de40453f68', passwordVariable: 'PASSWORD', usernameVariable: 'USERNAME')]) {
sh '''
curl -f --user $USERNAME:$PASSWORD --upload-file build/rpms/itzbund/*.rpm https://nexus.informationgrid.eu/repository/rpm-ingrid-itzbund/
curl -f --user $USERNAME:$PASSWORD --upload-file build/reports/*.bom.json https://nexus.informationgrid.eu/repository/rpm-ingrid-itzbund/
'''
}
if (env.TAG_NAME && env.TAG_NAME.startsWith("RPM-")) {
// No upload to other ITZBund repos
} else {
withCredentials([usernamePassword(credentialsId: '9623a365-d592-47eb-9029-a2de40453f68', passwordVariable: 'PASSWORD', usernameVariable: 'USERNAME')]) {
sh '''
curl -f --user $USERNAME:$PASSWORD --upload-file build/rpms/itzbund/*.rpm https://nexus.informationgrid.eu/repository/rpm-zdm_release/
curl -f --user $USERNAME:$PASSWORD --upload-file build/reports/*.bom.json https://nexus.informationgrid.eu/repository/rpm-zdm_release/
'''
}
}
}
}
}
}
}
post {
always {
script {
if (shouldBuildDevOrRelease()) {
junit 'build/test-results/**/*.xml'
}
}
}
changed {
// send Email with Jenkins' default configuration
script {
emailext (
body: '${DEFAULT_CONTENT}',
subject: '${DEFAULT_SUBJECT}',
to: '${DEFAULT_RECIPIENTS}')
}
}
}
}
def getCronParams() {
String tagTimestamp = env.TAG_TIMESTAMP
long diffInDays = 0
if (tagTimestamp != null) {
long diff = "${currentBuild.startTimeInMillis}".toLong() - "${tagTimestamp}".toLong()
diffInDays = diff / (1000 * 60 * 60 * 24)
echo "Days since release: ${diffInDays}"
}
def versionMatcher = /\d\.\d\.\d(.\d)?/
if( env.TAG_NAME ==~ versionMatcher && diffInDays < 180) {
// every Sunday between midnight and 6am
return 'H H(0-6) * * 0'
}
else {
return ''
}
}
def determineVersion() {
if (env.TAG_NAME) {
if (env.TAG_NAME.startsWith("RPM-")) { // e.g. RPM-8.0.0-0.1SNAPSHOT
def lastDashIndex = env.TAG_NAME.lastIndexOf("-")
return env.TAG_NAME.substring(4, lastDashIndex)
}
return env.TAG_NAME
} else {
return env.BRANCH_NAME.replaceAll('/', '_')
}
}
def determineRpmReleasePart() {
if (env.TAG_NAME) {
if (env.TAG_NAME.startsWith("RPM-")) {
return env.TAG_NAME.substring(env.TAG_NAME.lastIndexOf("-") + 1)
}
return '1'
} else {
return 'dev'
}
}
def shouldBuildDevOrRelease() {
// If no tag is being built OR it is the first build of a tag
boolean isTag = env.TAG_NAME != null && env.TAG_NAME.trim() != ''
return !isTag || (isTag && currentBuild.number == 1)
}
def shouldBuildDockerImage() {
if (env.TAG_NAME && env.TAG_NAME.startsWith("RPM-")) {
return false
} else return true
}