forked from FusionAuth/fusionauth-jwt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
320 lines (285 loc) · 15.2 KB
/
Jenkinsfile
File metadata and controls
320 lines (285 loc) · 15.2 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//Added license just to get past the license check for our CI/CD pipeline file.
@Library("jenkins-common@1.3.9") _
import com.codelogic.jenkins.common.DockerRunBuilder
def getChangeLogFormattedForDisplay() {
def changeLog = "" + currentBuild.changeSets.collect({
it.items.collect({ "${it.author} ${it.commitId} ${it.msg}" }).join("\n")
}).join("\n")
return changeLog ? changeLog : "No Commits or Changes"
}
def getMavenPublishVersion(BRANCH_TAG, MASTER_BRANCH_VERSION) {
PUBLISH_VERSION = BRANCH_TAG
if (BRANCH_TAG.equals("master")) {
PUBLISH_VERSION = MASTER_BRANCH_VERSION
}
else if (BRANCH_TAG.startsWith("v.")) {
PUBLISH_VERSION = BRANCH_TAG.replace("v.", "")
}
else {
PUBLISH_VERSION = "1.0.0-SNAPSHOT"
}
return PUBLISH_VERSION
}
def getDockerImageName(BRANCH_TAG, BASE_VALUE) {
REPO_NAME = BASE_VALUE
if (BRANCH_TAG.startsWith("v")) {
REPO_NAME = "${REPO_NAME}_release"
}
return REPO_NAME
}
pipeline {
// Run only on agent where Docker is installed
agent { node { label 'jenkins-linux-autostart-build-agent' } }
options {
// Discard everything except the last 10 builds
buildDiscarder(logRotator(numToKeepStr: '10'))
// This fixes the issue where builds are prevented due to "Suppress automatic SCM triggering" being enabled by Jenkins
overrideIndexTriggers(true)
timestamps()
timeout(time: 2, unit: 'HOURS')
}
environment {
ARTIFACTORY_CREDS = credentials('JenkinsArtifactory')
AZURE_SIGNING_SECRET = credentials("azure_signing_secret")
// make branch name safe for use as a docker tag
BRANCH_TAG = "${env.BRANCH_NAME}".replaceAll(/[^A-Za-z0-9_\-\.]/, "_").take(120)
CHANGE_LOG = getChangeLogFormattedForDisplay()
// Use docker images from our AWS ECR
DOCKER_BASE_REPO = "https://130246223486.dkr.ecr.us-east-2.amazonaws.com"
DOCKER_CREDENTIALS = "ecr:us-east-2:jenkins-cicd-aws-keys"
DOCKER_MAVEN = "130246223486.dkr.ecr.us-east-2.amazonaws.com/maven:3.6.3-jdk-11"
DOCKER_MAVEN_3_8_5 = "130246223486.dkr.ecr.us-east-2.amazonaws.com/maven:3.8.5-openjdk-17-slim"
// DOCKER_PACKAGING has debuild and rpmbuild to minimize downloads
DOCKER_PACKAGING="130246223486.dkr.ecr.us-east-2.amazonaws.com/packaging:latest-noble"
APACHE_INSTANCE_CREDS = credentials("CodeLogicApacheInstance")
// Current target version for the master branch (on the integration & qa branches, this will be the _next_ master branch version.)
MASTER_BRANCH_VERSION = "99.0.0-master-SNAPSHOT"
MAVEN_PUBLISH_VERSION = getMavenPublishVersion(BRANCH_TAG, MASTER_BRANCH_VERSION)
SECONDS_SINCE_EPOCH = sh(script: 'date -u +%s', returnStdout: true).trim()
TARGET_PLATFORMS_DEB = "linux/amd64,linux/arm64"
TARGET_PLATFORMS_RPM = "x86_64,aarch64"
}
stages {
// Only run the CI pipeline if it's one of these branches
stage('Check Branch') {
when {
not {
expression { BRANCH_NAME ==~ /(integration|qa|master|feature\/.*|renovate\/.*|v.*)/ }
}
}
steps {
sh 'exit 1'
}
}
stage("Resolve Version") {
steps {
script {
resolveVersionData()
}
}
}
stage('Build Branch and Run UTs & ITs') {
when {
expression { BRANCH_NAME ==~ /(integration|qa|master|feature\/.*|renovate\/.*)/ }
}
steps {
script {
docker.withRegistry(DOCKER_BASE_REPO, DOCKER_CREDENTIALS) {
// Maven steps - capture Docker output for failure analysis
// Integration tests are being run as root to provide access to /var/run/docker.sock
sh('''#!/bin/bash
set -o pipefail # Ensure pipeline failures are propagated
echo "Starting integration tests build at $(date)" | tee "${WORKSPACE}/integration-tests-build.log"
# Run Docker command and capture both output and exit code
if docker run \
--env "ARTIFACTORY_CREDS_PSW=${ARTIFACTORY_CREDS_PSW}" \
--env "ARTIFACTORY_CREDS_USR=${ARTIFACTORY_CREDS_USR}" \
--env "GROUP_ID=$(id -g)" \
--env "USER_ID=$(id -u)" \
--rm \
--user 0:0 \
--volume "${PWD}:${PWD}" \
--volume /var/run/docker.sock:/var/run/docker.sock \
--workdir "${PWD}" \
"${DOCKER_MAVEN_3_8_5}" \
sh -c 'mkdir -p ${PWD}/temp/dependency \
&& mvn dependency:copy-dependencies -DoutputDirectory=${PWD}/temp/dependency -DincludeScope=runtime \
&& mvn \
clean validate install \
--activate-profiles no-plugin-copy \
--define format=xml \
--define outputDirectory=target \
--define scanpath=target \
--define skipDependencyCheck=true \
--define skipITs=false \
--define skipSpotbugs=false \
--define skipTests=false \
--update-snapshots \
&& chown --recursive "${USER_ID}:${GROUP_ID}" *' \
2>&1 | tee -a "${WORKSPACE}/integration-tests-build.log"; then
echo "Integration tests build SUCCEEDED at $(date)" | tee -a "${WORKSPACE}/integration-tests-build.log"
else
exit_code=$?
echo "Integration tests build FAILED at $(date) with exit code $exit_code" | tee -a "${WORKSPACE}/integration-tests-build.log"
exit $exit_code
fi
''')
}
}
}
}
stage('Record Test Results') {
when {
expression { BRANCH_NAME ==~ /(integration|qa|master|feature\/.*|renovate\/.*)/ }
}
steps {
script {
echo "Skipping Dependency-Check analysis for all branches - renovate handles this functionality"
}
}
}
// stage('Merge to QA') {
// // Only merge Integration into QA if we're in the integration branch and all Unit Tests have passed...
// when {
// branch 'integration'
// }
// steps {
// mergeBranch("integration", "qa")
// }
// }
//
// stage('Merge QA to Master') {
// // Only merge QA into Master if we're in the QA branch and all Unit and Integration Tests have passed...
// when {
// branch 'qa'
// }
// steps {
// mergeBranch("qa", "master")
// }
// }
stage('CodeLogic Scan') {
when {
expression { BRANCH_NAME ==~ /(integration|feature\/.*|renovate\/.*)/ }
}
steps {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
// Publish CodeLogic Scan to Dogfood
sh('''
ls -lash ${PWD}/temp/dependency && \
docker run \
--env "AGENT_PASSWORD=${APACHE_INSTANCE_CREDS_PSW}" \
--env "AGENT_UUID=${APACHE_INSTANCE_CREDS_USR}" \
--env "CODELOGIC_HOST=https://apache.app.codelogic.com" \
--env "MAVEN_PUBLISH_VERSION=${MAVEN_PUBLISH_VERSION}" \
--env "SCAN_SPACE_NAME=${SCAN_SPACE_NAME}" \
--interactive \
--pull always \
--rm \
--volume "${PWD}:/scan" \
apache.app.codelogic.com/codelogic_java:latest \
analyze \
--application "fusionauth-jwt-${MAVEN_PUBLISH_VERSION}" \
--expunge-scan-sessions \
--method-filter io.fusionauth \
--rescan \
--recursive '*' \
--path /scan \
--scan-space-name \\"${SCAN_SPACE_NAME}\\"
''')
}
}
}
}
// Post pipeline actions
post {
failure {
script {
sendSlackFailure()
}
}
// Always perform this code, even if the pipeline stages fail
always {
script {
try {
// Collect Docker execution logs for build failure analysis
sh """#!/bin/bash
# Collect all available Docker build logs
first_file=true
for log_file in unit-tests-build.log integration-tests-build.log release-build.log; do
if [ -f "${env.WORKSPACE}/\$log_file" ]; then
# Determine stage status
if grep -q "SUCCEEDED" "${env.WORKSPACE}/\$log_file"; then
status="SUCCESS"
elif grep -q "FAILED" "${env.WORKSPACE}/\$log_file"; then
status="FAILED"
else
status="UNKNOWN"
fi
# Use > for first file, >> for subsequent files
if [ "\$first_file" = true ]; then
echo "=== \$log_file (\$status) ===" > '${env.WORKSPACE}/build-logs-summary.log'
first_file=false
else
echo "=== \$log_file (\$status) ===" >> '${env.WORKSPACE}/build-logs-summary.log'
fi
cat "${env.WORKSPACE}/\$log_file" >> '${env.WORKSPACE}/build-logs-summary.log'
echo "" >> '${env.WORKSPACE}/build-logs-summary.log'
fi
done
"""
} catch (Exception e) {
echo "Error collecting Docker logs: ${e.getMessage()}"
// Create a minimal error log
writeFile file: 'build-logs-summary.log', text: "Error: Failed to collect Docker build logs - ${e.getMessage()}"
}
}
script {
// Send build info only for failed renovate builds
if (currentBuild.result == 'FAILURE' && BRANCH_NAME ==~ /(feature\/.*)/) {
// Download and execute send_build_info.sh for failed renovate builds
sh("""#!/bin/bash
echo "Sending build information to dogfood for failed renovate build: ${BRANCH_NAME}"
# Download send_build_info.tar from dogfood server
wget https://apache.app.codelogic.com/codelogic/server/packages/send_build_info.tar -O /tmp/send_build_info.tar
# Extract the script
tar -xf /tmp/send_build_info.tar -C /tmp
# Make it executable
chmod +x /tmp/send_build_info.sh
# Execute send_build_info.sh with appropriate parameters for a failed build
/tmp/send_build_info.sh \\
--agent-uuid="${APACHE_INSTANCE_CREDS_USR}" \\
--agent-password="${APACHE_INSTANCE_CREDS_PSW}" \\
--build-number="${BUILD_NUMBER}" \\
--build-status="FAILURE" \\
--job-name="fusionauth-jwt-${BRANCH_NAME}" \\
--pipeline-system="Jenkins" \\
--server="https://apache.app.codelogic.com" \\
--log-file="${env.WORKSPACE}/build-logs-summary.log" \\
--log-lines=2000 \\
--verbose
# Clean up
rm -f /tmp/send_build_info.tar /tmp/send_build_info.sh
""")
}
}
// Clean out the workspace
cleanWs()
}
}
}