Skip to content

Commit 8556ea4

Browse files
Bot Updating Templated Files
1 parent 289393c commit 8556ea4

File tree

1 file changed

+45
-16
lines changed

1 file changed

+45
-16
lines changed

Diff for: Jenkinsfile

+45-16
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,15 @@ pipeline {
279279
set -e
280280
TEMPDIR=$(mktemp -d)
281281
docker pull ghcr.io/linuxserver/jenkins-builder:latest
282-
docker run --rm -e CONTAINER_NAME=${CONTAINER_NAME} -e GITHUB_BRANCH=master -v ${TEMPDIR}:/ansible/jenkins ghcr.io/linuxserver/jenkins-builder:latest
283-
# Stage 1 - Jenkinsfile update
282+
# Cloned repo paths for templating:
283+
# ${TEMPDIR}/docker-${CONTAINER_NAME}: Cloned branch master of ${LS_USER}/${LS_REPO} for running the jenkins builder on
284+
# ${TEMPDIR}/repo/${LS_REPO}: Cloned branch master of ${LS_USER}/${LS_REPO} for commiting various templated file changes and pushing back to Github
285+
# ${TEMPDIR}/docs/docker-documentation: Cloned docs repo for pushing docs updates to Github
286+
# ${TEMPDIR}/unraid/docker-templates: Cloned docker-templates repo to check for logos
287+
# ${TEMPDIR}/unraid/templates: Cloned templates repo for commiting unraid template changes and pushing back to Github
288+
git clone --branch master --depth 1 https://github.com/${LS_USER}/${LS_REPO}.git ${TEMPDIR}/docker-${CONTAINER_NAME}
289+
docker run --rm -v ${TEMPDIR}/docker-${CONTAINER_NAME}:/tmp -e LOCAL=true ghcr.io/linuxserver/jenkins-builder:latest
290+
echo "Starting Stage 1 - Jenkinsfile update"
284291
if [[ "$(md5sum Jenkinsfile | awk '{ print $1 }')" != "$(md5sum ${TEMPDIR}/docker-${CONTAINER_NAME}/Jenkinsfile | awk '{ print $1 }')" ]]; then
285292
mkdir -p ${TEMPDIR}/repo
286293
git clone https://github.com/${LS_USER}/${LS_REPO}.git ${TEMPDIR}/repo/${LS_REPO}
@@ -292,13 +299,13 @@ pipeline {
292299
git pull https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/${LS_USER}/${LS_REPO}.git master
293300
git push https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/${LS_USER}/${LS_REPO}.git master
294301
echo "true" > /tmp/${COMMIT_SHA}-${BUILD_NUMBER}
295-
echo "Updating Jenkinsfile"
302+
echo "Updating Jenkinsfile and exiting build, new one will trigger based on commit"
296303
rm -Rf ${TEMPDIR}
297304
exit 0
298305
else
299306
echo "Jenkinsfile is up to date."
300307
fi
301-
# Stage 2 - Delete old templates
308+
echo "Starting Stage 2 - Delete old templates"
302309
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"
303310
for i in ${OLD_TEMPLATES}; do
304311
if [[ -f "${i}" ]]; then
@@ -317,13 +324,13 @@ pipeline {
317324
git pull https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/${LS_USER}/${LS_REPO}.git master
318325
git push https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/${LS_USER}/${LS_REPO}.git master
319326
echo "true" > /tmp/${COMMIT_SHA}-${BUILD_NUMBER}
320-
echo "Deleting old and deprecated templates"
327+
echo "Deleting old/deprecated templates and exiting build, new one will trigger based on commit"
321328
rm -Rf ${TEMPDIR}
322329
exit 0
323330
else
324331
echo "No templates to delete"
325332
fi
326-
# Stage 3 - Update templates
333+
echo "Starting Stage 3 - Update templates"
327334
CURRENTHASH=$(grep -hs ^ ${TEMPLATED_FILES} | md5sum | cut -c1-8)
328335
cd ${TEMPDIR}/docker-${CONTAINER_NAME}
329336
NEWHASH=$(grep -hs ^ ${TEMPLATED_FILES} | md5sum | cut -c1-8)
@@ -347,23 +354,31 @@ pipeline {
347354
git pull https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/${LS_USER}/${LS_REPO}.git master
348355
git push https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/${LS_USER}/${LS_REPO}.git master
349356
echo "true" > /tmp/${COMMIT_SHA}-${BUILD_NUMBER}
357+
echo "Updating templates and exiting build, new one will trigger based on commit"
358+
rm -Rf ${TEMPDIR}
359+
exit 0
350360
else
351361
echo "false" > /tmp/${COMMIT_SHA}-${BUILD_NUMBER}
362+
echo "No templates to update"
352363
fi
364+
echo "Starting Stage 4 - External repo updates: Docs, Unraid Template and Readme Sync to Docker Hub"
353365
mkdir -p ${TEMPDIR}/docs
354366
git clone https://github.com/linuxserver/docker-documentation.git ${TEMPDIR}/docs/docker-documentation
355367
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
356368
cp ${TEMPDIR}/docker-${CONTAINER_NAME}/.jenkins-external/docker-${CONTAINER_NAME}.md ${TEMPDIR}/docs/docker-documentation/docs/images/
357369
cd ${TEMPDIR}/docs/docker-documentation
358370
GH_DOCS_DEFAULT_BRANCH=$(git remote show origin | grep "HEAD branch:" | sed 's|.*HEAD branch: ||')
359371
git add docs/images/docker-${CONTAINER_NAME}.md
372+
echo "Updating docs repo"
360373
git commit -m 'Bot Updating Documentation'
361374
git pull https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/linuxserver/docker-documentation.git ${GH_DOCS_DEFAULT_BRANCH} --rebase
362375
git push https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/linuxserver/docker-documentation.git ${GH_DOCS_DEFAULT_BRANCH} || \
363376
(MAXWAIT="10" && echo "Push to docs failed, trying again in ${MAXWAIT} seconds" && \
364377
sleep $((RANDOM % MAXWAIT)) && \
365378
git pull https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/linuxserver/docker-documentation.git ${GH_DOCS_DEFAULT_BRANCH} --rebase && \
366379
git push https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/linuxserver/docker-documentation.git ${GH_DOCS_DEFAULT_BRANCH})
380+
else
381+
echo "Docs update not needed, skipping"
367382
fi
368383
mkdir -p ${TEMPDIR}/unraid
369384
git clone https://github.com/linuxserver/docker-templates.git ${TEMPDIR}/unraid/docker-templates
@@ -374,6 +389,7 @@ pipeline {
374389
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
375390
fi
376391
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
392+
echo "Updating Unraid template"
377393
cd ${TEMPDIR}/unraid/templates/
378394
GH_TEMPLATES_DEFAULT_BRANCH=$(git remote show origin | grep "HEAD branch:" | sed 's|.*HEAD branch: ||')
379395
if grep -wq "${CONTAINER_NAME}" ${TEMPDIR}/unraid/templates/unraid/ignore.list; then
@@ -393,16 +409,27 @@ pipeline {
393409
sleep $((RANDOM % MAXWAIT)) && \
394410
git pull https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/linuxserver/templates.git ${GH_TEMPLATES_DEFAULT_BRANCH} --rebase && \
395411
git push https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/linuxserver/templates.git ${GH_TEMPLATES_DEFAULT_BRANCH})
412+
else
413+
echo "No updates to Unraid template needed, skipping"
396414
fi
397-
# Stage 4 - Sync Readme to Docker Hub
398415
if [[ "${BRANCH_NAME}" == "${GH_DEFAULT_BRANCH}" ]]; then
399-
if [[ $(cat ${TEMPDIR}/docker-${CONTAINER_NAME}/README.md | wc -m) > 25000 ]]; then
416+
if [[ $(cat ${TEMPDIR}/docker-${CONTAINER_NAME}/README.md | wc -m) -gt 25000 ]]; then
400417
echo "Readme is longer than 25,000 characters. Syncing the lite version to Docker Hub"
401418
DH_README_SYNC_PATH="${TEMPDIR}/docker-${CONTAINER_NAME}/.jenkins-external/README.lite"
402419
else
403420
echo "Syncing readme to Docker Hub"
404421
DH_README_SYNC_PATH="${TEMPDIR}/docker-${CONTAINER_NAME}/README.md"
405422
fi
423+
if curl -s https://hub.docker.com/v2/namespaces/${DOCKERHUB_IMAGE%%/*}/repositories/${DOCKERHUB_IMAGE##*/}/tags | jq -r '.message' | grep -q 404; then
424+
echo "Docker Hub endpoint doesn't exist. Creating endpoint first."
425+
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')
426+
curl -s \
427+
-H "Authorization: JWT ${DH_TOKEN}" \
428+
-H "Content-Type: application/json" \
429+
-X POST \
430+
-d '{"name":"'${DOCKERHUB_IMAGE##*/}'", "namespace":"'${DOCKERHUB_IMAGE%%/*}'"}' \
431+
https://hub.docker.com/v2/repositories/ || :
432+
fi
406433
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')
407434
curl -s \
408435
-H "Authorization: JWT ${DH_TOKEN}" \
@@ -467,14 +494,16 @@ pipeline {
467494
}
468495
steps{
469496
sh '''curl -H "Content-Type: application/json" -H "Private-Token: ${GITLAB_TOKEN}" -X POST https://gitlab.com/api/v4/projects \
470-
-d '{"namespace_id":'${GITLAB_NAMESPACE}',\
471-
"name":"'${LS_REPO}'",
472-
"mirror":true,\
473-
"import_url":"https://github.com/linuxserver/'${LS_REPO}'.git",\
474-
"issues_access_level":"disabled",\
475-
"merge_requests_access_level":"disabled",\
476-
"repository_access_level":"enabled",\
477-
"visibility":"public"}' '''
497+
-d '{"namespace_id":'${GITLAB_NAMESPACE}',\
498+
"name":"'${LS_REPO}'",
499+
"mirror":true,\
500+
"import_url":"https://github.com/linuxserver/'${LS_REPO}'.git",\
501+
"issues_access_level":"disabled",\
502+
"merge_requests_access_level":"disabled",\
503+
"repository_access_level":"enabled",\
504+
"visibility":"public"}' '''
505+
sh '''curl -H "Private-Token: ${GITLAB_TOKEN}" -X PUT "https://gitlab.com/api/v4/projects/Linuxserver.io%2F${LS_REPO}" \
506+
-d "mirror=true&import_url=https://github.com/linuxserver/${LS_REPO}.git" '''
478507
}
479508
}
480509
/* ###############

0 commit comments

Comments
 (0)