-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
131 lines (106 loc) · 4.59 KB
/
Jenkinsfile
File metadata and controls
131 lines (106 loc) · 4.59 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
#!groovy
@Library('github.com/cloudogu/ces-build-lib@5.3.1')
import com.cloudogu.ces.cesbuildlib.*
node('docker') {
properties([
// Keep only the last 10 build to preserve space
buildDiscarder(logRotator(numToKeepStr: '10')),
// Don't run concurrent builds for a branch, because they use the same workspace directory
disableConcurrentBuilds(),
parameters([
booleanParam(defaultValue: false, name: 'forceDeployGhPages',
description: 'GH Pages are deployed on main Branch only. If this box is checked it\'s deployed no what Branch is built.')
])
])
def introSlidePath = 'docs/slides/01-intro.md'
// Params for GitHub pages deployment
String ghPageCredentials = 'cesmarvin'
Git git = new Git(this, ghPageCredentials)
Docker docker = new Docker(this)
catchError {
stage('Checkout') {
checkout scm
git.clean('')
}
String pdfName = createPdfName()
String versionName = createVersion()
String imageName = "${env.JOB_NAME}:${versionName}"
String packagePath = 'target'
forceDeployGhPages = Boolean.valueOf(params.forceDeployGhPages)
def image
stage('Build') {
writeVersionNameToIntroSlide(versionName, introSlidePath)
image = docker.build imageName
// Extract rendered reveal webapp from container
sh "tempContainer=\$(docker create ${image.id}) && " +
"docker cp \${tempContainer}:/usr/share/nginx/html ${packagePath} && " +
"docker rm \${tempContainer}"
}
stage('Print PDF & Package WebApp') {
String pdfPath = "${packagePath}/${pdfName}"
printPdf pdfPath
// Avoid "ERROR: No artifacts found that match the file pattern " by using *.
// Has the risk of archiving other PDFs that might be there
archiveArtifacts "${packagePath}/*.pdf"
// Make world readable (useful when accessing from docker)
sh "chmod og+r '${pdfPath}'"
// Use a constant name for the PDF for easier URLs, for deploying
String finalPdfPath = "pdf/${createPdfName(false)}"
sh "mkdir -p ${packagePath}/pdf/ pdf"
sh "mv '${pdfPath}' '${packagePath}/${finalPdfPath}'"
sh "cp '${packagePath}/${finalPdfPath}' '${finalPdfPath}'"
}
stage('Deploy GH Pages') {
if (env.BRANCH_NAME == 'main' || forceDeployGhPages) {
git.pushGitHubPagesBranch(packagePath, versionName)
} else {
echo "Skipping deploy to GH pages, because not on main branch"
}
}
}
mailIfStatusChanged(git.commitAuthorEmail)
}
String createPdfName(boolean includeDate = true) {
String forbiddenChars = "[\\\\/:*?\"<>|]"
String title = sh (returnStdout: true, script: 'grep -r \'TITLE\' Dockerfile | sed "s/.*TITLE=\'\\(.*\\)\'.*/\\1/" ')
.trim()
.replaceAll(forbiddenChars, '')
String pdfName = ''
if (includeDate) {
pdfName = "${new Date().format('yyyy-MM-dd')}-"
}
pdfName += "${title}.pdf"
return pdfName
}
String createVersion() {
// E.g. "201708140933-1674930"
String versionName = "${new Date().format('yyyyMMddHHmm')}-${new Git(this).commitHashShort}"
if (env.BRANCH_NAME == "main") {
currentBuild.description = versionName
echo "Building version $versionName on branch ${env.BRANCH_NAME}"
} else {
versionName += '-SNAPSHOT'
}
return versionName
}
void writeVersionNameToIntroSlide(String versionName, String introSlidePath) {
def distIntro = "${introSlidePath}"
String filteredIntro = filterFile(distIntro, "<!--VERSION-->", "Version: $versionName")
sh "cp $filteredIntro $distIntro"
sh "mv $filteredIntro $introSlidePath"
}
void printPdf(String pdfPath) {
sh (returnStdout: true, script: "DEBUG=1 COMPRESS=true ./printPdf.sh | xargs -I{} mv {} '${pdfPath}'").trim()
}
/**
* Filters a {@code filePath}, replacing an {@code expression} by {@code replace} writing to new file, whose path is returned.
*
* @return path to filtered file
*/
String filterFile(String filePath, String expression, String replace) {
String filteredFilePath = filePath + ".filtered"
// Fail command (and build) if file not present
sh "test -e ${filePath} || (echo Title slide ${filePath} not found && return 1)"
sh "cat ${filePath} | sed 's/${expression}/${replace}/g' > ${filteredFilePath}"
return filteredFilePath
}