Skip to content

Commit 2f83bf5

Browse files
authored
Merge pull request #13 from thelamer/master
adding updated build logic for package tagging to repo
2 parents 74ad51f + 13e4272 commit 2f83bf5

File tree

1 file changed

+193
-112
lines changed

1 file changed

+193
-112
lines changed

Jenkinsfile

Lines changed: 193 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ pipeline {
22
agent {
33
label 'X86-64-MULTI'
44
}
5-
// Configuration for the variables used for this specific repo
5+
// Input to determine if this is a package check
6+
parameters {
7+
string(defaultValue: 'false', description: 'package check run', name: 'PACKAGE_CHECK')
8+
}
9+
// Configuraiton for the variables used for this specific repo
610
environment {
711
EXT_GIT_BRANCH = 'master'
812
EXT_USER = 'bookstackapp'
@@ -17,28 +21,12 @@ pipeline {
1721
BUILDS_DISCORD = credentials('build_webhook_url')
1822
GITHUB_TOKEN = credentials('498b4638-2d02-4ce5-832d-8a57d01d97ab')
1923
DIST_IMAGE = 'alpine'
20-
DIST_TAG = '3.8'
21-
DIST_PACKAGES = 'curl \
22-
memcached \
23-
php7-openssl \
24-
php7-pdo_mysql \
25-
php7-mbstring \
26-
php7-memcached \
27-
php7-tidy \
28-
php7-phar \
29-
php7-dom \
30-
php7-tokenizer \
31-
php7-gd \
32-
php7-mysqlnd \
33-
php7-tidy \
34-
php7-simplexml \
35-
tar'
3624
MULTIARCH = 'true'
3725
CI = 'true'
3826
CI_WEB = 'true'
3927
CI_PORT = '80'
4028
CI_SSL = 'false'
41-
CI_DELAY = '10'
29+
CI_DELAY = '120'
4230
TEST_MYSQL_HOST = credentials('mysql_test_host')
4331
TEST_MYSQL_PASSWORD = credentials('mysql_test_password')
4432
CI_AUTH = 'user:password'
@@ -49,6 +37,7 @@ pipeline {
4937
stage("Set ENV Variables base"){
5038
steps{
5139
script{
40+
env.EXIT_STATUS = ''
5241
env.LS_RELEASE = sh(
5342
script: '''curl -s https://api.github.com/repos/${LS_USER}/${LS_REPO}/releases/latest | jq -r '. | .tag_name' ''',
5443
returnStdout: true).trim()
@@ -88,14 +77,21 @@ pipeline {
8877
/* #######################
8978
Package Version Tagging
9079
####################### */
91-
// If this is an alpine base image determine the base package tag to use
92-
stage("Set Package tag Alpine"){
80+
// Grab the current package versions in Git to determine package tag
81+
stage("Set Package tag"){
9382
steps{
94-
sh '''docker pull alpine:${DIST_TAG}'''
9583
script{
9684
env.PACKAGE_TAG = sh(
97-
script: '''docker run --rm alpine:${DIST_TAG} sh -c 'apk update --quiet\
98-
&& apk info '"${DIST_PACKAGES}"' | md5sum | cut -c1-8' ''',
85+
script: '''#!/bin/bash
86+
http_code=$(curl --write-out %{http_code} -s -o /dev/null \
87+
https://raw.githubusercontent.com/${LS_USER}/${LS_REPO}/master/package_versions.txt)
88+
if [[ "${http_code}" -ne 200 ]] ; then
89+
echo none
90+
else
91+
curl -s \
92+
https://raw.githubusercontent.com/${LS_USER}/${LS_REPO}/master/package_versions.txt \
93+
| md5sum | cut -c1-8
94+
fi''',
9995
returnStdout: true).trim()
10096
}
10197
}
@@ -222,99 +218,173 @@ pipeline {
222218
}
223219
steps {
224220
script{
225-
env.CI_URL = 'README_UPDATE'
226-
env.RELEASE_LINK = 'README_UPDATE'
227-
currentBuild.rawBuild.result = Result.ABORTED
228-
throw new hudson.AbortException('ABORTED_README')
221+
env.EXIT_STATUS = 'ABORTED'
229222
}
230223
}
231224
}
232225
/* ###############
233226
Build Container
234227
############### */
235-
// Build Docker container for push to LS Repo
236-
stage('Build-Single') {
237-
when {
238-
environment name: 'MULTIARCH', value: 'false'
239-
}
240-
steps {
241-
sh "docker build --no-cache -t ${IMAGE}:${META_TAG} \
242-
--build-arg ${BUILD_VERSION_ARG}=${EXT_RELEASE} --build-arg VERSION=\"${META_TAG}\" --build-arg BUILD_DATE=${GITHUB_DATE} ."
243-
}
244-
}
245-
// Build MultiArch Docker containers for push to LS Repo
246-
stage('Build-Multi') {
247-
when {
248-
environment name: 'MULTIARCH', value: 'true'
249-
}
250-
parallel {
251-
stage('Build X86') {
252-
steps {
253-
sh "docker build --no-cache -t ${IMAGE}:amd64-${META_TAG} \
254-
--build-arg ${BUILD_VERSION_ARG}=${EXT_RELEASE} --build-arg VERSION=\"${META_TAG}\" --build-arg BUILD_DATE=${GITHUB_DATE} ."
255-
}
256-
}
257-
stage('Build ARMHF') {
258-
agent {
259-
label 'ARMHF'
260-
}
261-
steps {
262-
withCredentials([
263-
[
264-
$class: 'UsernamePasswordMultiBinding',
265-
credentialsId: '3f9ba4d5-100d-45b0-a3c4-633fd6061207',
266-
usernameVariable: 'DOCKERUSER',
267-
passwordVariable: 'DOCKERPASS'
268-
]
269-
]) {
270-
echo 'Logging into DockerHub'
271-
sh '''#! /bin/bash
272-
echo $DOCKERPASS | docker login -u $DOCKERUSER --password-stdin
273-
'''
274-
sh "curl https://lsio-ci.ams3.digitaloceanspaces.com/qemu-arm-static -o qemu-arm-static"
275-
sh "chmod +x qemu-*"
276-
sh "docker build --no-cache -f Dockerfile.armhf -t ${IMAGE}:arm32v6-${META_TAG} \
277-
--build-arg ${BUILD_VERSION_ARG}=${EXT_RELEASE} --build-arg VERSION=\"${META_TAG}\" --build-arg BUILD_DATE=${GITHUB_DATE} ."
278-
sh "docker tag ${IMAGE}:arm32v6-${META_TAG} lsiodev/buildcache:arm32v6-${COMMIT_SHA}-${BUILD_NUMBER}"
279-
sh "docker push lsiodev/buildcache:arm32v6-${COMMIT_SHA}-${BUILD_NUMBER}"
280-
}
281-
}
282-
}
283-
stage('Build ARM64') {
284-
agent {
285-
label 'ARM64'
286-
}
287-
steps {
288-
withCredentials([
289-
[
290-
$class: 'UsernamePasswordMultiBinding',
291-
credentialsId: '3f9ba4d5-100d-45b0-a3c4-633fd6061207',
292-
usernameVariable: 'DOCKERUSER',
293-
passwordVariable: 'DOCKERPASS'
294-
]
295-
]) {
296-
echo 'Logging into DockerHub'
297-
sh '''#! /bin/bash
298-
echo $DOCKERPASS | docker login -u $DOCKERUSER --password-stdin
299-
'''
300-
sh "curl https://lsio-ci.ams3.digitaloceanspaces.com/qemu-aarch64-static -o qemu-aarch64-static"
301-
sh "chmod +x qemu-*"
302-
sh "docker build --no-cache -f Dockerfile.aarch64 -t ${IMAGE}:arm64v8-${META_TAG} \
303-
--build-arg ${BUILD_VERSION_ARG}=${EXT_RELEASE} --build-arg VERSION=\"${META_TAG}\" --build-arg BUILD_DATE=${GITHUB_DATE} ."
304-
sh "docker tag ${IMAGE}:arm64v8-${META_TAG} lsiodev/buildcache:arm64v8-${COMMIT_SHA}-${BUILD_NUMBER}"
305-
sh "docker push lsiodev/buildcache:arm64v8-${COMMIT_SHA}-${BUILD_NUMBER}"
306-
}
307-
}
308-
}
309-
}
310-
}
228+
// Build Docker container for push to LS Repo
229+
stage('Build-Single') {
230+
when {
231+
environment name: 'MULTIARCH', value: 'false'
232+
environment name: 'EXIT_STATUS', value: ''
233+
}
234+
steps {
235+
sh "docker build --no-cache -t ${IMAGE}:${META_TAG} \
236+
--build-arg ${BUILD_VERSION_ARG}=${EXT_RELEASE} --build-arg VERSION=\"${META_TAG}\" --build-arg BUILD_DATE=${GITHUB_DATE} ."
237+
}
238+
}
239+
// Build MultiArch Docker containers for push to LS Repo
240+
stage('Build-Multi') {
241+
when {
242+
environment name: 'MULTIARCH', value: 'true'
243+
environment name: 'EXIT_STATUS', value: ''
244+
}
245+
parallel {
246+
stage('Build X86') {
247+
steps {
248+
sh "docker build --no-cache -t ${IMAGE}:amd64-${META_TAG} \
249+
--build-arg ${BUILD_VERSION_ARG}=${EXT_RELEASE} --build-arg VERSION=\"${META_TAG}\" --build-arg BUILD_DATE=${GITHUB_DATE} ."
250+
}
251+
}
252+
stage('Build ARMHF') {
253+
agent {
254+
label 'ARMHF'
255+
}
256+
steps {
257+
withCredentials([
258+
[
259+
$class: 'UsernamePasswordMultiBinding',
260+
credentialsId: '3f9ba4d5-100d-45b0-a3c4-633fd6061207',
261+
usernameVariable: 'DOCKERUSER',
262+
passwordVariable: 'DOCKERPASS'
263+
]
264+
]) {
265+
echo 'Logging into DockerHub'
266+
sh '''#! /bin/bash
267+
echo $DOCKERPASS | docker login -u $DOCKERUSER --password-stdin
268+
'''
269+
sh "curl https://lsio-ci.ams3.digitaloceanspaces.com/qemu-arm-static -o qemu-arm-static"
270+
sh "chmod +x qemu-*"
271+
sh "docker build --no-cache -f Dockerfile.armhf -t ${IMAGE}:arm32v6-${META_TAG} \
272+
--build-arg ${BUILD_VERSION_ARG}=${EXT_RELEASE} --build-arg VERSION=\"${META_TAG}\" --build-arg BUILD_DATE=${GITHUB_DATE} ."
273+
sh "docker tag ${IMAGE}:arm32v6-${META_TAG} lsiodev/buildcache:arm32v6-${COMMIT_SHA}-${BUILD_NUMBER}"
274+
sh "docker push lsiodev/buildcache:arm32v6-${COMMIT_SHA}-${BUILD_NUMBER}"
275+
}
276+
}
277+
}
278+
stage('Build ARM64') {
279+
agent {
280+
label 'ARM64'
281+
}
282+
steps {
283+
withCredentials([
284+
[
285+
$class: 'UsernamePasswordMultiBinding',
286+
credentialsId: '3f9ba4d5-100d-45b0-a3c4-633fd6061207',
287+
usernameVariable: 'DOCKERUSER',
288+
passwordVariable: 'DOCKERPASS'
289+
]
290+
]) {
291+
echo 'Logging into DockerHub'
292+
sh '''#! /bin/bash
293+
echo $DOCKERPASS | docker login -u $DOCKERUSER --password-stdin
294+
'''
295+
sh "curl https://lsio-ci.ams3.digitaloceanspaces.com/qemu-aarch64-static -o qemu-aarch64-static"
296+
sh "chmod +x qemu-*"
297+
sh "docker build --no-cache -f Dockerfile.aarch64 -t ${IMAGE}:arm64v8-${META_TAG} \
298+
--build-arg ${BUILD_VERSION_ARG}=${EXT_RELEASE} --build-arg VERSION=\"${META_TAG}\" --build-arg BUILD_DATE=${GITHUB_DATE} ."
299+
sh "docker tag ${IMAGE}:arm64v8-${META_TAG} lsiodev/buildcache:arm64v8-${COMMIT_SHA}-${BUILD_NUMBER}"
300+
sh "docker push lsiodev/buildcache:arm64v8-${COMMIT_SHA}-${BUILD_NUMBER}"
301+
}
302+
}
303+
}
304+
}
305+
}
306+
// Take the image we just built and dump package versions for comparison
307+
stage('Update-packages') {
308+
when {
309+
branch "master"
310+
environment name: 'CHANGE_ID', value: ''
311+
environment name: 'EXIT_STATUS', value: ''
312+
}
313+
steps {
314+
sh '''#! /bin/bash
315+
TEMPDIR=$(mktemp -d)
316+
if [ "${MULTIARCH}" == "true" ]; then
317+
LOCAL_CONTAINER=${IMAGE}:amd64-${META_TAG}
318+
else
319+
LOCAL_CONTAINER=${IMAGE}:${META_TAG}
320+
fi
321+
if [ "${DIST_IMAGE}" == "alpine" ]; then
322+
docker run --rm -v ${TEMPDIR}:/tmp ${LOCAL_CONTAINER} sh -c '\
323+
apk info > packages && \
324+
apk info -v > versions && \
325+
paste -d " " packages versions > /tmp/package_versions.txt'
326+
elif [ "${DIST_IMAGE}" == "ubuntu" ]; then
327+
docker run --rm -v ${TEMPDIR}:/tmp ${LOCAL_CONTAINER} sh -c '\
328+
apt -qq list --installed | awk "{print \$1,\$2}" > /tmp/package_versions.txt'
329+
fi
330+
if [ "$(md5sum ${TEMPDIR}/package_versions.txt | cut -c1-8 )" != "${PACKAGE_TAG}" ]; then
331+
git clone https://github.com/${LS_USER}/${LS_REPO}.git ${TEMPDIR}/${LS_REPO}
332+
cp ${TEMPDIR}/package_versions.txt ${TEMPDIR}/${LS_REPO}/
333+
cd ${TEMPDIR}/${LS_REPO}/
334+
git --git-dir ${TEMPDIR}/${LS_REPO}/.git add package_versions.txt
335+
git --git-dir ${TEMPDIR}/${LS_REPO}/.git commit -m 'Bot Updating Package Versions'
336+
git --git-dir ${TEMPDIR}/${LS_REPO}/.git push https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/${LS_USER}/${LS_REPO}.git --all
337+
echo "true" > /tmp/packages-${COMMIT_SHA}-${BUILD_NUMBER}
338+
else
339+
echo "false" > /tmp/packages-${COMMIT_SHA}-${BUILD_NUMBER}
340+
fi
341+
rm -Rf ${TEMPDIR}'''
342+
script{
343+
env.PACKAGE_UPDATED = sh(
344+
script: '''cat /tmp/packages-${COMMIT_SHA}-${BUILD_NUMBER}''',
345+
returnStdout: true).trim()
346+
}
347+
}
348+
}
349+
// Exit the build if the package file was just updated
350+
stage('PACKAGE-exit') {
351+
when {
352+
branch "master"
353+
environment name: 'CHANGE_ID', value: ''
354+
environment name: 'PACKAGE_UPDATED', value: 'true'
355+
environment name: 'EXIT_STATUS', value: ''
356+
}
357+
steps {
358+
script{
359+
env.EXIT_STATUS = 'ABORTED'
360+
}
361+
}
362+
}
363+
// Exit the build if this is just a package check and there are no changes to push
364+
stage('PACKAGECHECK-exit') {
365+
when {
366+
branch "master"
367+
environment name: 'CHANGE_ID', value: ''
368+
environment name: 'PACKAGE_UPDATED', value: 'false'
369+
environment name: 'EXIT_STATUS', value: ''
370+
expression {
371+
params.PACKAGE_CHECK == 'true'
372+
}
373+
}
374+
steps {
375+
script{
376+
env.EXIT_STATUS = 'ABORTED'
377+
}
378+
}
379+
}
311380
/* #######
312381
Testing
313382
####### */
314383
// Run Container tests
315384
stage('Test') {
316385
when {
317386
environment name: 'CI', value: 'true'
387+
environment name: 'EXIT_STATUS', value: ''
318388
}
319389
steps {
320390
withCredentials([
@@ -361,6 +431,7 @@ pipeline {
361431
stage('Docker-Push-Single') {
362432
when {
363433
environment name: 'MULTIARCH', value: 'false'
434+
environment name: 'EXIT_STATUS', value: ''
364435
}
365436
steps {
366437
withCredentials([
@@ -385,6 +456,7 @@ pipeline {
385456
stage('Docker-Push-Multi') {
386457
when {
387458
environment name: 'MULTIARCH', value: 'true'
459+
environment name: 'EXIT_STATUS', value: ''
388460
}
389461
steps {
390462
withCredentials([
@@ -435,6 +507,7 @@ pipeline {
435507
env.LS_RELEASE != env.EXT_RELEASE + '-pkg-' + env.PACKAGE_TAG + '-ls' + env.LS_TAG_NUMBER
436508
}
437509
environment name: 'CHANGE_ID', value: ''
510+
environment name: 'EXIT_STATUS', value: ''
438511
}
439512
steps {
440513
echo "Pushing New tag for current commit ${EXT_RELEASE}-pkg-${PACKAGE_TAG}-ls${LS_TAG_NUMBER}"
@@ -460,6 +533,7 @@ pipeline {
460533
stage('Sync-README') {
461534
when {
462535
environment name: 'CHANGE_ID', value: ''
536+
environment name: 'EXIT_STATUS', value: ''
463537
}
464538
steps {
465539
withCredentials([
@@ -487,15 +561,22 @@ pipeline {
487561
Send status to Discord
488562
###################### */
489563
post {
490-
success {
491-
sh ''' curl -X POST --data '{"avatar_url": "https://wiki.jenkins-ci.org/download/attachments/2916393/headshot.png","embeds": [{"color": 1681177,\
492-
"description": "**Build:** '${BUILD_NUMBER}'\\n**CI Results:** '${CI_URL}'\\n**Status:** Success\\n**Job:** '${RUN_DISPLAY_URL}'\\n**Change:** '${CODE_URL}'\\n**External Release:**: '${RELEASE_LINK}'\\n**DockerHub:** '${DOCKERHUB_LINK}'\\n"}],\
493-
"username": "Jenkins"}' ${BUILDS_DISCORD} '''
494-
}
495-
failure {
496-
sh ''' curl -X POST --data '{"avatar_url": "https://wiki.jenkins-ci.org/download/attachments/2916393/headshot.png","embeds": [{"color": 16711680,\
497-
"description": "**Build:** '${BUILD_NUMBER}'\\n**CI Results:** '${CI_URL}'\\n**Status:** failure\\n**Job:** '${RUN_DISPLAY_URL}'\\n**Change:** '${CODE_URL}'\\n**External Release:**: '${RELEASE_LINK}'\\n**DockerHub:** '${DOCKERHUB_LINK}'\\n"}],\
498-
"username": "Jenkins"}' ${BUILDS_DISCORD} '''
564+
always {
565+
script{
566+
if (env.EXIT_STATUS == "ABORTED"){
567+
sh 'echo "build aborted"'
568+
}
569+
else if (currentBuild.currentResult == "SUCCESS"){
570+
sh ''' curl -X POST --data '{"avatar_url": "https://wiki.jenkins-ci.org/download/attachments/2916393/headshot.png","embeds": [{"color": 1681177,\
571+
"description": "**Build:** '${BUILD_NUMBER}'\\n**CI Results:** '${CI_URL}'\\n**Status:** Success\\n**Job:** '${RUN_DISPLAY_URL}'\\n**Change:** '${CODE_URL}'\\n**External Release:**: '${RELEASE_LINK}'\\n**DockerHub:** '${DOCKERHUB_LINK}'\\n"}],\
572+
"username": "Jenkins"}' ${BUILDS_DISCORD} '''
573+
}
574+
else {
575+
sh ''' curl -X POST --data '{"avatar_url": "https://wiki.jenkins-ci.org/download/attachments/2916393/headshot.png","embeds": [{"color": 16711680,\
576+
"description": "**Build:** '${BUILD_NUMBER}'\\n**CI Results:** '${CI_URL}'\\n**Status:** failure\\n**Job:** '${RUN_DISPLAY_URL}'\\n**Change:** '${CODE_URL}'\\n**External Release:**: '${RELEASE_LINK}'\\n**DockerHub:** '${DOCKERHUB_LINK}'\\n"}],\
577+
"username": "Jenkins"}' ${BUILDS_DISCORD} '''
578+
}
579+
}
499580
}
500581
}
501582
}

0 commit comments

Comments
 (0)