Skip to content

Commit bc035c2

Browse files
Bot Updating Templated Files
1 parent f681ce6 commit bc035c2

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
@@ -266,8 +266,15 @@ pipeline {
266266
set -e
267267
TEMPDIR=$(mktemp -d)
268268
docker pull ghcr.io/linuxserver/jenkins-builder:latest
269-
docker run --rm -e CONTAINER_NAME=${CONTAINER_NAME} -e GITHUB_BRANCH=master -v ${TEMPDIR}:/ansible/jenkins ghcr.io/linuxserver/jenkins-builder:latest
270-
# Stage 1 - Jenkinsfile update
269+
# Cloned repo paths for templating:
270+
# ${TEMPDIR}/docker-${CONTAINER_NAME}: Cloned branch master of ${LS_USER}/${LS_REPO} for running the jenkins builder on
271+
# ${TEMPDIR}/repo/${LS_REPO}: Cloned branch master of ${LS_USER}/${LS_REPO} for commiting various templated file changes and pushing back to Github
272+
# ${TEMPDIR}/docs/docker-documentation: Cloned docs repo for pushing docs updates to Github
273+
# ${TEMPDIR}/unraid/docker-templates: Cloned docker-templates repo to check for logos
274+
# ${TEMPDIR}/unraid/templates: Cloned templates repo for commiting unraid template changes and pushing back to Github
275+
git clone --branch master --depth 1 https://github.com/${LS_USER}/${LS_REPO}.git ${TEMPDIR}/docker-${CONTAINER_NAME}
276+
docker run --rm -v ${TEMPDIR}/docker-${CONTAINER_NAME}:/tmp -e LOCAL=true ghcr.io/linuxserver/jenkins-builder:latest
277+
echo "Starting Stage 1 - Jenkinsfile update"
271278
if [[ "$(md5sum Jenkinsfile | awk '{ print $1 }')" != "$(md5sum ${TEMPDIR}/docker-${CONTAINER_NAME}/Jenkinsfile | awk '{ print $1 }')" ]]; then
272279
mkdir -p ${TEMPDIR}/repo
273280
git clone https://github.com/${LS_USER}/${LS_REPO}.git ${TEMPDIR}/repo/${LS_REPO}
@@ -279,13 +286,13 @@ pipeline {
279286
git pull https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/${LS_USER}/${LS_REPO}.git master
280287
git push https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/${LS_USER}/${LS_REPO}.git master
281288
echo "true" > /tmp/${COMMIT_SHA}-${BUILD_NUMBER}
282-
echo "Updating Jenkinsfile"
289+
echo "Updating Jenkinsfile and exiting build, new one will trigger based on commit"
283290
rm -Rf ${TEMPDIR}
284291
exit 0
285292
else
286293
echo "Jenkinsfile is up to date."
287294
fi
288-
# Stage 2 - Delete old templates
295+
echo "Starting Stage 2 - Delete old templates"
289296
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"
290297
for i in ${OLD_TEMPLATES}; do
291298
if [[ -f "${i}" ]]; then
@@ -304,13 +311,13 @@ pipeline {
304311
git pull https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/${LS_USER}/${LS_REPO}.git master
305312
git push https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/${LS_USER}/${LS_REPO}.git master
306313
echo "true" > /tmp/${COMMIT_SHA}-${BUILD_NUMBER}
307-
echo "Deleting old and deprecated templates"
314+
echo "Deleting old/deprecated templates and exiting build, new one will trigger based on commit"
308315
rm -Rf ${TEMPDIR}
309316
exit 0
310317
else
311318
echo "No templates to delete"
312319
fi
313-
# Stage 3 - Update templates
320+
echo "Starting Stage 3 - Update templates"
314321
CURRENTHASH=$(grep -hs ^ ${TEMPLATED_FILES} | md5sum | cut -c1-8)
315322
cd ${TEMPDIR}/docker-${CONTAINER_NAME}
316323
NEWHASH=$(grep -hs ^ ${TEMPLATED_FILES} | md5sum | cut -c1-8)
@@ -334,33 +341,50 @@ pipeline {
334341
git pull https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/${LS_USER}/${LS_REPO}.git master
335342
git push https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/${LS_USER}/${LS_REPO}.git master
336343
echo "true" > /tmp/${COMMIT_SHA}-${BUILD_NUMBER}
344+
echo "Updating templates and exiting build, new one will trigger based on commit"
345+
rm -Rf ${TEMPDIR}
346+
exit 0
337347
else
338348
echo "false" > /tmp/${COMMIT_SHA}-${BUILD_NUMBER}
349+
echo "No templates to update"
339350
fi
351+
echo "Starting Stage 4 - External repo updates: Docs, Unraid Template and Readme Sync to Docker Hub"
340352
mkdir -p ${TEMPDIR}/docs
341353
git clone https://github.com/linuxserver/docker-documentation.git ${TEMPDIR}/docs/docker-documentation
342354
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
343355
cp ${TEMPDIR}/docker-${CONTAINER_NAME}/.jenkins-external/docker-${CONTAINER_NAME}.md ${TEMPDIR}/docs/docker-documentation/docs/images/
344356
cd ${TEMPDIR}/docs/docker-documentation
345357
GH_DOCS_DEFAULT_BRANCH=$(git remote show origin | grep "HEAD branch:" | sed 's|.*HEAD branch: ||')
346358
git add docs/images/docker-${CONTAINER_NAME}.md
359+
echo "Updating docs repo"
347360
git commit -m 'Bot Updating Documentation'
348361
git pull https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/linuxserver/docker-documentation.git ${GH_DOCS_DEFAULT_BRANCH} --rebase
349362
git push https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/linuxserver/docker-documentation.git ${GH_DOCS_DEFAULT_BRANCH} || \
350363
(MAXWAIT="10" && echo "Push to docs failed, trying again in ${MAXWAIT} seconds" && \
351364
sleep $((RANDOM % MAXWAIT)) && \
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})
367+
else
368+
echo "Docs update not needed, skipping"
354369
fi
355-
# Stage 4 - Sync Readme to Docker Hub
356370
if [[ "${BRANCH_NAME}" == "${GH_DEFAULT_BRANCH}" ]]; then
357-
if [[ $(cat ${TEMPDIR}/docker-${CONTAINER_NAME}/README.md | wc -m) > 25000 ]]; then
371+
if [[ $(cat ${TEMPDIR}/docker-${CONTAINER_NAME}/README.md | wc -m) -gt 25000 ]]; then
358372
echo "Readme is longer than 25,000 characters. Syncing the lite version to Docker Hub"
359373
DH_README_SYNC_PATH="${TEMPDIR}/docker-${CONTAINER_NAME}/.jenkins-external/README.lite"
360374
else
361375
echo "Syncing readme to Docker Hub"
362376
DH_README_SYNC_PATH="${TEMPDIR}/docker-${CONTAINER_NAME}/README.md"
363377
fi
378+
if curl -s https://hub.docker.com/v2/namespaces/${DOCKERHUB_IMAGE%%/*}/repositories/${DOCKERHUB_IMAGE##*/}/tags | jq -r '.message' | grep -q 404; then
379+
echo "Docker Hub endpoint doesn't exist. Creating endpoint first."
380+
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')
381+
curl -s \
382+
-H "Authorization: JWT ${DH_TOKEN}" \
383+
-H "Content-Type: application/json" \
384+
-X POST \
385+
-d '{"name":"'${DOCKERHUB_IMAGE##*/}'", "namespace":"'${DOCKERHUB_IMAGE%%/*}'"}' \
386+
https://hub.docker.com/v2/repositories/ || :
387+
fi
364388
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')
365389
curl -s \
366390
-H "Authorization: JWT ${DH_TOKEN}" \
@@ -425,14 +449,16 @@ pipeline {
425449
}
426450
steps{
427451
sh '''curl -H "Content-Type: application/json" -H "Private-Token: ${GITLAB_TOKEN}" -X POST https://gitlab.com/api/v4/projects \
428-
-d '{"namespace_id":'${GITLAB_NAMESPACE}',\
429-
"name":"'${LS_REPO}'",
430-
"mirror":true,\
431-
"import_url":"https://github.com/linuxserver/'${LS_REPO}'.git",\
432-
"issues_access_level":"disabled",\
433-
"merge_requests_access_level":"disabled",\
434-
"repository_access_level":"enabled",\
435-
"visibility":"public"}' '''
452+
-d '{"namespace_id":'${GITLAB_NAMESPACE}',\
453+
"name":"'${LS_REPO}'",
454+
"mirror":true,\
455+
"import_url":"https://github.com/linuxserver/'${LS_REPO}'.git",\
456+
"issues_access_level":"disabled",\
457+
"merge_requests_access_level":"disabled",\
458+
"repository_access_level":"enabled",\
459+
"visibility":"public"}' '''
460+
sh '''curl -H "Private-Token: ${GITLAB_TOKEN}" -X PUT "https://gitlab.com/api/v4/projects/Linuxserver.io%2F${LS_REPO}" \
461+
-d "mirror=true&import_url=https://github.com/linuxserver/${LS_REPO}.git" '''
436462
}
437463
}
438464
/* ###############

0 commit comments

Comments
 (0)