-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathJenkinsfile
More file actions
549 lines (488 loc) · 24.2 KB
/
Copy pathJenkinsfile
File metadata and controls
549 lines (488 loc) · 24.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
@Library('dyalog-scripts') _
def hashSame = null
// Per-version SVN path configuration.
// Convention: trunk is always the current development version;
// released versions live under branches/{version}/.
def getSvnDocbinUrl(String version) {
if (version == env.TRUNK_VERSION) {
return "docbin/trunk/documentation"
}
return "docbin/branches/${version}/documentation"
}
def getSvnReadmeUrl(String version) {
if (version == env.TRUNK_VERSION) {
return "dyalog/trunk/svn/docs/readmes"
}
return "dyalog/branches/${version}/svn/docs/readmes"
}
/**
* Download release assets for a specific documentation version.
* Finds the latest non-draft release whose tag starts with "v{version}."
* and downloads all assets to the target directory.
*/
def getVersionedReleaseAssets(String repo, String targetDir, String version) {
withCredentials([
usernamePassword(
credentialsId: getCredentialsId('github'),
usernameVariable: 'GITHUB_USER',
passwordVariable: 'GITHUB_PASS'
)
]) {
sh """#!/bin/bash
set -euo pipefail
echo "\${GITHUB_PASS}" | /usr/bin/gh auth login --with-token
REPO="Dyalog/${repo}"
TAG=\$(gh release list -R "\$REPO" --exclude-drafts \\
--json tagName,createdAt \\
| jq -r '[.[] | select(.tagName | startswith("v${version}."))]
| sort_by(.createdAt) | last | .tagName')
if [ -z "\$TAG" ] || [ "\$TAG" = "null" ]; then
echo "ERROR: No release found matching v${version}.*"
exit 1
fi
echo "Downloading assets from release \$TAG for version ${version}"
rm -rf "\$WORKSPACE/${targetDir}"
mkdir -p "\$WORKSPACE/${targetDir}"
gh release download "\$TAG" -R "\$REPO" -D "\$WORKSPACE/${targetDir}"
gh release view "\$TAG" -R "\$REPO" \\
--json apiUrl,author,createdAt,id,isDraft,isPrerelease,name,publishedAt,tagName \\
> "\$WORKSPACE/${targetDir}/buildinfo.json"
echo "Downloaded:"
ls "\$WORKSPACE/${targetDir}"
"""
}
}
pipeline {
agent none
options {
// One job at a time and timestamped log lines
disableConcurrentBuilds()
timestamps()
// Keep last 10 builds but only 5 artifacts (backups)
buildDiscarder(logRotator(
numToKeepStr: '10',
artifactNumToKeepStr: '5'
))
}
parameters {
booleanParam(
name: 'SKIP_BACKUP',
defaultValue: false,
description: 'Skip creating backup before deployment'
)
booleanParam(
name: 'DEBUG',
defaultValue: false,
description: 'Enable debug output to show environment variables and state'
)
}
environment {
GITHUB_REPO = 'https://github.com/dyalog/documentation.git'
WEB_ROOT = '/DockerVolumes/websites/docs.dyalog.com/'
WEB_URL = 'docs.dyalog.com'
HASH_SAME = 'unknown'
SWARM_NAME = "docsweb"
// ============================================================
// PRODUCTION_VERSIONS: Space-separated list of versions to deploy
// 21.0 is published as a live preview (reachable at /21.0/);
// 20.0 remains the default (set_as_latest stays unchecked).
// ============================================================
PRODUCTION_VERSIONS = '20.0 21.0'
// The "development" version currently on trunk in SVN.
// When v21 is released and v22 development starts, update this to '22.0'.
TRUNK_VERSION = '21.0'
GITDOCURL = 'documentation'
// Version-agnostic SVN paths
SVNSHARPPLOTURL = "dyalogtools/Causeway/trunk/release"
SVNDOCDIR = 'svn_docs'
GITDOCDIR = 'git_docs'
}
stages {
stage('Update svndocs') {
agent {
docker
{
image 'dyalogci/ubuntu:22.04-build'
args '-v /home/jenkins/.ssh:/build/.ssh'
}
}
options
{
skipDefaultCheckout(true)
}
stages
{
stage('Checkout from svndocs')
{
steps {
script
{
sh '[ "x" != "x$WORKSPACE" ] && rm -rf $WORKSPACE/*'
doGitCheckout('JenkinsBuild', 'Jenkins')
// Fetch release assets and SVN checkout for each production version
def versions = env.PRODUCTION_VERSIONS.split(' ')
versions.each { version ->
getVersionedReleaseAssets(GITDOCURL, "${GITDOCDIR}/${version}", version)
doSvnCheckout(getSvnDocbinUrl(version), "${SVNDOCDIR}/${version}")
}
}
}
}
stage('Update and Commit svndocs')
{
steps
{
withCredentials([
usernamePassword(credentialsId: getCredentialsId('svn'),
passwordVariable: 'SVN_PASS',
usernameVariable: 'SVN_USER')
])
{
script {
def versions = env.PRODUCTION_VERSIONS.split(' ')
versions.each { version ->
sh """
SVNDOCDIR="${SVNDOCDIR}/${version}" \
GITDOCDIR="${GITDOCDIR}/${version}" \
./Jenkins/gitdocs2svn
"""
}
}
}
}
}
}
} // Update svndocs
stage('Update online files')
{
agent {
// Change to 'swarm && gosport for live server'
label 'swarm && gosport'
}
stages {
stage('Initialise') {
steps {
script {
try {
env.BUILD_TIMESTAMP = sh(
script: 'date -u +%Y%m%d_%H%M%S',
returnStdout: true
).trim()
echo "Build timestamp: ${env.BUILD_TIMESTAMP}"
} catch (Exception e) {
error "Failed to generate build timestamp: ${e.message}"
}
}
}
}
stage('Get files from svn/docbin etc') {
steps {
script {
def versions = env.PRODUCTION_VERSIONS.split(' ')
versions.each { version ->
dir("${version}/files") {
deleteDir()
}
dir("${version}") {
doSvnCheckout(getSvnDocbinUrl(version), "files", true, 'svncom')
doSvnCheckout(env.SVNSHARPPLOTURL, "files/sharpplot", true, 'svncom')
doSvnCheckout(getSvnReadmeUrl(version), "files/readmes", true, 'svncom')
sh "\$WORKSPACE/get_svn_docbin ${version}"
}
}
}
}
}
stage('Check for changes') { // Check if the current deployed hash matches the latest commit
steps {
script {
try {
def currentHash = sh(
script: '''#!/bin/bash
set -euo pipefail
cat "${WEB_ROOT}/.git-hash" 2>/dev/null || true
''',
returnStdout: true
).trim()
def latestHash = sh(
script: '''#!/bin/bash
set -euo pipefail
git rev-parse HEAD
''',
returnStdout: true
).trim()
echo "Current deployed hash : ${currentHash ?: 'none'}"
echo "Latest gh-pages hash : ${latestHash}"
if (currentHash && currentHash == latestHash) {
hashSame = true
env.HASH_SAME = 'true'
echo 'No changes detected – deployment will be skipped.'
} else {
hashSame = false
env.HASH_SAME = 'false'
env.LATEST_HASH = latestHash
echo 'Changes detected or no current deployment – proceeding with deployment.'
}
} catch (Exception e) {
echo "ERROR: Failed to check for changes - ${e.message}"
hashSame = null
throw e
}
}
}
}
stage('Debug Environment') {
when {
expression { params.DEBUG }
}
steps {
script {
echo "DEBUG: After Check for changes stage:"
echo " hashSame (global) = ${hashSame}"
echo " HASH_SAME (env) = ${env.HASH_SAME}"
echo " LATEST_HASH = ${env.LATEST_HASH ?: 'not set'}"
echo " BUILD_TIMESTAMP = ${env.BUILD_TIMESTAMP ?: 'not set'}"
echo " PRODUCTION_VERSIONS = ${env.PRODUCTION_VERSIONS}"
echo " TRUNK_VERSION = ${env.TRUNK_VERSION}"
echo " WEB_ROOT exists = ${sh(script: "test -d '${env.WEB_ROOT}' && echo 'yes' || echo 'no'", returnStdout: true).trim()}"
}
}
}
stage('Backup current site') {
when {
allOf {
expression { hashSame == false }
// Check if WEB_ROOT exists and is a directory
expression { sh(script: "test -d '${env.WEB_ROOT}' && echo 'exists' || true", returnStdout: true).trim() == 'exists' }
expression { !params.SKIP_BACKUP }
}
}
options {
timeout(time: 10, unit: 'MINUTES')
}
steps {
script {
def currentHash = sh(
script: '''
if [ -f "${WEB_ROOT}/.git-hash" ]; then
cat "${WEB_ROOT}/.git-hash" 2>/dev/null | cut -c1-8
else
echo "unknown"
fi
''',
returnStdout: true
).trim()
def backupName = "docs.backup.${env.BUILD_TIMESTAMP}.${currentHash}.tar.gz"
env.BACKUP_FILE = backupName
}
sh '''#!/bin/bash
set -euo pipefail
echo "Backing up current site at ${WEB_ROOT} before deployment."
echo "Creating compressed backup: ${BACKUP_FILE}"
cd "${WEB_ROOT}"
# Build list of existing directories to backup
BACKUP_TARGETS=""
for VERSION in ${PRODUCTION_VERSIONS}; do
if [ -d "${VERSION}" ]; then
BACKUP_TARGETS="${BACKUP_TARGETS} ${VERSION}"
else
echo " Note: ${VERSION} does not exist yet, skipping from backup"
fi
done
# Add .git-hash if it exists
if [ -f ".git-hash" ]; then
BACKUP_TARGETS="${BACKUP_TARGETS} .git-hash"
fi
# Only create backup if we have something to backup
if [ -z "${BACKUP_TARGETS}" ]; then
echo "WARNING: Nothing to backup (no existing production versions)"
# Create empty marker file so archiveArtifacts doesn't fail
touch "${WORKSPACE}/${BACKUP_FILE}.empty"
else
echo "Backing up:${BACKUP_TARGETS}"
tar -czf "${WORKSPACE}/${BACKUP_FILE}" ${BACKUP_TARGETS}
BACKUP_SIZE=$(ls -lh "${WORKSPACE}/${BACKUP_FILE}" | awk '{print $5}')
echo "Backup created successfully. Size: ${BACKUP_SIZE}"
fi
cd "${WORKSPACE}"
'''
archiveArtifacts artifacts: "${env.BACKUP_FILE}",
fingerprint: true,
onlyIfSuccessful: true
}
}
stage('Deploy site') {
when {
expression { hashSame == false }
}
options {
lock('docs-deploy')
}
steps {
sh '''#!/bin/bash
set -euo pipefail
echo "Starting deployment to ${WEB_ROOT}."
if [ -z "${WEB_ROOT}" ]; then
echo "ERROR: WEB_ROOT NOT SET"
exit 1
fi
# Create WEB_ROOT if it doesn't exist
if [ ! -d "${WEB_ROOT}" ]; then
mkdir -p "${WEB_ROOT}"
fi
# Track deployment success
DEPLOYED_COUNT=0
# Deploy only PRODUCTION_VERSIONS
for VERSION in ${PRODUCTION_VERSIONS}; do
echo "Processing version: ${VERSION}"
# CRITICAL: Verify source exists before touching production
if [ ! -d "${WORKSPACE}/${VERSION}" ]; then
echo " ERROR: Source ${WORKSPACE}/${VERSION} does not exist!"
echo " This version may not have been published to gh-pages."
echo " Skipping to avoid wiping production."
continue
fi
if [ ! -f "${WORKSPACE}/${VERSION}/index.html" ]; then
echo " ERROR: ${WORKSPACE}/${VERSION}/index.html missing!"
echo " Source appears incomplete. Skipping to avoid corruption."
continue
fi
# Source verified - safe to deploy
echo " Source verified, deploying..."
# Clear existing version directory (only now that source is verified)
if [ -d "${WEB_ROOT}/${VERSION}" ]; then
rm -rf "${WEB_ROOT}/${VERSION:?}"/*
fi
# Sync this version
rsync -rl --delete --exclude-from="${WORKSPACE}/.rsync-exclude" "${WORKSPACE}/${VERSION}" "${WEB_ROOT}"
echo " Deployed ${VERSION} successfully"
DEPLOYED_COUNT=$((DEPLOYED_COUNT + 1))
done
# Only update git hash if at least one version was deployed
if [ $DEPLOYED_COUNT -eq 0 ]; then
echo "ERROR: No versions were deployed! Not updating .git-hash"
exit 1
fi
# Update git hash
echo "Storing new git hash (${LATEST_HASH})."
echo "${LATEST_HASH}" | tee "${WEB_ROOT}/.git-hash" >/dev/null
echo "Deployment completed successfully. Deployed ${DEPLOYED_COUNT} version(s)."
'''
script {
env.DEPLOYMENT_EXECUTED = 'true'
}
}
post {
failure {
echo "ERROR: Deployment failed at ${env.WEB_ROOT}"
}
}
}
stage('Verify deployment') {
when {
expression { hashSame == false }
}
steps {
sh '''#!/bin/bash
set -euo pipefail
echo "Verifying deployment at ${WEB_ROOT}..."
FAILED=0
for VERSION in ${PRODUCTION_VERSIONS}; do
if ! test -f "${WEB_ROOT}/${VERSION}/index.html"; then
echo "ERROR: ${VERSION}/index.html not found"
FAILED=1
else
echo " ${VERSION}/index.html OK"
fi
done
if [ $FAILED -eq 1 ]; then
exit 1
fi
echo "Deployment verified successfully."
echo "Site location: ${WEB_ROOT}"
echo "Deployed Git hash: $(cat "${WEB_ROOT}/.git-hash" 2>/dev/null || echo 'unknown')"
'''
}
post {
failure {
echo "ERROR: Deployment verification failed - site may be in inconsistent state"
}
}
}
stage('Update Containers') { // Deploy the swarm server incase anything has changed
// Note: Swarm is clever enough that if nothing changes in the config, it won't re-deploy the service.
steps {
sh '''#!/bin/bash
sed -i 's%{{WEB_ROOT}}%${WEB_ROOT}%g' service.yml
sed -i 's%{{WEB_URL}}%${WEB_URL}%g' service.yml
'''
swarmDeploy "${SWARM_NAME}"
}
}
}
post {
success {
script {
if (env.HASH_SAME == 'true') {
echo "No deployment was needed - site is already up to date."
} else if (env.DEPLOYMENT_EXECUTED == 'true') {
echo "Site successfully deployed. New active commit: ${env.LATEST_HASH}"
} else if (env.HASH_SAME == 'false') {
echo "WARNING: Changes were detected but deployment did not execute. Check pipeline logs."
} else {
echo "Pipeline completed successfully."
}
}
}
failure {
script {
// Report which stage failed
echo "FAILURE: Pipeline failed at stage: ${env.STAGE_NAME ?: 'Unknown'}"
// Only attempt rollback if a deployment was actually attempted
if (hashSame == false) {
echo "ERROR: Deployment failed. Attempting automatic rollback to the latest backup..."
try {
// Copy the most recent successful build's backup
copyArtifacts projectName: env.JOB_NAME,
selector: lastSuccessful(),
filter: 'docs.backup.*.tar.gz',
flatten: true,
optional: false
sh '''#!/bin/bash
set -euo pipefail
# Find the backup file that was copied
BACKUP_FILE=$(ls docs.backup.*.tar.gz | head -n1)
if [[ -n "${BACKUP_FILE}" ]]; then
echo "Found backup for rollback: ${BACKUP_FILE}"
# Clear contents but preserve mount point
echo "Clearing failed deployment from ${WEB_ROOT}..."
find "${WEB_ROOT}" -mindepth 1 -delete
echo "Extracting ${BACKUP_FILE} to ${WEB_ROOT}..."
tar -xzf "${BACKUP_FILE}" -C "${WEB_ROOT}" --strip-components=1
echo "Extraction complete."
chown -R www-data:www-data "${WEB_ROOT}"
echo "Ownership set for rolled-back site."
echo "Automatic rollback completed successfully from ${BACKUP_FILE}"
if test -f "${WEB_ROOT}/.git-hash"; then
echo "Rolled back to Git hash: $(cat "${WEB_ROOT}/.git-hash")"
fi
# Clean up the copied artifact
rm -f "${BACKUP_FILE}"
else
echo "ERROR: No backup file found after copy"
exit 1
fi
'''
} catch (Exception e) {
echo "CRITICAL: Rollback failed - ${e.message}"
echo "The site at ${env.WEB_ROOT} might be in an inconsistent or broken state."
}
} else {
echo "ERROR: Pipeline failed, but no deployment was attempted."
}
}
}
}
} // Update online files
} // Stages
} // Pipeline