-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJenkinsfile
166 lines (151 loc) · 5.77 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
/*
* (C) Copyright 2019 Nuxeo (http://nuxeo.com/) and others.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Contributors:
* Antoine Taillefer <[email protected]>
* Nelson Silva <[email protected]>
*/
properties([
[$class: 'GithubProjectProperty', projectUrlStr: 'https://github.com/nuxeo/jx-webui-env/'],
[$class: 'BuildDiscarderProperty', strategy: [$class: 'LogRotator', daysToKeepStr: '60', numToKeepStr: '60', artifactNumToKeepStr: '5']],
disableConcurrentBuilds(),
])
void setGitHubBuildStatus(String context, String message, String state) {
step([
$class: 'GitHubCommitStatusSetter',
reposSource: [$class: 'ManuallyEnteredRepositorySource', url: 'https://github.com/nuxeo/jx-webui-env'],
contextSource: [$class: 'ManuallyEnteredCommitContextSource', context: context],
statusResultSource: [$class: 'ConditionalStatusResultSource', results: [[$class: 'AnyBuildResult', message: message, state: state]]],
])
}
String getTargetNamespace() {
return BRANCH_NAME == 'master' ? 'webui' : 'webui-staging'
}
String getDryRun() {
return BRANCH_NAME == 'master' ? 'false' : 'true'
}
pipeline {
agent {
label 'jenkins-jx-base'
}
environment {
DRY_RUN = getDryRun()
SERVICE_ACCOUNT = 'jenkins'
NAMESPACE = getTargetNamespace()
}
stages {
stage('Upgrade Jenkins X platform') {
steps {
setGitHubBuildStatus('upgrade', 'Upgrade Jenkins X platform', 'PENDING')
container('jx-base') {
echo "Upgrade Jenkins X ${NAMESPACE} platform"
script {
// get the existing docker config
def dockerConfig = sh(
script: "jx step credential -s jenkins-docker-cfg -k config.json | tr -d '\\n'",
returnStdout: true
).trim();
// get the existing npm token
def npmToken = sh(
script: 'jx step credential -s jenkins-npm-token -k token',
returnStdout: true
).trim();
// get the existing nexus password
def packagesPassword = sh(
script: "jx step credential -s packages.nuxeo.com-auth -k password",
returnStdout: true
).trim();
withEnv([
"INTERNAL_DOCKER_REGISTRY=${DOCKER_REGISTRY}",
"DOCKER_REGISTRY_CONFIG=${dockerConfig}",
"NPM_TOKEN=${npmToken}",
"PACKAGES_PASSWORD=${packagesPassword}",
]) {
sh """
# initialize Helm without installing Tiller
helm init --client-only --service-account ${SERVICE_ACCOUNT}
# add local chart repository
helm repo add jenkins-x http://chartmuseum.jenkins-x.io
# replace env vars in values.yaml
# specify them explicitly to not replace DOCKER_REGISTRY which needs to be relative to the upgraded namespace:
# webui-staging (PR) or webui (master)
envsubst '\${NAMESPACE} \${INTERNAL_DOCKER_REGISTRY} \${DOCKER_REGISTRY_CONFIG} \${NPM_TOKEN} \${PACKAGES_PASSWORD} \${DRY_RUN}' < values.yaml > myvalues.yaml
# upgrade Jenkins X platform
jx upgrade platform --namespace=${NAMESPACE} \
--version 2.0.1824 \
--local-cloud-environment \
--always-upgrade \
--cleanup-temp-files=true \
--batch-mode
./tools/podTemplateTolerationPatch.sh
# restart Jenkins pod
kubectl scale deployment jenkins -n ${NAMESPACE} --replicas 0
kubectl scale deployment jenkins -n ${NAMESPACE} --replicas 1
"""
}
}
}
}
post {
success {
setGitHubBuildStatus('upgrade', 'Upgrade Jenkins X platform', 'SUCCESS')
}
failure {
setGitHubBuildStatus('upgrade', 'Upgrade Jenkins X platform', 'FAILURE')
}
}
}
stage('Perform Git release') {
// TODO: skip if no changes since latest tag, to be able to manually run the pipeline on the master branch
// in order to revert a bad "jx upgrade platform" launched by a PR. This would run "jx upgrade platform"
// on the latest (stable) tag, aka master, without adding add an extra Git tag.
when {
branch 'master'
}
steps {
setGitHubBuildStatus('release', 'Release', 'PENDING')
container('jx-base') {
script {
VERSION = sh(returnStdout: true, script: 'jx-release-version')
}
withEnv(["VERSION=${VERSION}"]) {
sh """
# ensure we're not on a detached head
git checkout master
# create the Git credentials
jx step git credentials
git config credential.helper store
# Git tag
jx step tag -v ${VERSION}
# Git release
jx step changelog -v v${VERSION}
"""
}
}
}
post {
always {
step([$class: 'JiraIssueUpdater', issueSelector: [$class: 'DefaultIssueSelector'], scm: scm])
}
success {
setGitHubBuildStatus('release', 'Release', 'SUCCESS')
}
failure {
setGitHubBuildStatus('release', 'Release', 'FAILURE')
}
}
}
}
}