-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathJenkinsfile
233 lines (194 loc) · 8.03 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
232
233
properties([
buildDiscarder(logRotator(numToKeepStr: '10')),
pipelineTriggers([
[$class:"SCMTrigger", scmpoll_spec:"H/10 * * * *"],
]),
disableConcurrentBuilds()
])
def label = "packages-${UUID.randomUUID().toString()}"
def HELM_VERSION = "3.9.2"
def needToDeploy() {
return BRANCH_NAME == 'master' || BRANCH_NAME == 'develop'
}
def isStaging() {
return BRANCH_NAME == 'develop'
}
podTemplate(
label: label,
containers: [
containerTemplate(
name: 'jekyll',
image: 'docker.io/jekyll/jekyll:4.1.0',
ttyEnabled: true,
command: 'cat',
resourceRequestMemory: '4Gi',
resourceLimitMemory: '4Gi',
)
],
volumes: [
emptyDirVolume(mountPath: '/home/jenkins', memory: false),
configMapVolume(mountPath: '/home/jenkins/.ssh', configMapName: 'known-hosts', subPath: '.')
]
) {
node(label) {
timeout(time: 60, unit: "MINUTES") {
container('jnlp') {
withEnv([
"PROJECT_NAME=packages",
"PROJECT_BOT_NAME=Eclipse IoT Packages Bot",
"HELM_VERSION=${HELM_VERSION}",
]) {
deleteDir()
try {
stage ('Init') {
sh '''
pwd
env | sort
'''
}
stage ('Clone') {
dir ("build") {
checkout scm
}
}
stage('Helm Install') {
sh '''
curl --silent --location https://get.helm.sh/helm-v${HELM_VERSION}-linux-amd64.tar.gz --output helm.tar.gz
tar --strip-components=1 -xvzf helm.tar.gz linux-amd64/helm
'''
}
} catch (err) {
currentBuild.result = 'FAILED'
throw err
} /* try */
} /* with-env */
} /* container (jnlp) */
container('jekyll') {
withEnv([
"PROJECT_NAME=packages",
"PROJECT_BOT_NAME=Eclipse IoT Packages Bot",
"HELM_VERSION=${HELM_VERSION}",
"XDG_CACHE_HOME=${env.WORKSPACE}/helm-cache",
"XDG_CONFIG_HOME=${env.WORKSPACE}/helm-config",
"XDG_DATA_HOME=${env.WORKSPACE}/helm-data",
]) {
try {
stage('Helm Build') {
dir("build") {
sh '''
mkdir -p helm-repository
for i in $(ls charts/*/Chart.yaml packages/*/Chart.yaml); do
echo "Building $(dirname $i) ..."
${WORKSPACE}/helm package -u "$(dirname $i)" -d helm-repository
done
find helm-repository
'''
}
}
stage ('Jekyll Dependencies') {
dir ("build/homepage") {
sh '''
cp -rd /usr/gem/. /usr/local/bundle/
bundle install --path /usr/local/bundle/
'''
}
}
stage ('Jekyll Build') {
dir ("build/homepage") {
if ( isStaging() ) {
sh 'bundle exec ruby /usr/gem/bin/jekyll build --config _config.yml,_config_staging.yml'
} else {
sh 'bundle exec ruby /usr/gem/bin/jekyll build'
}
}
}
} catch (err) {
currentBuild.result = 'FAILED'
throw err
}
} /* with-env */
} /* container (jekyll) */
container('jnlp') {
withEnv([
"PROJECT_NAME=packages",
"PROJECT_BOT_NAME=Eclipse IoT Packages Bot",
]) {
try {
if ( needToDeploy() ) {
stage ('Checkout website') {
dir ("www") {
sshagent(['github-bot-ssh']) {
sh '''
if [ ${BRANCH_NAME} == "develop" ]; then
BRANCH_NAME="staging"
fi
git config --global user.email "${PROJECT_NAME}[email protected]"
git config --global user.name "${PROJECT_BOT_NAME}"
git clone [email protected]:eclipse/${PROJECT_NAME}-website.git .
git checkout ${BRANCH_NAME}
'''
}
}
}
stage ('Copy content') {
// delete the whole directory, and re-populate with the new content
sh 'rm -rf www/* && cp -Rvf build/homepage/_site/* www/'
// pre-fill with the published charts and index from the git repository
dir ("www") {
sh 'git checkout -- charts/'
}
// and only copy new packages, add marker file
sh '''
mkdir build/helm-add
for i in $(ls build/helm-repository/*.tgz); do
if [ ! -f "www/charts/$(basename $i)" ]; then
echo "Publishing $(basename $i) ..."
cp -v "$i" build/helm-add/
touch build/perform-merge
fi
done
'''
}
// only update the index, when it is missing
if (fileExists('build/perform-merge')) {
stage ('Rebuild helm Index') {
// merge added content with existing index
if ( isStaging() ) {
sh '${WORKSPACE}/helm repo index --merge www/charts/index.yaml build/helm-add --url https://staging.eclipse.org/packages/charts'
} else {
sh '${WORKSPACE}/helm repo index --merge www/charts/index.yaml build/helm-add --url https://eclipse.org/packages/charts'
}
// copy new content and index to www-repo
sh 'cp -v build/helm-add/* www/charts/'
}
} /* if */
stage ('Commit and push') {
dir("www") {
sshagent(['github-bot-ssh']) {
sh '''
if [ ${BRANCH_NAME} == "develop" ]; then
BRANCH_NAME="staging"
fi
git add -A
if ! git diff --cached --exit-code; then
echo "Changes have been detected, publishing to repo 'www.eclipse.org/${PROJECT_NAME}'"
git commit -m "Website build ${JOB_NAME}-${BUILD_NUMBER}"
git log --graph --abbrev-commit --date=relative -n 5
git push origin HEAD:${BRANCH_NAME}
else
echo "No changes have been detected since last build, nothing to publish"
fi
'''
}
}
}
}
} catch (err) {
currentBuild.result = 'FAILED'
throw err
}
} /* with-env */
} /* container (jnlp) */
} /* timeout */
} /* node */
} /* pod */