forked from zowe/docs-site
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
231 lines (210 loc) · 7.92 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
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
#!groovy
/**
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright IBM Corporation 2018
*/
def isPullRequest = env.BRANCH_NAME.startsWith('PR-')
def slackChannel = '#test-build-notify'
def githubRepository = 'zowe/docs-site'
def allowPublishing = false
def isTestPublishing = false
def publishTargetPath = 'stable'
def isMasterBranch = env.BRANCH_NAME == 'master'
def isReleaseBranch = env.BRANCH_NAME ==~ /^v[0-9]+\.[0-9]+\.[0-9x]+$/
def opts = []
// keep last 20 builds for regular branches, no keep for pull requests
opts.push(buildDiscarder(logRotator(numToKeepStr: (isPullRequest ? '' : '20'))))
// disable concurrent build
opts.push(disableConcurrentBuilds())
// define custom build parameters
def customParameters = []
// >>>>>>>> parameters to control pipeline behavior
customParameters.push(booleanParam(
name: 'RUN_PUBLISH',
description: 'If run the publish step. Only take effect when building on "master" and "v?.?.x" branches. This option provides a chance to disable publish on those branches.',
defaultValue: true
))
customParameters.push(string(
name: 'PUBLISH_BRANCH',
description: 'Target branch to publish. By default will be "gh-pages". If you specify a branch name starts with "gh-pages-test", you can always perform test publishing.',
defaultValue: 'gh-pages',
trim: true,
required: true
))
customParameters.push(string(
name: 'PUBLISH_PATH',
description: 'Target URL path to publish. Default is "stable" for master branch, "v?.?.x" for v?.?.x release branches.',
defaultValue: '',
trim: true,
required: false
))
customParameters.push(booleanParam(
name: 'FULL_SITE_LINKS_CHECK',
description: 'If run links check on all latest and archived versions, not only checking current build.',
defaultValue: false
))
customParameters.push(credentials(
name: 'GITHUB_CREDENTIALS',
description: 'Github user credentials. Required to publish build result to gh-pages.',
credentialType: 'com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl',
defaultValue: 'zowe-robot-github',
required: true
))
customParameters.push(string(
name: 'GITHUB_USER_EMAIL',
description: 'github user email',
defaultValue: '[email protected]',
trim: true,
required: true
))
customParameters.push(string(
name: 'GITHUB_USER_NAME',
description: 'github user name',
defaultValue: 'Zowe Robot',
trim: true,
required: true
))
opts.push(parameters(customParameters))
// set build properties
properties(opts)
node ('ibm-jenkins-slave-dind') {
currentBuild.result = 'SUCCESS'
// if we are on master, or v?.?.? / v?.?.x branch, we allow publish
// if we publish target branch to test branch, we allow it anyway
isTestPublishing = params.PUBLISH_BRANCH.startsWith('gh-pages-test')
if (isMasterBranch || isReleaseBranch || isTestPublishing) {
allowPublishing = true
}
if (allowPublishing && isReleaseBranch) {
publishTargetPath = env.BRANCH_NAME
}
if (allowPublishing && params.PUBLISH_PATH) { // this is manually assigned parameter value
publishTargetPath = params.PUBLISH_PATH
}
try {
stage('checkout') {
// checkout source code
checkout scm
// check if it's pull request
echo "Current branch is ${env.BRANCH_NAME}"
if (allowPublishing) {
echo "**** Will publish to \"${params.PUBLISH_BRANCH}\" branch \"${publishTargetPath}\" directory"
} else {
echo "**** Publish stage will be skipped"
}
if (isPullRequest) {
echo "This is a pull request"
}
}
stage('prepare') {
// prepare .deploy folder
// checkout params.PUBLISH_BRANCH to .deploy folder
sh """
git config --global user.email \"${params.GITHUB_USER_EMAIL}\"
git config --global user.name \"${params.GITHUB_USER_NAME}\"
mkdir -p .deploy
cd .deploy
git init
git remote add origin https://github.com/${githubRepository}.git
git fetch
git checkout -B ${params.PUBLISH_BRANCH}
if [ -n "\$(git ls-remote --heads origin ${params.PUBLISH_BRANCH})" ]; then git pull origin ${params.PUBLISH_BRANCH}; fi
cd ..
"""
if (isMasterBranch) {
// alway try to update default pages from master branch
sh 'cp -r gh-pages-default/. .deploy/'
}
}
stage('build') {
ansiColor('xterm') {
sh 'npm install'
sh "PUBLISH_TARGET_PATH=${publishTargetPath} NODE_OPTIONS=--max_old_space_size=4096 npm run docs:build"
}
}
stage('test') {
ansiColor('xterm') {
// list all files generated
sh "find .deploy | grep -v '.deploy/.git'"
// check broken links
timeout(30) {
if (params.FULL_SITE_LINKS_CHECK) {
sh 'npm run test:links'
} else {
def publishTargetPathConverted = publishTargetPath.replaceAll(/\./, '-')
sh "npm run test:links -- --start-point /${publishTargetPathConverted}/"
}
}
}
}
stage('pdf') {
ansiColor('xterm') {
// lock building pdf step since the docker pulling image stage cause build failure
lock('building-docs-pdf') {
timeout(time: 1, unit: 'HOURS') {
sh 'npm run docs:pdf'
}
}
if (fileExists('.deploy/.pdf/out/Zowe_Documentation.pdf')) {
def publishTargetPathConverted = publishTargetPath.replaceAll(/\./, '-')
sh "cp .deploy/.pdf/out/Zowe_Documentation.pdf .deploy/${publishTargetPathConverted}/"
} else {
error 'Failed to generate PDF document.'
}
// clean up pdf tmp folder
echo 'Cleaning up .deploy/.pdf ...'
sh 'rm -fr .deploy/.pdf || true'
}
}
utils.conditionalStage('publish', allowPublishing && params.RUN_PUBLISH) {
ansiColor('xterm') {
withCredentials([usernamePassword(
credentialsId: params.GITHUB_CREDENTIALS,
passwordVariable: 'GIT_PASSWORD',
usernameVariable: 'GIT_USERNAME'
)]) {
sh """
cd .deploy
git add -A
git commit -s -m \"deploy from ${env.JOB_NAME}#${env.BUILD_NUMBER}\"
git push 'https://${GIT_USERNAME}:${GIT_PASSWORD}@github.com/${githubRepository}.git' ${params.PUBLISH_BRANCH}
"""
}
}
}
stage('done') {
// send out notification
// slackSend channel: slackChannel,
// color: 'good',
// message: "Job \"${env.JOB_NAME}\" build #${env.BUILD_NUMBER} succeeded.\n\nCheck detail: ${env.BUILD_URL}"
emailext body: "Job \"${env.JOB_NAME}\" build #${env.BUILD_NUMBER} succeeded.\n\nCheck detail: ${env.BUILD_URL}" ,
subject: "[Jenkins] Job \"${env.JOB_NAME}\" build #${env.BUILD_NUMBER} succeeded",
recipientProviders: [
[$class: 'RequesterRecipientProvider'],
[$class: 'CulpritsRecipientProvider'],
[$class: 'DevelopersRecipientProvider'],
[$class: 'UpstreamComitterRecipientProvider']
]
}
} catch (err) {
currentBuild.result = 'FAILURE'
// catch all failures to send out notification
// slackSend channel: slackChannel,
// color: 'warning',
// message: "Job \"${env.JOB_NAME}\" build #${env.BUILD_NUMBER} failed.\n\nError: ${err}\n\nCheck detail: ${env.BUILD_URL}"
emailext body: "Job \"${env.JOB_NAME}\" build #${env.BUILD_NUMBER} failed.\n\nError: ${err}\n\nCheck detail: ${env.BUILD_URL}" ,
subject: "[Jenkins] Job \"${env.JOB_NAME}\" build #${env.BUILD_NUMBER} failed",
recipientProviders: [
[$class: 'RequesterRecipientProvider'],
[$class: 'CulpritsRecipientProvider'],
[$class: 'DevelopersRecipientProvider'],
[$class: 'UpstreamComitterRecipientProvider']
]
throw err
}
}