Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit 713e7aa

Browse files
Bot Updating Templated Files
1 parent 46e86ef commit 713e7aa

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
@@ -270,8 +270,15 @@ pipeline {
270270
set -e
271271
TEMPDIR=$(mktemp -d)
272272
docker pull ghcr.io/linuxserver/jenkins-builder:latest
273-
docker run --rm -e CONTAINER_NAME=${CONTAINER_NAME} -e GITHUB_BRANCH=master -v ${TEMPDIR}:/ansible/jenkins ghcr.io/linuxserver/jenkins-builder:latest
274-
# Stage 1 - Jenkinsfile update
273+
# Cloned repo paths for templating:
274+
# ${TEMPDIR}/docker-${CONTAINER_NAME}: Cloned branch master of ${LS_USER}/${LS_REPO} for running the jenkins builder on
275+
# ${TEMPDIR}/repo/${LS_REPO}: Cloned branch master of ${LS_USER}/${LS_REPO} for commiting various templated file changes and pushing back to Github
276+
# ${TEMPDIR}/docs/docker-documentation: Cloned docs repo for pushing docs updates to Github
277+
# ${TEMPDIR}/unraid/docker-templates: Cloned docker-templates repo to check for logos
278+
# ${TEMPDIR}/unraid/templates: Cloned templates repo for commiting unraid template changes and pushing back to Github
279+
git clone --branch master --depth 1 https://github.com/${LS_USER}/${LS_REPO}.git ${TEMPDIR}/docker-${CONTAINER_NAME}
280+
docker run --rm -v ${TEMPDIR}/docker-${CONTAINER_NAME}:/tmp -e LOCAL=true ghcr.io/linuxserver/jenkins-builder:latest
281+
echo "Starting Stage 1 - Jenkinsfile update"
275282
if [[ "$(md5sum Jenkinsfile | awk '{ print $1 }')" != "$(md5sum ${TEMPDIR}/docker-${CONTAINER_NAME}/Jenkinsfile | awk '{ print $1 }')" ]]; then
276283
mkdir -p ${TEMPDIR}/repo
277284
git clone https://github.com/${LS_USER}/${LS_REPO}.git ${TEMPDIR}/repo/${LS_REPO}
@@ -283,13 +290,13 @@ pipeline {
283290
git pull https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/${LS_USER}/${LS_REPO}.git master
284291
git push https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/${LS_USER}/${LS_REPO}.git master
285292
echo "true" > /tmp/${COMMIT_SHA}-${BUILD_NUMBER}
286-
echo "Updating Jenkinsfile"
293+
echo "Updating Jenkinsfile and exiting build, new one will trigger based on commit"
287294
rm -Rf ${TEMPDIR}
288295
exit 0
289296
else
290297
echo "Jenkinsfile is up to date."
291298
fi
292-
# Stage 2 - Delete old templates
299+
echo "Starting Stage 2 - Delete old templates"
293300
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"
294301
for i in ${OLD_TEMPLATES}; do
295302
if [[ -f "${i}" ]]; then
@@ -308,13 +315,13 @@ pipeline {
308315
git pull https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/${LS_USER}/${LS_REPO}.git master
309316
git push https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/${LS_USER}/${LS_REPO}.git master
310317
echo "true" > /tmp/${COMMIT_SHA}-${BUILD_NUMBER}
311-
echo "Deleting old and deprecated templates"
318+
echo "Deleting old/deprecated templates and exiting build, new one will trigger based on commit"
312319
rm -Rf ${TEMPDIR}
313320
exit 0
314321
else
315322
echo "No templates to delete"
316323
fi
317-
# Stage 3 - Update templates
324+
echo "Starting Stage 3 - Update templates"
318325
CURRENTHASH=$(grep -hs ^ ${TEMPLATED_FILES} | md5sum | cut -c1-8)
319326
cd ${TEMPDIR}/docker-${CONTAINER_NAME}
320327
NEWHASH=$(grep -hs ^ ${TEMPLATED_FILES} | md5sum | cut -c1-8)
@@ -338,23 +345,31 @@ pipeline {
338345
git pull https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/${LS_USER}/${LS_REPO}.git master
339346
git push https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/${LS_USER}/${LS_REPO}.git master
340347
echo "true" > /tmp/${COMMIT_SHA}-${BUILD_NUMBER}
348+
echo "Updating templates and exiting build, new one will trigger based on commit"
349+
rm -Rf ${TEMPDIR}
350+
exit 0
341351
else
342352
echo "false" > /tmp/${COMMIT_SHA}-${BUILD_NUMBER}
353+
echo "No templates to update"
343354
fi
355+
echo "Starting Stage 4 - External repo updates: Docs, Unraid Template and Readme Sync to Docker Hub"
344356
mkdir -p ${TEMPDIR}/docs
345357
git clone https://github.com/linuxserver/docker-documentation.git ${TEMPDIR}/docs/docker-documentation
346358
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
347359
cp ${TEMPDIR}/docker-${CONTAINER_NAME}/.jenkins-external/docker-${CONTAINER_NAME}.md ${TEMPDIR}/docs/docker-documentation/docs/images/
348360
cd ${TEMPDIR}/docs/docker-documentation
349361
GH_DOCS_DEFAULT_BRANCH=$(git remote show origin | grep "HEAD branch:" | sed 's|.*HEAD branch: ||')
350362
git add docs/images/docker-${CONTAINER_NAME}.md
363+
echo "Updating docs repo"
351364
git commit -m 'Bot Updating Documentation'
352365
git pull https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/linuxserver/docker-documentation.git ${GH_DOCS_DEFAULT_BRANCH} --rebase
353366
git push https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/linuxserver/docker-documentation.git ${GH_DOCS_DEFAULT_BRANCH} || \
354367
(MAXWAIT="10" && echo "Push to docs failed, trying again in ${MAXWAIT} seconds" && \
355368
sleep $((RANDOM % MAXWAIT)) && \
356369
git pull https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/linuxserver/docker-documentation.git ${GH_DOCS_DEFAULT_BRANCH} --rebase && \
357370
git push https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/linuxserver/docker-documentation.git ${GH_DOCS_DEFAULT_BRANCH})
371+
else
372+
echo "Docs update not needed, skipping"
358373
fi
359374
mkdir -p ${TEMPDIR}/unraid
360375
git clone https://github.com/linuxserver/docker-templates.git ${TEMPDIR}/unraid/docker-templates
@@ -365,6 +380,7 @@ pipeline {
365380
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
366381
fi
367382
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
383+
echo "Updating Unraid template"
368384
cd ${TEMPDIR}/unraid/templates/
369385
GH_TEMPLATES_DEFAULT_BRANCH=$(git remote show origin | grep "HEAD branch:" | sed 's|.*HEAD branch: ||')
370386
if grep -wq "${CONTAINER_NAME}" ${TEMPDIR}/unraid/templates/unraid/ignore.list; then
@@ -384,16 +400,27 @@ pipeline {
384400
sleep $((RANDOM % MAXWAIT)) && \
385401
git pull https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/linuxserver/templates.git ${GH_TEMPLATES_DEFAULT_BRANCH} --rebase && \
386402
git push https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/linuxserver/templates.git ${GH_TEMPLATES_DEFAULT_BRANCH})
403+
else
404+
echo "No updates to Unraid template needed, skipping"
387405
fi
388-
# Stage 4 - Sync Readme to Docker Hub
389406
if [[ "${BRANCH_NAME}" == "${GH_DEFAULT_BRANCH}" ]]; then
390-
if [[ $(cat ${TEMPDIR}/docker-${CONTAINER_NAME}/README.md | wc -m) > 25000 ]]; then
407+
if [[ $(cat ${TEMPDIR}/docker-${CONTAINER_NAME}/README.md | wc -m) -gt 25000 ]]; then
391408
echo "Readme is longer than 25,000 characters. Syncing the lite version to Docker Hub"
392409
DH_README_SYNC_PATH="${TEMPDIR}/docker-${CONTAINER_NAME}/.jenkins-external/README.lite"
393410
else
394411
echo "Syncing readme to Docker Hub"
395412
DH_README_SYNC_PATH="${TEMPDIR}/docker-${CONTAINER_NAME}/README.md"
396413
fi
414+
if curl -s https://hub.docker.com/v2/namespaces/${DOCKERHUB_IMAGE%%/*}/repositories/${DOCKERHUB_IMAGE##*/}/tags | jq -r '.message' | grep -q 404; then
415+
echo "Docker Hub endpoint doesn't exist. Creating endpoint first."
416+
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')
417+
curl -s \
418+
-H "Authorization: JWT ${DH_TOKEN}" \
419+
-H "Content-Type: application/json" \
420+
-X POST \
421+
-d '{"name":"'${DOCKERHUB_IMAGE##*/}'", "namespace":"'${DOCKERHUB_IMAGE%%/*}'"}' \
422+
https://hub.docker.com/v2/repositories/ || :
423+
fi
397424
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')
398425
curl -s \
399426
-H "Authorization: JWT ${DH_TOKEN}" \
@@ -458,14 +485,16 @@ pipeline {
458485
}
459486
steps{
460487
sh '''curl -H "Content-Type: application/json" -H "Private-Token: ${GITLAB_TOKEN}" -X POST https://gitlab.com/api/v4/projects \
461-
-d '{"namespace_id":'${GITLAB_NAMESPACE}',\
462-
"name":"'${LS_REPO}'",
463-
"mirror":true,\
464-
"import_url":"https://github.com/linuxserver/'${LS_REPO}'.git",\
465-
"issues_access_level":"disabled",\
466-
"merge_requests_access_level":"disabled",\
467-
"repository_access_level":"enabled",\
468-
"visibility":"public"}' '''
488+
-d '{"namespace_id":'${GITLAB_NAMESPACE}',\
489+
"name":"'${LS_REPO}'",
490+
"mirror":true,\
491+
"import_url":"https://github.com/linuxserver/'${LS_REPO}'.git",\
492+
"issues_access_level":"disabled",\
493+
"merge_requests_access_level":"disabled",\
494+
"repository_access_level":"enabled",\
495+
"visibility":"public"}' '''
496+
sh '''curl -H "Private-Token: ${GITLAB_TOKEN}" -X PUT "https://gitlab.com/api/v4/projects/Linuxserver.io%2F${LS_REPO}" \
497+
-d "mirror=true&import_url=https://github.com/linuxserver/${LS_REPO}.git" '''
469498
}
470499
}
471500
/* ###############

0 commit comments

Comments
 (0)