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}