forgejo-runner/entrypoint.sh

78 lines
2 KiB
Bash

#!/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}