Skip to content

Commit bc0dab4

Browse files
Bot Updating Templated Files
1 parent 7032768 commit bc0dab4

File tree

1 file changed

+42
-16
lines changed

1 file changed

+42
-16
lines changed

Jenkinsfile

Lines changed: 42 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=main -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 main of ${LS_USER}/${LS_REPO} for running the jenkins builder on
275+
# ${TEMPDIR}/repo/${LS_REPO}: Cloned branch main 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 main --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 main
284291
git push https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/${LS_USER}/${LS_REPO}.git main
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 main
309316
git push https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/${LS_USER}/${LS_REPO}.git main
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,33 +345,50 @@ pipeline {
338345
git pull https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/${LS_USER}/${LS_REPO}.git main
339346
git push https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/${LS_USER}/${LS_REPO}.git main
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
359-
# Stage 4 - Sync Readme to Docker Hub
360374
if [[ "${BRANCH_NAME}" == "${GH_DEFAULT_BRANCH}" ]]; then
361-
if [[ $(cat ${TEMPDIR}/docker-${CONTAINER_NAME}/README.md | wc -m) > 25000 ]]; then
375+
if [[ $(cat ${TEMPDIR}/docker-${CONTAINER_NAME}/README.md | wc -m) -gt 25000 ]]; then
362376
echo "Readme is longer than 25,000 characters. Syncing the lite version to Docker Hub"
363377
DH_README_SYNC_PATH="${TEMPDIR}/docker-${CONTAINER_NAME}/.jenkins-external/README.lite"
364378
else
365379
echo "Syncing readme to Docker Hub"
366380
DH_README_SYNC_PATH="${TEMPDIR}/docker-${CONTAINER_NAME}/README.md"
367381
fi
382+
if curl -s https://hub.docker.com/v2/namespaces/${DOCKERHUB_IMAGE%%/*}/repositories/${DOCKERHUB_IMAGE##*/}/tags | jq -r '.message' | grep -q 404; then
383+
echo "Docker Hub endpoint doesn't exist. Creating endpoint first."
384+
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')
385+
curl -s \
386+
-H "Authorization: JWT ${DH_TOKEN}" \
387+
-H "Content-Type: application/json" \
388+
-X POST \
389+
-d '{"name":"'${DOCKERHUB_IMAGE##*/}'", "namespace":"'${DOCKERHUB_IMAGE%%/*}'"}' \
390+
https://hub.docker.com/v2/repositories/ || :
391+
fi
368392
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')
369393
curl -s \
370394
-H "Authorization: JWT ${DH_TOKEN}" \
@@ -429,14 +453,16 @@ pipeline {
429453
}
430454
steps{
431455
sh '''curl -H "Content-Type: application/json" -H "Private-Token: ${GITLAB_TOKEN}" -X POST https://gitlab.com/api/v4/projects \
432-
-d '{"namespace_id":'${GITLAB_NAMESPACE}',\
433-
"name":"'${LS_REPO}'",
434-
"mirror":true,\
435-
"import_url":"https://github.com/linuxserver/'${LS_REPO}'.git",\
436-
"issues_access_level":"disabled",\
437-
"merge_requests_access_level":"disabled",\
438-
"repository_access_level":"enabled",\
439-
"visibility":"public"}' '''
456+
-d '{"namespace_id":'${GITLAB_NAMESPACE}',\
457+
"name":"'${LS_REPO}'",
458+
"mirror":true,\
459+
"import_url":"https://github.com/linuxserver/'${LS_REPO}'.git",\
460+
"issues_access_level":"disabled",\
461+
"merge_requests_access_level":"disabled",\
462+
"repository_access_level":"enabled",\
463+
"visibility":"public"}' '''
464+
sh '''curl -H "Private-Token: ${GITLAB_TOKEN}" -X PUT "https://gitlab.com/api/v4/projects/Linuxserver.io%2F${LS_REPO}" \
465+
-d "mirror=true&import_url=https://github.com/linuxserver/${LS_REPO}.git" '''
440466
}
441467
}
442468
/* ###############

0 commit comments

Comments
 (0)