Skip to content

Commit 309d288

Browse files
Bot Updating Templated Files
1 parent 67773aa commit 309d288

File tree

1 file changed

+45
-16
lines changed

1 file changed

+45
-16
lines changed

Jenkinsfile

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,15 @@ pipeline {
268268
set -e
269269
TEMPDIR=$(mktemp -d)
270270
docker pull ghcr.io/linuxserver/jenkins-builder:latest
271-
docker run --rm -e CONTAINER_NAME=${CONTAINER_NAME} -e GITHUB_BRANCH=master -v ${TEMPDIR}:/ansible/jenkins ghcr.io/linuxserver/jenkins-builder:latest
272-
# Stage 1 - Jenkinsfile update
271+
# Cloned repo paths for templating:
272+
# ${TEMPDIR}/docker-${CONTAINER_NAME}: Cloned branch master of ${LS_USER}/${LS_REPO} for running the jenkins builder on
273+
# ${TEMPDIR}/repo/${LS_REPO}: Cloned branch master of ${LS_USER}/${LS_REPO} for commiting various templated file changes and pushing back to Github
274+
# ${TEMPDIR}/docs/docker-documentation: Cloned docs repo for pushing docs updates to Github
275+
# ${TEMPDIR}/unraid/docker-templates: Cloned docker-templates repo to check for logos
276+
# ${TEMPDIR}/unraid/templates: Cloned templates repo for commiting unraid template changes and pushing back to Github
277+
git clone --branch master --depth 1 https://github.com/${LS_USER}/${LS_REPO}.git ${TEMPDIR}/docker-${CONTAINER_NAME}
278+
docker run --rm -v ${TEMPDIR}/docker-${CONTAINER_NAME}:/tmp -e LOCAL=true ghcr.io/linuxserver/jenkins-builder:latest
279+
echo "Starting Stage 1 - Jenkinsfile update"
273280
if [[ "$(md5sum Jenkinsfile | awk '{ print $1 }')" != "$(md5sum ${TEMPDIR}/docker-${CONTAINER_NAME}/Jenkinsfile | awk '{ print $1 }')" ]]; then
274281
mkdir -p ${TEMPDIR}/repo
275282
git clone https://github.com/${LS_USER}/${LS_REPO}.git ${TEMPDIR}/repo/${LS_REPO}
@@ -281,13 +288,13 @@ pipeline {
281288
git pull https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/${LS_USER}/${LS_REPO}.git master
282289
git push https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/${LS_USER}/${LS_REPO}.git master
283290
echo "true" > /tmp/${COMMIT_SHA}-${BUILD_NUMBER}
284-
echo "Updating Jenkinsfile"
291+
echo "Updating Jenkinsfile and exiting build, new one will trigger based on commit"
285292
rm -Rf ${TEMPDIR}
286293
exit 0
287294
else
288295
echo "Jenkinsfile is up to date."
289296
fi
290-
# Stage 2 - Delete old templates
297+
echo "Starting Stage 2 - Delete old templates"
291298
OLD_TEMPLATES=".github/ISSUE_TEMPLATE.md .github/ISSUE_TEMPLATE/issue.bug.md .github/ISSUE_TEMPLATE/issue.feature.md .github/workflows/call_invalid_helper.yml .github/workflows/stale.yml Dockerfile.armhf"
292299
for i in ${OLD_TEMPLATES}; do
293300
if [[ -f "${i}" ]]; then
@@ -306,13 +313,13 @@ pipeline {
306313
git pull https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/${LS_USER}/${LS_REPO}.git master
307314
git push https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/${LS_USER}/${LS_REPO}.git master
308315
echo "true" > /tmp/${COMMIT_SHA}-${BUILD_NUMBER}
309-
echo "Deleting old and deprecated templates"
316+
echo "Deleting old/deprecated templates and exiting build, new one will trigger based on commit"
310317
rm -Rf ${TEMPDIR}
311318
exit 0
312319
else
313320
echo "No templates to delete"
314321
fi
315-
# Stage 3 - Update templates
322+
echo "Starting Stage 3 - Update templates"
316323
CURRENTHASH=$(grep -hs ^ ${TEMPLATED_FILES} | md5sum | cut -c1-8)
317324
cd ${TEMPDIR}/docker-${CONTAINER_NAME}
318325
NEWHASH=$(grep -hs ^ ${TEMPLATED_FILES} | md5sum | cut -c1-8)
@@ -336,23 +343,31 @@ pipeline {
336343
git pull https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/${LS_USER}/${LS_REPO}.git master
337344
git push https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/${LS_USER}/${LS_REPO}.git master
338345
echo "true" > /tmp/${COMMIT_SHA}-${BUILD_NUMBER}
346+
echo "Updating templates and exiting build, new one will trigger based on commit"
347+
rm -Rf ${TEMPDIR}
348+
exit 0
339349
else
340350
echo "false" > /tmp/${COMMIT_SHA}-${BUILD_NUMBER}
351+
echo "No templates to update"
341352
fi
353+
echo "Starting Stage 4 - External repo updates: Docs, Unraid Template and Readme Sync to Docker Hub"
342354
mkdir -p ${TEMPDIR}/docs
343355
git clone https://github.com/linuxserver/docker-documentation.git ${TEMPDIR}/docs/docker-documentation
344356
if [[ "${BRANCH_NAME}" == "${GH_DEFAULT_BRANCH}" ]] && [[ (! -f ${TEMPDIR}/docs/docker-documentation/docs/images/docker-${CONTAINER_NAME}.md) || ("$(md5sum ${TEMPDIR}/docs/docker-documentation/docs/images/docker-${CONTAINER_NAME}.md | awk '{ print $1 }')" != "$(md5sum ${TEMPDIR}/docker-${CONTAINER_NAME}/.jenkins-external/docker-${CONTAINER_NAME}.md | awk '{ print $1 }')") ]]; then
345357
cp ${TEMPDIR}/docker-${CONTAINER_NAME}/.jenkins-external/docker-${CONTAINER_NAME}.md ${TEMPDIR}/docs/docker-documentation/docs/images/
346358
cd ${TEMPDIR}/docs/docker-documentation
347359
GH_DOCS_DEFAULT_BRANCH=$(git remote show origin | grep "HEAD branch:" | sed 's|.*HEAD branch: ||')
348360
git add docs/images/docker-${CONTAINER_NAME}.md
361+
echo "Updating docs repo"
349362
git commit -m 'Bot Updating Documentation'
350363
git pull https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/linuxserver/docker-documentation.git ${GH_DOCS_DEFAULT_BRANCH} --rebase
351364
git push https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/linuxserver/docker-documentation.git ${GH_DOCS_DEFAULT_BRANCH} || \
352365
(MAXWAIT="10" && echo "Push to docs failed, trying again in ${MAXWAIT} seconds" && \
353366
sleep $((RANDOM % MAXWAIT)) && \
354367
git pull https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/linuxserver/docker-documentation.git ${GH_DOCS_DEFAULT_BRANCH} --rebase && \
355368
git push https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/linuxserver/docker-documentation.git ${GH_DOCS_DEFAULT_BRANCH})
369+
else
370+
echo "Docs update not needed, skipping"
356371
fi
357372
mkdir -p ${TEMPDIR}/unraid
358373
git clone https://github.com/linuxserver/docker-templates.git ${TEMPDIR}/unraid/docker-templates
@@ -363,6 +378,7 @@ pipeline {
363378
sed -i "s|master/linuxserver.io/img/linuxserver-ls-logo.png|master/linuxserver.io/img/${CONTAINER_NAME}-icon.png|" ${TEMPDIR}/docker-${CONTAINER_NAME}/.jenkins-external/${CONTAINER_NAME}.xml
364379
fi
365380
if [[ "${BRANCH_NAME}" == "${GH_DEFAULT_BRANCH}" ]] && [[ (! -f ${TEMPDIR}/unraid/templates/unraid/${CONTAINER_NAME}.xml) || ("$(md5sum ${TEMPDIR}/unraid/templates/unraid/${CONTAINER_NAME}.xml | awk '{ print $1 }')" != "$(md5sum ${TEMPDIR}/docker-${CONTAINER_NAME}/.jenkins-external/${CONTAINER_NAME}.xml | awk '{ print $1 }')") ]]; then
381+
echo "Updating Unraid template"
366382
cd ${TEMPDIR}/unraid/templates/
367383
GH_TEMPLATES_DEFAULT_BRANCH=$(git remote show origin | grep "HEAD branch:" | sed 's|.*HEAD branch: ||')
368384
if grep -wq "${CONTAINER_NAME}" ${TEMPDIR}/unraid/templates/unraid/ignore.list; then
@@ -382,16 +398,27 @@ pipeline {
382398
sleep $((RANDOM % MAXWAIT)) && \
383399
git pull https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/linuxserver/templates.git ${GH_TEMPLATES_DEFAULT_BRANCH} --rebase && \
384400
git push https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/linuxserver/templates.git ${GH_TEMPLATES_DEFAULT_BRANCH})
401+
else
402+
echo "No updates to Unraid template needed, skipping"
385403
fi
386-
# Stage 4 - Sync Readme to Docker Hub
387404
if [[ "${BRANCH_NAME}" == "${GH_DEFAULT_BRANCH}" ]]; then
388-
if [[ $(cat ${TEMPDIR}/docker-${CONTAINER_NAME}/README.md | wc -m) > 25000 ]]; then
405+
if [[ $(cat ${TEMPDIR}/docker-${CONTAINER_NAME}/README.md | wc -m) -gt 25000 ]]; then
389406
echo "Readme is longer than 25,000 characters. Syncing the lite version to Docker Hub"
390407
DH_README_SYNC_PATH="${TEMPDIR}/docker-${CONTAINER_NAME}/.jenkins-external/README.lite"
391408
else
392409
echo "Syncing readme to Docker Hub"
393410
DH_README_SYNC_PATH="${TEMPDIR}/docker-${CONTAINER_NAME}/README.md"
394411
fi
412+
if curl -s https://hub.docker.com/v2/namespaces/${DOCKERHUB_IMAGE%%/*}/repositories/${DOCKERHUB_IMAGE##*/}/tags | jq -r '.message' | grep -q 404; then
413+
echo "Docker Hub endpoint doesn't exist. Creating endpoint first."
414+
DH_TOKEN=$(curl -d '{"username":"linuxserverci", "password":"'${DOCKERHUB_TOKEN}'"}' -H "Content-Type: application/json" -X POST https://hub.docker.com/v2/users/login | jq -r '.token')
415+
curl -s \
416+
-H "Authorization: JWT ${DH_TOKEN}" \
417+
-H "Content-Type: application/json" \
418+
-X POST \
419+
-d '{"name":"'${DOCKERHUB_IMAGE##*/}'", "namespace":"'${DOCKERHUB_IMAGE%%/*}'"}' \
420+
https://hub.docker.com/v2/repositories/ || :
421+
fi
395422
DH_TOKEN=$(curl -d '{"username":"linuxserverci", "password":"'${DOCKERHUB_TOKEN}'"}' -H "Content-Type: application/json" -X POST https://hub.docker.com/v2/users/login | jq -r '.token')
396423
curl -s \
397424
-H "Authorization: JWT ${DH_TOKEN}" \
@@ -456,14 +483,16 @@ pipeline {
456483
}
457484
steps{
458485
sh '''curl -H "Content-Type: application/json" -H "Private-Token: ${GITLAB_TOKEN}" -X POST https://gitlab.com/api/v4/projects \
459-
-d '{"namespace_id":'${GITLAB_NAMESPACE}',\
460-
"name":"'${LS_REPO}'",
461-
"mirror":true,\
462-
"import_url":"https://github.com/linuxserver/'${LS_REPO}'.git",\
463-
"issues_access_level":"disabled",\
464-
"merge_requests_access_level":"disabled",\
465-
"repository_access_level":"enabled",\
466-
"visibility":"public"}' '''
486+
-d '{"namespace_id":'${GITLAB_NAMESPACE}',\
487+
"name":"'${LS_REPO}'",
488+
"mirror":true,\
489+
"import_url":"https://github.com/linuxserver/'${LS_REPO}'.git",\
490+
"issues_access_level":"disabled",\
491+
"merge_requests_access_level":"disabled",\
492+
"repository_access_level":"enabled",\
493+
"visibility":"public"}' '''
494+
sh '''curl -H "Private-Token: ${GITLAB_TOKEN}" -X PUT "https://gitlab.com/api/v4/projects/Linuxserver.io%2F${LS_REPO}" \
495+
-d "mirror=true&import_url=https://github.com/linuxserver/${LS_REPO}.git" '''
467496
}
468497
}
469498
/* ###############

0 commit comments

Comments
 (0)