Skip to content

Commit 89d1468

Browse files
authored
Merge pull request #10 from thelamer/master
adding updated build logic for package tagging to repo
2 parents a362965 + 21b4c09 commit 89d1468

File tree

1 file changed

+192
-101
lines changed

1 file changed

+192
-101
lines changed

Jenkinsfile

Lines changed: 192 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ 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_RELEASE_TYPE = 'pip_version'
8-
EXT_GIT_BRANCH = 'none'
912
EXT_PIP = 'python-ldap'
1013
BUILD_VERSION_ARG = 'LDAP_VERSION'
1114
LS_USER = 'linuxserver'
@@ -17,11 +20,6 @@ pipeline {
1720
BUILDS_DISCORD = credentials('build_webhook_url')
1821
GITHUB_TOKEN = credentials('498b4638-2d02-4ce5-832d-8a57d01d97ab')
1922
DIST_IMAGE = 'alpine'
20-
DIST_TAG = '3.8'
21-
DIST_PACKAGES = 'libffi \
22-
libldap \
23-
py2-pip \
24-
python2'
2523
MULTIARCH='true'
2624
CI='true'
2725
CI_WEB='false'
@@ -37,6 +35,7 @@ pipeline {
3735
stage("Set ENV Variables base"){
3836
steps{
3937
script{
38+
env.EXIT_STATUS = ''
4039
env.LS_RELEASE = sh(
4140
script: '''curl -s https://api.github.com/repos/${LS_USER}/${LS_REPO}/releases/latest | jq -r '. | .tag_name' ''',
4241
returnStdout: true).trim()
@@ -76,14 +75,21 @@ pipeline {
7675
/* #######################
7776
Package Version Tagging
7877
####################### */
79-
// If this is an alpine base image determine the base package tag to use
80-
stage("Set Package tag Alpine"){
78+
// Grab the current package versions in Git to determine package tag
79+
stage("Set Package tag"){
8180
steps{
82-
sh '''docker pull alpine:${DIST_TAG}'''
8381
script{
8482
env.PACKAGE_TAG = sh(
85-
script: '''docker run --rm alpine:${DIST_TAG} sh -c 'apk update --quiet\
86-
&& apk info '"${DIST_PACKAGES}"' | md5sum | cut -c1-8' ''',
83+
script: '''#!/bin/bash
84+
http_code=$(curl --write-out %{http_code} -s -o /dev/null \
85+
https://raw.githubusercontent.com/${LS_USER}/${LS_REPO}/master/package_versions.txt)
86+
if [[ "${http_code}" -ne 200 ]] ; then
87+
echo none
88+
else
89+
curl -s \
90+
https://raw.githubusercontent.com/${LS_USER}/${LS_REPO}/master/package_versions.txt \
91+
| md5sum | cut -c1-8
92+
fi''',
8793
returnStdout: true).trim()
8894
}
8995
}
@@ -203,99 +209,173 @@ pipeline {
203209
}
204210
steps {
205211
script{
206-
env.CI_URL = 'README_UPDATE'
207-
env.RELEASE_LINK = 'README_UPDATE'
208-
currentBuild.rawBuild.result = Result.ABORTED
209-
throw new hudson.AbortException('ABORTED_README')
212+
env.EXIT_STATUS = 'ABORTED'
210213
}
211214
}
212215
}
213216
/* ###############
214217
Build Container
215218
############### */
216-
// Build Docker container for push to LS Repo
217-
stage('Build-Single') {
218-
when {
219-
environment name: 'MULTIARCH', value: 'false'
220-
}
221-
steps {
222-
sh "docker build --no-cache -t ${IMAGE}:${META_TAG} \
223-
--build-arg ${BUILD_VERSION_ARG}=${EXT_RELEASE} --build-arg VERSION=\"${META_TAG}\" --build-arg BUILD_DATE=${GITHUB_DATE} ."
224-
}
225-
}
226-
// Build MultiArch Docker containers for push to LS Repo
227-
stage('Build-Multi') {
228-
when {
229-
environment name: 'MULTIARCH', value: 'true'
230-
}
231-
parallel {
232-
stage('Build X86') {
233-
steps {
234-
sh "docker build --no-cache -t ${IMAGE}:amd64-${META_TAG} \
235-
--build-arg ${BUILD_VERSION_ARG}=${EXT_RELEASE} --build-arg VERSION=\"${META_TAG}\" --build-arg BUILD_DATE=${GITHUB_DATE} ."
236-
}
237-
}
238-
stage('Build ARMHF') {
239-
agent {
240-
label 'ARMHF'
241-
}
242-
steps {
243-
withCredentials([
244-
[
245-
$class: 'UsernamePasswordMultiBinding',
246-
credentialsId: '3f9ba4d5-100d-45b0-a3c4-633fd6061207',
247-
usernameVariable: 'DOCKERUSER',
248-
passwordVariable: 'DOCKERPASS'
249-
]
250-
]) {
251-
echo 'Logging into DockerHub'
252-
sh '''#! /bin/bash
253-
echo $DOCKERPASS | docker login -u $DOCKERUSER --password-stdin
254-
'''
255-
sh "curl https://lsio-ci.ams3.digitaloceanspaces.com/qemu-arm-static -o qemu-arm-static"
256-
sh "chmod +x qemu-*"
257-
sh "docker build --no-cache -f Dockerfile.armhf -t ${IMAGE}:arm32v6-${META_TAG} \
258-
--build-arg ${BUILD_VERSION_ARG}=${EXT_RELEASE} --build-arg VERSION=\"${META_TAG}\" --build-arg BUILD_DATE=${GITHUB_DATE} ."
259-
sh "docker tag ${IMAGE}:arm32v6-${META_TAG} lsiodev/buildcache:arm32v6-${COMMIT_SHA}-${BUILD_NUMBER}"
260-
sh "docker push lsiodev/buildcache:arm32v6-${COMMIT_SHA}-${BUILD_NUMBER}"
261-
}
262-
}
263-
}
264-
stage('Build ARM64') {
265-
agent {
266-
label 'ARM64'
267-
}
268-
steps {
269-
withCredentials([
270-
[
271-
$class: 'UsernamePasswordMultiBinding',
272-
credentialsId: '3f9ba4d5-100d-45b0-a3c4-633fd6061207',
273-
usernameVariable: 'DOCKERUSER',
274-
passwordVariable: 'DOCKERPASS'
275-
]
276-
]) {
277-
echo 'Logging into DockerHub'
278-
sh '''#! /bin/bash
279-
echo $DOCKERPASS | docker login -u $DOCKERUSER --password-stdin
280-
'''
281-
sh "curl https://lsio-ci.ams3.digitaloceanspaces.com/qemu-aarch64-static -o qemu-aarch64-static"
282-
sh "chmod +x qemu-*"
283-
sh "docker build --no-cache -f Dockerfile.aarch64 -t ${IMAGE}:arm64v8-${META_TAG} \
284-
--build-arg ${BUILD_VERSION_ARG}=${EXT_RELEASE} --build-arg VERSION=\"${META_TAG}\" --build-arg BUILD_DATE=${GITHUB_DATE} ."
285-
sh "docker tag ${IMAGE}:arm64v8-${META_TAG} lsiodev/buildcache:arm64v8-${COMMIT_SHA}-${BUILD_NUMBER}"
286-
sh "docker push lsiodev/buildcache:arm64v8-${COMMIT_SHA}-${BUILD_NUMBER}"
287-
}
288-
}
289-
}
290-
}
291-
}
219+
// Build Docker container for push to LS Repo
220+
stage('Build-Single') {
221+
when {
222+
environment name: 'MULTIARCH', value: 'false'
223+
environment name: 'EXIT_STATUS', value: ''
224+
}
225+
steps {
226+
sh "docker build --no-cache -t ${IMAGE}:${META_TAG} \
227+
--build-arg ${BUILD_VERSION_ARG}=${EXT_RELEASE} --build-arg VERSION=\"${META_TAG}\" --build-arg BUILD_DATE=${GITHUB_DATE} ."
228+
}
229+
}
230+
// Build MultiArch Docker containers for push to LS Repo
231+
stage('Build-Multi') {
232+
when {
233+
environment name: 'MULTIARCH', value: 'true'
234+
environment name: 'EXIT_STATUS', value: ''
235+
}
236+
parallel {
237+
stage('Build X86') {
238+
steps {
239+
sh "docker build --no-cache -t ${IMAGE}:amd64-${META_TAG} \
240+
--build-arg ${BUILD_VERSION_ARG}=${EXT_RELEASE} --build-arg VERSION=\"${META_TAG}\" --build-arg BUILD_DATE=${GITHUB_DATE} ."
241+
}
242+
}
243+
stage('Build ARMHF') {
244+
agent {
245+
label 'ARMHF'
246+
}
247+
steps {
248+
withCredentials([
249+
[
250+
$class: 'UsernamePasswordMultiBinding',
251+
credentialsId: '3f9ba4d5-100d-45b0-a3c4-633fd6061207',
252+
usernameVariable: 'DOCKERUSER',
253+
passwordVariable: 'DOCKERPASS'
254+
]
255+
]) {
256+
echo 'Logging into DockerHub'
257+
sh '''#! /bin/bash
258+
echo $DOCKERPASS | docker login -u $DOCKERUSER --password-stdin
259+
'''
260+
sh "curl https://lsio-ci.ams3.digitaloceanspaces.com/qemu-arm-static -o qemu-arm-static"
261+
sh "chmod +x qemu-*"
262+
sh "docker build --no-cache -f Dockerfile.armhf -t ${IMAGE}:arm32v6-${META_TAG} \
263+
--build-arg ${BUILD_VERSION_ARG}=${EXT_RELEASE} --build-arg VERSION=\"${META_TAG}\" --build-arg BUILD_DATE=${GITHUB_DATE} ."
264+
sh "docker tag ${IMAGE}:arm32v6-${META_TAG} lsiodev/buildcache:arm32v6-${COMMIT_SHA}-${BUILD_NUMBER}"
265+
sh "docker push lsiodev/buildcache:arm32v6-${COMMIT_SHA}-${BUILD_NUMBER}"
266+
}
267+
}
268+
}
269+
stage('Build ARM64') {
270+
agent {
271+
label 'ARM64'
272+
}
273+
steps {
274+
withCredentials([
275+
[
276+
$class: 'UsernamePasswordMultiBinding',
277+
credentialsId: '3f9ba4d5-100d-45b0-a3c4-633fd6061207',
278+
usernameVariable: 'DOCKERUSER',
279+
passwordVariable: 'DOCKERPASS'
280+
]
281+
]) {
282+
echo 'Logging into DockerHub'
283+
sh '''#! /bin/bash
284+
echo $DOCKERPASS | docker login -u $DOCKERUSER --password-stdin
285+
'''
286+
sh "curl https://lsio-ci.ams3.digitaloceanspaces.com/qemu-aarch64-static -o qemu-aarch64-static"
287+
sh "chmod +x qemu-*"
288+
sh "docker build --no-cache -f Dockerfile.aarch64 -t ${IMAGE}:arm64v8-${META_TAG} \
289+
--build-arg ${BUILD_VERSION_ARG}=${EXT_RELEASE} --build-arg VERSION=\"${META_TAG}\" --build-arg BUILD_DATE=${GITHUB_DATE} ."
290+
sh "docker tag ${IMAGE}:arm64v8-${META_TAG} lsiodev/buildcache:arm64v8-${COMMIT_SHA}-${BUILD_NUMBER}"
291+
sh "docker push lsiodev/buildcache:arm64v8-${COMMIT_SHA}-${BUILD_NUMBER}"
292+
}
293+
}
294+
}
295+
}
296+
}
297+
// Take the image we just built and dump package versions for comparison
298+
stage('Update-packages') {
299+
when {
300+
branch "master"
301+
environment name: 'CHANGE_ID', value: ''
302+
environment name: 'EXIT_STATUS', value: ''
303+
}
304+
steps {
305+
sh '''#! /bin/bash
306+
TEMPDIR=$(mktemp -d)
307+
if [ "${MULTIARCH}" == "true" ]; then
308+
LOCAL_CONTAINER=${IMAGE}:amd64-${META_TAG}
309+
else
310+
LOCAL_CONTAINER=${IMAGE}:${META_TAG}
311+
fi
312+
if [ "${DIST_IMAGE}" == "alpine" ]; then
313+
docker run --rm -v ${TEMPDIR}:/tmp ${LOCAL_CONTAINER} sh -c '\
314+
apk info > packages && \
315+
apk info -v > versions && \
316+
paste -d " " packages versions > /tmp/package_versions.txt'
317+
elif [ "${DIST_IMAGE}" == "ubuntu" ]; then
318+
docker run --rm -v ${TEMPDIR}:/tmp ${LOCAL_CONTAINER} sh -c '\
319+
apt -qq list --installed | awk "{print \$1,\$2}" > /tmp/package_versions.txt'
320+
fi
321+
if [ "$(md5sum ${TEMPDIR}/package_versions.txt | cut -c1-8 )" != "${PACKAGE_TAG}" ]; then
322+
git clone https://github.com/${LS_USER}/${LS_REPO}.git ${TEMPDIR}/${LS_REPO}
323+
cp ${TEMPDIR}/package_versions.txt ${TEMPDIR}/${LS_REPO}/
324+
cd ${TEMPDIR}/${LS_REPO}/
325+
git --git-dir ${TEMPDIR}/${LS_REPO}/.git add package_versions.txt
326+
git --git-dir ${TEMPDIR}/${LS_REPO}/.git commit -m 'Bot Updating Package Versions'
327+
git --git-dir ${TEMPDIR}/${LS_REPO}/.git push https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/${LS_USER}/${LS_REPO}.git --all
328+
echo "true" > /tmp/packages-${COMMIT_SHA}-${BUILD_NUMBER}
329+
else
330+
echo "false" > /tmp/packages-${COMMIT_SHA}-${BUILD_NUMBER}
331+
fi
332+
rm -Rf ${TEMPDIR}'''
333+
script{
334+
env.PACKAGE_UPDATED = sh(
335+
script: '''cat /tmp/packages-${COMMIT_SHA}-${BUILD_NUMBER}''',
336+
returnStdout: true).trim()
337+
}
338+
}
339+
}
340+
// Exit the build if the package file was just updated
341+
stage('PACKAGE-exit') {
342+
when {
343+
branch "master"
344+
environment name: 'CHANGE_ID', value: ''
345+
environment name: 'PACKAGE_UPDATED', value: 'true'
346+
environment name: 'EXIT_STATUS', value: ''
347+
}
348+
steps {
349+
script{
350+
env.EXIT_STATUS = 'ABORTED'
351+
}
352+
}
353+
}
354+
// Exit the build if this is just a package check and there are no changes to push
355+
stage('PACKAGECHECK-exit') {
356+
when {
357+
branch "master"
358+
environment name: 'CHANGE_ID', value: ''
359+
environment name: 'PACKAGE_UPDATED', value: 'false'
360+
environment name: 'EXIT_STATUS', value: ''
361+
expression {
362+
params.PACKAGE_CHECK == 'true'
363+
}
364+
}
365+
steps {
366+
script{
367+
env.EXIT_STATUS = 'ABORTED'
368+
}
369+
}
370+
}
292371
/* #######
293372
Testing
294373
####### */
295374
// Run Container tests
296375
stage('Test') {
297376
when {
298377
environment name: 'CI', value: 'true'
378+
environment name: 'EXIT_STATUS', value: ''
299379
}
300380
steps {
301381
withCredentials([
@@ -342,6 +422,7 @@ pipeline {
342422
stage('Docker-Push-Single') {
343423
when {
344424
environment name: 'MULTIARCH', value: 'false'
425+
environment name: 'EXIT_STATUS', value: ''
345426
}
346427
steps {
347428
withCredentials([
@@ -366,6 +447,7 @@ pipeline {
366447
stage('Docker-Push-Multi') {
367448
when {
368449
environment name: 'MULTIARCH', value: 'true'
450+
environment name: 'EXIT_STATUS', value: ''
369451
}
370452
steps {
371453
withCredentials([
@@ -416,6 +498,7 @@ pipeline {
416498
env.LS_RELEASE != env.EXT_RELEASE + '-pkg-' + env.PACKAGE_TAG + '-ls' + env.LS_TAG_NUMBER
417499
}
418500
environment name: 'CHANGE_ID', value: ''
501+
environment name: 'EXIT_STATUS', value: ''
419502
}
420503
steps {
421504
echo "Pushing New tag for current commit ${EXT_RELEASE}-pkg-${PACKAGE_TAG}-ls${LS_TAG_NUMBER}"
@@ -441,6 +524,7 @@ pipeline {
441524
stage('Sync-README') {
442525
when {
443526
environment name: 'CHANGE_ID', value: ''
527+
environment name: 'EXIT_STATUS', value: ''
444528
}
445529
steps {
446530
withCredentials([
@@ -468,15 +552,22 @@ pipeline {
468552
Send status to Discord
469553
###################### */
470554
post {
471-
success {
472-
sh ''' curl -X POST --data '{"avatar_url": "https://wiki.jenkins-ci.org/download/attachments/2916393/headshot.png","embeds": [{"color": 1681177,\
473-
"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"}],\
474-
"username": "Jenkins"}' ${BUILDS_DISCORD} '''
475-
}
476-
failure {
477-
sh ''' curl -X POST --data '{"avatar_url": "https://wiki.jenkins-ci.org/download/attachments/2916393/headshot.png","embeds": [{"color": 16711680,\
478-
"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"}],\
479-
"username": "Jenkins"}' ${BUILDS_DISCORD} '''
555+
always {
556+
script{
557+
if (env.EXIT_STATUS == "ABORTED"){
558+
sh 'echo "build aborted"'
559+
}
560+
else if (currentBuild.currentResult == "SUCCESS"){
561+
sh ''' curl -X POST --data '{"avatar_url": "https://wiki.jenkins-ci.org/download/attachments/2916393/headshot.png","embeds": [{"color": 1681177,\
562+
"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"}],\
563+
"username": "Jenkins"}' ${BUILDS_DISCORD} '''
564+
}
565+
else {
566+
sh ''' curl -X POST --data '{"avatar_url": "https://wiki.jenkins-ci.org/download/attachments/2916393/headshot.png","embeds": [{"color": 16711680,\
567+
"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"}],\
568+
"username": "Jenkins"}' ${BUILDS_DISCORD} '''
569+
}
570+
}
480571
}
481572
}
482573
}

0 commit comments

Comments
 (0)