forked from zowe/docs-site
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile.archive
171 lines (151 loc) · 6.83 KB
/
Jenkinsfile.archive
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
#!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 versionPattern = /^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(string(
name: 'RELEASE_VERSION',
description: 'Version to release. For example, v0.9.x',
trim: true,
required: true
))
customParameters.push(string(
name: 'BASE_BRANCH',
description: 'Base branch of the release branch',
defaultValue: 'master',
trim: true,
required: true
))
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'
// validate release version name
if (!params.RELEASE_VERSION) {
error "RELEASE_VERSION parameter is required to release"
}
def isValidVersion = params.RELEASE_VERSION ==~ versionPattern
if (!isValidVersion) {
error "\"${params.RELEASE_VERSION}\" is not a valid version name. Please use pattern of \"v?.?.?\""
}
try {
stage('checkout') {
// checkout source code
checkout scm
// check if it's pull request
echo "Current branch is ${env.BRANCH_NAME}"
if (isPullRequest) {
echo "This is a pull request"
}
// config github users
sh """
git config --global user.email \"${params.GITHUB_USER_EMAIL}\"
git config --global user.name \"${params.GITHUB_USER_NAME}\"
"""
}
stage('release') {
def remote = sh(script: "git ls-remote --heads origin ${params.RELEASE_VERSION}", returnStdout: true).trim()
if (remote) {
error "Branch \"${params.RELEASE_VERSION}\" is already existed"
}
echo "Creating new branch \"${params.RELEASE_VERSION}\" from origin/${params.BASE_BRANCH} ..."
sh "git config remote.origin.fetch \"+refs/heads/*:refs/remotes/origin/*\""
sh "git fetch"
sh "git checkout -b ${params.RELEASE_VERSION} origin/${params.BASE_BRANCH}"
withCredentials([usernamePassword(
credentialsId: params.GITHUB_CREDENTIALS,
passwordVariable: 'GIT_PASSWORD',
usernameVariable: 'GIT_USERNAME'
)]) {
echo "Publish to origin ..."
sh "git push 'https://${GIT_USERNAME}:${GIT_PASSWORD}@github.com/${githubRepository}.git' ${params.RELEASE_VERSION}"
echo "Protect the branch ..."
sh "curl -u '${GIT_USERNAME}:${GIT_PASSWORD}' -X PUT -H 'Content-Type: application/json' -d '{\"required_status_checks\":{\"strict\":true,\"contexts\":[\"DCO\"]},\"enforce_admins\":true,\"required_pull_request_reviews\": {\"dismiss_stale_reviews\":false,\"require_code_owner_reviews\":false},\"restrictions\":{\"users\":[],\"teams\":[\"Documentation\"]}}' https://api.github.com/repos/${githubRepository}/branches/${params.RELEASE_VERSION}/protection"
}
}
stage('done') {
echo """
*************************************************************************************************
* *
* Branch ${params.RELEASE_VERSION} is created and protected. *
* *
* You can go ahead update docs/.vuepress/versions.json on master branch to add the new version. *
* *
* Example version entry is: *
* *
* { *
* "text": "v0.9.x", *
* "link": "v0-9-x/" *
* } *
* *
*************************************************************************************************
"""
// 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
}
}