From 5a0579f03aaaed5d7bb8216a30c2573ea2dc0d8a Mon Sep 17 00:00:00 2001 From: merith-xyz Date: Wed, 25 Sep 2024 09:46:06 -0700 Subject: [PATCH] basic entrypoint, automatic registration of runner works --- Dockerfile | 5 ++-- entrypoint.sh | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 entrypoint.sh diff --git a/Dockerfile b/Dockerfile index 50f1965..6843a3a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -38,10 +38,11 @@ LABEL maintainer="contact@forgejo.org" \ ENV HOME=/data -USER 1000:1000 +COPY entrypoint.sh /entrypoint.sh +RUN chmod +x /entrypoint.sh WORKDIR /data VOLUME ["/data"] -CMD ["/bin/forgejo-runner"] +ENTRYPOINT ["/entrypoint.sh"] diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..4fd5ae8 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,78 @@ +#!/usr/bin/env bash + +set -e + +## Initial setup +if [[ ! -d /data ]]; then + mkdir -p /data +fi +cd /data + +if [[ -z "${RUNNER_FILE}" ]]; then + RUNNER_FILE="/data/.runner" +fi + +if [[ ! -f "${RUNNER_FILE}" ]]; then + touch "${RUNNER_FILE}" +fi + +if [[ -z "${CONFIG_FILE}" ]]; then + CONFIG_FILE="/data/config.yml" +fi +CONFIG_ARG="--config ${CONFIG_FILE}" + +if [[ ! -f "${CONFIG_FILE}" ]]; then + forgejo-runner generate-config > ${CONFIG_FILE} +fi + +EXTRA_ARGS="" +if [[ ! -z "${RUNNER_LABELS}" ]]; then + EXTRA_ARGS="${EXTRA_ARGS} --labels ${RUNNER_LABELS}" +fi + +# For simplicity sake, I am not using the same ENV variable names as the original script + +if [[ -z "${RUNNER_FILE}" ]]; then + RUNNER_FILE=".runner" +fi +sed -i "/^ file:/c\ file: ${RUNNER_FILE}" ${CONFIG_FILE} + +if [[ ! -s "${RUNNER_FILE}" ]]; then + try=$((try + 1)) + success=0 + if [[ -z "${RUNNER_TOKEN}" ]]; then + echo "RUNNER_TOKEN is not set" + exit 1 + fi + + if [[ -z "${FORGEJO_URL}" ]]; then + echo "FORGEJO_URL is not set" + echo "Defaulting to http://forgejo:8080" + fi + + + # The point of this loop is to make it simple, when running both forgejo-runner and gitea in docker, + # for the forgejo-runner to wait a moment for gitea to become available before erroring out. Within + # the context of a single docker-compose, something similar could be done via healthchecks, but + # this is more flexible. + while [[ $success -eq 0 ]] && [[ $try -lt ${MAX_REG_ATTEMPTS:-10} ]]; do + forgejo-runner register \ + --instance "${FORGEJO_URL:-http://forgejo:8080}" \ + --token "${RUNNER_TOKEN}" \ + --name "${RUNNER_NAME:-$(hostname)}" \ + ${CONFIG_ARG} ${EXTRA_ARGS} --no-interactive 2>&1 | tee /tmp/reg.log + + cat /tmp/reg.log | grep 'Runner registered successfully' >/dev/null + if [[ $? -eq 0 ]]; then + echo "SUCCESS" + success=1 + else + echo "Waiting to retry ..." + sleep 5 + fi + done +fi +# Prevent reading the token from the forgejo-runner process +unset RUNNER_TOKEN + +forgejo-runner daemon ${CONFIG_ARG}