forked from jenkins-zh/jenkins-zh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
227 lines (216 loc) · 9.38 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
pipeline {
agent {
label "hugo"
}
options {
disableConcurrentBuilds()
quietPeriod 5
timeout(time: 30, unit: 'MINUTES')
}
parameters {
string defaultValue: '', description: '', name: 'previewUpstream', trim: true
string defaultValue: '', description: '', name: 'previewUpstreamPR', trim: true
}
triggers {
upstream(upstreamProjects: 'jenkins-zh/wechat/master/,jenkins-zh/hugo-jenkins-theme/master/', threshold: hudson.model.Result.SUCCESS)
}
stages{
stage("Fetch source"){
failFast true
parallel {
stage('Clone site'){
steps{
gitClone('https://github.com/jenkins-zh/jenkins-zh.github.io', 'jenkins-zh.github.io')
}
}
stage("Fetch theme"){
steps{
gitClone('https://github.com/jenkins-zh/hugo-jenkins-theme', 'themes/hugo-jenkins-theme')
}
}
stage("Fetch wechat articles"){
steps{
gitClone('https://github.com/jenkins-infra/wechat', 'content/wechat')
script{
if(params.previewUpstreamPR != ''){
sh 'cd content/wechat && git fetch origin +refs/pull/' + params.previewUpstreamPR + '/merge'
sh '''
cd content/wechat && git checkout FETCH_HEAD
'''
}
}
sh '''
pwd
ls -ahl
cd content/wechat/articles && rm sample.md && find . -name *.md -exec mv {} . \\;
rm -rfv content/wechat/articles/2018
rm -rfv content/wechat/images/*.md
rm -rfv content/wechat/management/
rm -rfv content/wechat/*.md
'''
}
}
}
}
stage("Build"){
steps{
script {
def baseUrl = "https://jenkins-zh.cn/"
def environment = "production"
if(params.previewUpstream != ''){
baseUrl = "http://" + params.previewUpstream.toLowerCase() + ".preview.jenkins-zh.cn/"
environment = "preview"
}else if (env.BRANCH_NAME != "master") {
baseUrl = "http://" + env.BRANCH_NAME.toLowerCase() + ".preview.jenkins-zh.cn/"
baseUrl = "http://" + params.previewUpstream.toLowerCase() + ".preview.jenkins-zh.cn/"
environment = "preview"
}
hugo destination: 'jenkins-zh.github.io', buildFuture: true, verbose: true, baseUrl: baseUrl, environment: environment
}
}
}
stage("Image"){
when {
anyOf {
expression {
return params.previewUpstream != ''
}
not {
branch 'master'
}
}
}
steps{
container('tools'){
script {
if(params.previewUpstream != ''){
env.BRANCH_NAME = params.previewUpstream
} else {
echo 'preview upstream is: ' + params.previewUpstream + '.'
}
env.BRANCH_NAME = env.BRANCH_NAME.toLowerCase()
withCredentials([usernamePassword(credentialsId: 'jenkins-zh-docker', passwordVariable: 'PASSWD', usernameVariable: 'USER')]) {
sh '''
docker build . -t surenpi/jenkins-zh:v$BRANCH_NAME-$BUILD_ID
docker login --username $USER --password $PASSWD
'''
retry(3) {
timeout(3) {
sh '''
docker push surenpi/jenkins-zh:v$BRANCH_NAME-$BUILD_ID
docker logout
'''
}
}
}
}
}
}
}
stage("Preview"){
when {
allOf {
expression {
return params.previewUpstream != ''
}
not {
branch 'master'
}
expression {
return false // skip this stage for tech reason
}
}
}
steps{
container('tools'){
sh '''
rm -rfv website-ns.yaml website-deploy.yaml website-ingress.yaml website-service.yaml
'''
script{
if(params.previewUpstream != ''){
env.BRANCH_NAME = params.previewUpstream
}
env.BRANCH_NAME = env.BRANCH_NAME.toLowerCase()
def website = readYaml file: "config/website.yaml"
for(item in website){
switch(item.kind) {
case "Deployment":
item.spec.template.spec.containers[0].image = "surenpi/jenkins-zh:v$BRANCH_NAME-$BUILD_ID"
echo 'going to write website-deploy.yaml'
break;
case "Ingress":
item.spec.rules[0].host = "${BRANCH_NAME}.preview.jenkins-zh.cn"
echo 'going to write website-ingress.yaml'
break;
case "Namespace":
item.metadata.name = "${BRANCH_NAME}"
echo 'going to write website-ns.yaml'
break;
}
}
writeYaml file: 'website-ns.yaml', data: website[0]
writeYaml file: 'website-deploy.yaml', data: website[1]
writeYaml file: 'website-service.yaml', data: website[2]
writeYaml file: 'website-ingress.yaml', data: website[3]
sh '''
cat website-ns.yaml
kubectl apply -f website-ns.yaml
cat website-deploy.yaml
kubectl apply -f website-deploy.yaml -n $BRANCH_NAME
cat website-service.yaml
kubectl apply -f website-service.yaml -n $BRANCH_NAME
cat website-ingress.yaml
kubectl apply -f website-ingress.yaml -n $BRANCH_NAME
'''
if(params.previewUpstream != '') {
echo 'preview upstream is: ' + params.previewUpstream
} else {
pullRequest.createStatus(status: 'success',
context: 'continuous-integration/jenkins/pr-merge/preview',
description: 'Website preview',
targetUrl: "http://${BRANCH_NAME}.preview.jenkins-zh.cn")
}
}
}
}
}
stage("Publish"){
when {
allOf {
expression {
return params.previewUpstream == ''
}
branch 'master'
}
}
steps{
hugoGitPublish authorEmail: '[email protected]', authorName: 'suren',
commitLog: 'Auto commit by hugo-plugin.',
publishDir: 'jenkins-zh.github.io',
committerEmail: '[email protected]', committerName: 'suren',
credentialsId: 'a832798a-0513-4d45-a47a-95d152dc915a',
publishBranch: env.BRANCH_NAME,
targetUrl: 'https://github.com/jenkins-zh/jenkins-zh.github.io'
}
}
stage("Notify"){
when {
allOf {
expression {
return params.previewUpstream == ''
}
branch 'master'
}
}
steps{
mail from: '[email protected]',
to: '[email protected]',
subject: 'Jenkins 中文社区网站发布成功!',
body: '访问地址 https://jenkins-zh.cn/'
}
}
}
}
def gitClone(url, subDir){
checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: subDir]], submoduleCfg: [], userRemoteConfigs: [[url: url]]])
}