forked from zowe/docs-site
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
211 lines (191 loc) · 6.89 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
#!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 = 'latest'
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 piublish step.',
defaultValue: true
))
customParameters.push(string(
name: 'PUBLISH_BRANCH',
description: 'Target branch to publish',
defaultValue: 'gh-pages',
trim: true,
required: true
))
customParameters.push(string(
name: 'PUBLISH_PATH',
description: 'Target URL path to publish. Default is "latest" for master branch, "v?.?.x" for v?.?.x release branches.',
defaultValue: '',
trim: true,
required: false
))
customParameters.push(credentials(
name: 'GITHUB_CREDENTIALS',
description: 'Github user credentials',
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-nvm') {
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 (!fileExists('.deploy/latest/index.html')) {
// this is the old documentation directory structure, latest folder doesn't exist
// we need to migrate to new structure
if (isMasterBranch || isTestPublishing) {
// clean the .deploy folder to generate latest folder
sh 'rm -fr .deploy/*'
} else {
error 'Migration "gh-pages" from old directory structure can only be done on master branch.'
}
}
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} npm run docs:build"
}
}
stage('test') {
ansiColor('xterm') {
// list all files generated
sh "find .deploy | grep -v '.deploy/.git'"
// check broken links
timeout(30) {
sh 'npm run test:links'
}
}
}
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 -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
}
}