Skip to content

Commit 9ead675

Browse files
Bot Updating Templated Files
1 parent 6acb372 commit 9ead675

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
@@ -269,8 +269,15 @@ pipeline {
269269
set -e
270270
TEMPDIR=$(mktemp -d)
271271
docker pull ghcr.io/linuxserver/jenkins-builder:latest
272-
docker run --rm -e CONTAINER_NAME=${CONTAINER_NAME} -e GITHUB_BRANCH=master -v ${TEMPDIR}:/ansible/jenkins ghcr.io/linuxserver/jenkins-builder:latest
273-
# Stage 1 - Jenkinsfile update
272+
# Cloned repo paths for templating:
273+
# ${TEMPDIR}/docker-${CONTAINER_NAME}: Cloned branch master of ${LS_USER}/${LS_REPO} for running the jenkins builder on
274+
# ${TEMPDIR}/repo/${LS_REPO}: Cloned branch master of ${LS_USER}/${LS_REPO} for commiting various templated file changes and pushing back to Github
275+
# ${TEMPDIR}/docs/docker-documentation: Cloned docs repo for pushing docs updates to Github
276+
# ${TEMPDIR}/unraid/docker-templates: Cloned docker-templates repo to check for logos
277+
# ${TEMPDIR}/unraid/templates: Cloned templates repo for commiting unraid template changes and pushing back to Github
278+
git clone --branch master --depth 1 https://github.com/${LS_USER}/${LS_REPO}.git ${TEMPDIR}/docker-${CONTAINER_NAME}
279+
docker run --rm -v ${TEMPDIR}/docker-${CONTAINER_NAME}:/tmp -e LOCAL=true ghcr.io/linuxserver/jenkins-builder:latest
280+
echo "Starting Stage 1 - Jenkinsfile update"
274281
if [[ "$(md5sum Jenkinsfile | awk '{ print $1 }')" != "$(md5sum ${TEMPDIR}/docker-${CONTAINER_NAME}/Jenkinsfile | awk '{ print $1 }')" ]]; then
275282
mkdir -p ${TEMPDIR}/repo
276283
git clone https://github.com/${LS_USER}/${LS_REPO}.git ${TEMPDIR}/repo/${LS_REPO}
@@ -282,13 +289,13 @@ pipeline {
282289
git pull https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/${LS_USER}/${LS_REPO}.git master
283290
git push https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/${LS_USER}/${LS_REPO}.git master
284291
echo "true" > /tmp/${COMMIT_SHA}-${BUILD_NUMBER}
285-
echo "Updating Jenkinsfile"
292+
echo "Updating Jenkinsfile and exiting build, new one will trigger based on commit"
286293
rm -Rf ${TEMPDIR}
287294
exit 0
288295
else
289296
echo "Jenkinsfile is up to date."
290297
fi
291-
# Stage 2 - Delete old templates
298+
echo "Starting Stage 2 - Delete old templates"
292299
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"
293300
for i in ${OLD_TEMPLATES}; do
294301
if [[ -f "${i}" ]]; then
@@ -307,13 +314,13 @@ pipeline {
307314
git pull https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/${LS_USER}/${LS_REPO}.git master
308315
git push https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/${LS_USER}/${LS_REPO}.git master
309316
echo "true" > /tmp/${COMMIT_SHA}-${BUILD_NUMBER}
310-
echo "Deleting old and deprecated templates"
317+
echo "Deleting old/deprecated templates and exiting build, new one will trigger based on commit"
311318
rm -Rf ${TEMPDIR}
312319
exit 0
313320
else
314321
echo "No templates to delete"
315322
fi
316-
# Stage 3 - Update templates
323+
echo "Starting Stage 3 - Update templates"
317324
CURRENTHASH=$(grep -hs ^ ${TEMPLATED_FILES} | md5sum | cut -c1-8)
318325
cd ${TEMPDIR}/docker-${CONTAINER_NAME}
319326
NEWHASH=$(grep -hs ^ ${TEMPLATED_FILES} | md5sum | cut -c1-8)
@@ -337,23 +344,31 @@ pipeline {
337344
git pull https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/${LS_USER}/${LS_REPO}.git master
338345
git push https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/${LS_USER}/${LS_REPO}.git master
339346
echo "true" > /tmp/${COMMIT_SHA}-${BUILD_NUMBER}
347+
echo "Updating templates and exiting build, new one will trigger based on commit"
348+
rm -Rf ${TEMPDIR}
349+
exit 0
340350
else
341351
echo "false" > /tmp/${COMMIT_SHA}-${BUILD_NUMBER}
352+
echo "No templates to update"
342353
fi
354+
echo "Starting Stage 4 - External repo updates: Docs, Unraid Template and Readme Sync to Docker Hub"
343355
mkdir -p ${TEMPDIR}/docs
344356
git clone https://github.com/linuxserver/docker-documentation.git ${TEMPDIR}/docs/docker-documentation
345357
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
346358
cp ${TEMPDIR}/docker-${CONTAINER_NAME}/.jenkins-external/docker-${CONTAINER_NAME}.md ${TEMPDIR}/docs/docker-documentation/docs/images/
347359
cd ${TEMPDIR}/docs/docker-documentation
348360
GH_DOCS_DEFAULT_BRANCH=$(git remote show origin | grep "HEAD branch:" | sed 's|.*HEAD branch: ||')
349361
git add docs/images/docker-${CONTAINER_NAME}.md
362+
echo "Updating docs repo"
350363
git commit -m 'Bot Updating Documentation'
351364
git pull https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/linuxserver/docker-documentation.git ${GH_DOCS_DEFAULT_BRANCH} --rebase
352365
git push https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/linuxserver/docker-documentation.git ${GH_DOCS_DEFAULT_BRANCH} || \
353366
(MAXWAIT="10" && echo "Push to docs failed, trying again in ${MAXWAIT} seconds" && \
354367
sleep $((RANDOM % MAXWAIT)) && \
355368
git pull https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/linuxserver/docker-documentation.git ${GH_DOCS_DEFAULT_BRANCH} --rebase && \
356369
git push https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/linuxserver/docker-documentation.git ${GH_DOCS_DEFAULT_BRANCH})
370+
else
371+
echo "Docs update not needed, skipping"
357372
fi
358373
mkdir -p ${TEMPDIR}/unraid
359374
git clone https://github.com/linuxserver/docker-templates.git ${TEMPDIR}/unraid/docker-templates
@@ -364,6 +379,7 @@ pipeline {
364379
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
365380
fi
366381
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
382+
echo "Updating Unraid template"
367383
cd ${TEMPDIR}/unraid/templates/
368384
GH_TEMPLATES_DEFAULT_BRANCH=$(git remote show origin | grep "HEAD branch:" | sed 's|.*HEAD branch: ||')
369385
if grep -wq "${CONTAINER_NAME}" ${TEMPDIR}/unraid/templates/unraid/ignore.list; then
@@ -383,16 +399,27 @@ pipeline {
383399
sleep $((RANDOM % MAXWAIT)) && \
384400
git pull https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/linuxserver/templates.git ${GH_TEMPLATES_DEFAULT_BRANCH} --rebase && \
385401
git push https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/linuxserver/templates.git ${GH_TEMPLATES_DEFAULT_BRANCH})
402+
else
403+
echo "No updates to Unraid template needed, skipping"
386404
fi
387-
# Stage 4 - Sync Readme to Docker Hub
388405
if [[ "${BRANCH_NAME}" == "${GH_DEFAULT_BRANCH}" ]]; then
389-
if [[ $(cat ${TEMPDIR}/docker-${CONTAINER_NAME}/README.md | wc -m) > 25000 ]]; then
406+
if [[ $(cat ${TEMPDIR}/docker-${CONTAINER_NAME}/README.md | wc -m) -gt 25000 ]]; then
390407
echo "Readme is longer than 25,000 characters. Syncing the lite version to Docker Hub"
391408
DH_README_SYNC_PATH="${TEMPDIR}/docker-${CONTAINER_NAME}/.jenkins-external/README.lite"
392409
else
393410
echo "Syncing readme to Docker Hub"
394411
DH_README_SYNC_PATH="${TEMPDIR}/docker-${CONTAINER_NAME}/README.md"
395412
fi
413+
if curl -s https://hub.docker.com/v2/namespaces/${DOCKERHUB_IMAGE%%/*}/repositories/${DOCKERHUB_IMAGE##*/}/tags | jq -r '.message' | grep -q 404; then
414+
echo "Docker Hub endpoint doesn't exist. Creating endpoint first."
415+
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')
416+
curl -s \
417+
-H "Authorization: JWT ${DH_TOKEN}" \
418+
-H "Content-Type: application/json" \
419+
-X POST \
420+
-d '{"name":"'${DOCKERHUB_IMAGE##*/}'", "namespace":"'${DOCKERHUB_IMAGE%%/*}'"}' \
421+
https://hub.docker.com/v2/repositories/ || :
422+
fi
396423
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')
397424
curl -s \
398425
-H "Authorization: JWT ${DH_TOKEN}" \
@@ -457,14 +484,16 @@ pipeline {
457484
}
458485
steps{
459486
sh '''curl -H "Content-Type: application/json" -H "Private-Token: ${GITLAB_TOKEN}" -X POST https://gitlab.com/api/v4/projects \
460-
-d '{"namespace_id":'${GITLAB_NAMESPACE}',\
461-
"name":"'${LS_REPO}'",
462-
"mirror":true,\
463-
"import_url":"https://github.com/linuxserver/'${LS_REPO}'.git",\
464-
"issues_access_level":"disabled",\
465-
"merge_requests_access_level":"disabled",\
466-
"repository_access_level":"enabled",\
467-
"visibility":"public"}' '''
487+
-d '{"namespace_id":'${GITLAB_NAMESPACE}',\
488+
"name":"'${LS_REPO}'",
489+
"mirror":true,\
490+
"import_url":"https://github.com/linuxserver/'${LS_REPO}'.git",\
491+
"issues_access_level":"disabled",\
492+
"merge_requests_access_level":"disabled",\
493+
"repository_access_level":"enabled",\
494+
"visibility":"public"}' '''
495+
sh '''curl -H "Private-Token: ${GITLAB_TOKEN}" -X PUT "https://gitlab.com/api/v4/projects/Linuxserver.io%2F${LS_REPO}" \
496+
-d "mirror=true&import_url=https://github.com/linuxserver/${LS_REPO}.git" '''
468497
}
469498
}
470499
/* ###############

0 commit comments

Comments
 (0)