Skip to content

Commit 72f635c

Browse files
committedMar 16, 2024
Bot Updating Templated Files
1 parent 91e3b24 commit 72f635c

File tree

1 file changed

+45
-16
lines changed

1 file changed

+45
-16
lines changed
 

‎Jenkinsfile

+45-16
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,15 @@ pipeline {
272272
set -e
273273
TEMPDIR=$(mktemp -d)
274274
docker pull ghcr.io/linuxserver/jenkins-builder:latest
275-
docker run --rm -e CONTAINER_NAME=${CONTAINER_NAME} -e GITHUB_BRANCH=master -v ${TEMPDIR}:/ansible/jenkins ghcr.io/linuxserver/jenkins-builder:latest
276-
# Stage 1 - Jenkinsfile update
275+
# Cloned repo paths for templating:
276+
# ${TEMPDIR}/docker-${CONTAINER_NAME}: Cloned branch master of ${LS_USER}/${LS_REPO} for running the jenkins builder on
277+
# ${TEMPDIR}/repo/${LS_REPO}: Cloned branch master of ${LS_USER}/${LS_REPO} for commiting various templated file changes and pushing back to Github
278+
# ${TEMPDIR}/docs/docker-documentation: Cloned docs repo for pushing docs updates to Github
279+
# ${TEMPDIR}/unraid/docker-templates: Cloned docker-templates repo to check for logos
280+
# ${TEMPDIR}/unraid/templates: Cloned templates repo for commiting unraid template changes and pushing back to Github
281+
git clone --branch master --depth 1 https://github.com/${LS_USER}/${LS_REPO}.git ${TEMPDIR}/docker-${CONTAINER_NAME}
282+
docker run --rm -v ${TEMPDIR}/docker-${CONTAINER_NAME}:/tmp -e LOCAL=true ghcr.io/linuxserver/jenkins-builder:latest
283+
echo "Starting Stage 1 - Jenkinsfile update"
277284
if [[ "$(md5sum Jenkinsfile | awk '{ print $1 }')" != "$(md5sum ${TEMPDIR}/docker-${CONTAINER_NAME}/Jenkinsfile | awk '{ print $1 }')" ]]; then
278285
mkdir -p ${TEMPDIR}/repo
279286
git clone https://github.com/${LS_USER}/${LS_REPO}.git ${TEMPDIR}/repo/${LS_REPO}
@@ -285,13 +292,13 @@ pipeline {
285292
git pull https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/${LS_USER}/${LS_REPO}.git master
286293
git push https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/${LS_USER}/${LS_REPO}.git master
287294
echo "true" > /tmp/${COMMIT_SHA}-${BUILD_NUMBER}
288-
echo "Updating Jenkinsfile"
295+
echo "Updating Jenkinsfile and exiting build, new one will trigger based on commit"
289296
rm -Rf ${TEMPDIR}
290297
exit 0
291298
else
292299
echo "Jenkinsfile is up to date."
293300
fi
294-
# Stage 2 - Delete old templates
301+
echo "Starting Stage 2 - Delete old templates"
295302
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"
296303
for i in ${OLD_TEMPLATES}; do
297304
if [[ -f "${i}" ]]; then
@@ -310,13 +317,13 @@ pipeline {
310317
git pull https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/${LS_USER}/${LS_REPO}.git master
311318
git push https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/${LS_USER}/${LS_REPO}.git master
312319
echo "true" > /tmp/${COMMIT_SHA}-${BUILD_NUMBER}
313-
echo "Deleting old and deprecated templates"
320+
echo "Deleting old/deprecated templates and exiting build, new one will trigger based on commit"
314321
rm -Rf ${TEMPDIR}
315322
exit 0
316323
else
317324
echo "No templates to delete"
318325
fi
319-
# Stage 3 - Update templates
326+
echo "Starting Stage 3 - Update templates"
320327
CURRENTHASH=$(grep -hs ^ ${TEMPLATED_FILES} | md5sum | cut -c1-8)
321328
cd ${TEMPDIR}/docker-${CONTAINER_NAME}
322329
NEWHASH=$(grep -hs ^ ${TEMPLATED_FILES} | md5sum | cut -c1-8)
@@ -340,23 +347,31 @@ pipeline {
340347
git pull https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/${LS_USER}/${LS_REPO}.git master
341348
git push https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/${LS_USER}/${LS_REPO}.git master
342349
echo "true" > /tmp/${COMMIT_SHA}-${BUILD_NUMBER}
350+
echo "Updating templates and exiting build, new one will trigger based on commit"
351+
rm -Rf ${TEMPDIR}
352+
exit 0
343353
else
344354
echo "false" > /tmp/${COMMIT_SHA}-${BUILD_NUMBER}
355+
echo "No templates to update"
345356
fi
357+
echo "Starting Stage 4 - External repo updates: Docs, Unraid Template and Readme Sync to Docker Hub"
346358
mkdir -p ${TEMPDIR}/docs
347359
git clone https://github.com/linuxserver/docker-documentation.git ${TEMPDIR}/docs/docker-documentation
348360
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
349361
cp ${TEMPDIR}/docker-${CONTAINER_NAME}/.jenkins-external/docker-${CONTAINER_NAME}.md ${TEMPDIR}/docs/docker-documentation/docs/images/
350362
cd ${TEMPDIR}/docs/docker-documentation
351363
GH_DOCS_DEFAULT_BRANCH=$(git remote show origin | grep "HEAD branch:" | sed 's|.*HEAD branch: ||')
352364
git add docs/images/docker-${CONTAINER_NAME}.md
365+
echo "Updating docs repo"
353366
git commit -m 'Bot Updating Documentation'
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} || \
356369
(MAXWAIT="10" && echo "Push to docs failed, trying again in ${MAXWAIT} seconds" && \
357370
sleep $((RANDOM % MAXWAIT)) && \
358371
git pull https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/linuxserver/docker-documentation.git ${GH_DOCS_DEFAULT_BRANCH} --rebase && \
359372
git push https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/linuxserver/docker-documentation.git ${GH_DOCS_DEFAULT_BRANCH})
373+
else
374+
echo "Docs update not needed, skipping"
360375
fi
361376
mkdir -p ${TEMPDIR}/unraid
362377
git clone https://github.com/linuxserver/docker-templates.git ${TEMPDIR}/unraid/docker-templates
@@ -367,6 +382,7 @@ pipeline {
367382
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
368383
fi
369384
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
385+
echo "Updating Unraid template"
370386
cd ${TEMPDIR}/unraid/templates/
371387
GH_TEMPLATES_DEFAULT_BRANCH=$(git remote show origin | grep "HEAD branch:" | sed 's|.*HEAD branch: ||')
372388
if grep -wq "${CONTAINER_NAME}" ${TEMPDIR}/unraid/templates/unraid/ignore.list; then
@@ -386,16 +402,27 @@ pipeline {
386402
sleep $((RANDOM % MAXWAIT)) && \
387403
git pull https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/linuxserver/templates.git ${GH_TEMPLATES_DEFAULT_BRANCH} --rebase && \
388404
git push https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/linuxserver/templates.git ${GH_TEMPLATES_DEFAULT_BRANCH})
405+
else
406+
echo "No updates to Unraid template needed, skipping"
389407
fi
390-
# Stage 4 - Sync Readme to Docker Hub
391408
if [[ "${BRANCH_NAME}" == "${GH_DEFAULT_BRANCH}" ]]; then
392-
if [[ $(cat ${TEMPDIR}/docker-${CONTAINER_NAME}/README.md | wc -m) > 25000 ]]; then
409+
if [[ $(cat ${TEMPDIR}/docker-${CONTAINER_NAME}/README.md | wc -m) -gt 25000 ]]; then
393410
echo "Readme is longer than 25,000 characters. Syncing the lite version to Docker Hub"
394411
DH_README_SYNC_PATH="${TEMPDIR}/docker-${CONTAINER_NAME}/.jenkins-external/README.lite"
395412
else
396413
echo "Syncing readme to Docker Hub"
397414
DH_README_SYNC_PATH="${TEMPDIR}/docker-${CONTAINER_NAME}/README.md"
398415
fi
416+
if curl -s https://hub.docker.com/v2/namespaces/${DOCKERHUB_IMAGE%%/*}/repositories/${DOCKERHUB_IMAGE##*/}/tags | jq -r '.message' | grep -q 404; then
417+
echo "Docker Hub endpoint doesn't exist. Creating endpoint first."
418+
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')
419+
curl -s \
420+
-H "Authorization: JWT ${DH_TOKEN}" \
421+
-H "Content-Type: application/json" \
422+
-X POST \
423+
-d '{"name":"'${DOCKERHUB_IMAGE##*/}'", "namespace":"'${DOCKERHUB_IMAGE%%/*}'"}' \
424+
https://hub.docker.com/v2/repositories/ || :
425+
fi
399426
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')
400427
curl -s \
401428
-H "Authorization: JWT ${DH_TOKEN}" \
@@ -460,14 +487,16 @@ pipeline {
460487
}
461488
steps{
462489
sh '''curl -H "Content-Type: application/json" -H "Private-Token: ${GITLAB_TOKEN}" -X POST https://gitlab.com/api/v4/projects \
463-
-d '{"namespace_id":'${GITLAB_NAMESPACE}',\
464-
"name":"'${LS_REPO}'",
465-
"mirror":true,\
466-
"import_url":"https://github.com/linuxserver/'${LS_REPO}'.git",\
467-
"issues_access_level":"disabled",\
468-
"merge_requests_access_level":"disabled",\
469-
"repository_access_level":"enabled",\
470-
"visibility":"public"}' '''
490+
-d '{"namespace_id":'${GITLAB_NAMESPACE}',\
491+
"name":"'${LS_REPO}'",
492+
"mirror":true,\
493+
"import_url":"https://github.com/linuxserver/'${LS_REPO}'.git",\
494+
"issues_access_level":"disabled",\
495+
"merge_requests_access_level":"disabled",\
496+
"repository_access_level":"enabled",\
497+
"visibility":"public"}' '''
498+
sh '''curl -H "Private-Token: ${GITLAB_TOKEN}" -X PUT "https://gitlab.com/api/v4/projects/Linuxserver.io%2F${LS_REPO}" \
499+
-d "mirror=true&import_url=https://github.com/linuxserver/${LS_REPO}.git" '''
471500
}
472501
}
473502
/* ###############

0 commit comments

Comments
 (0)
Please sign in to comment.