basic entrypoint, automatic registration of runner works

This commit is contained in:
merith-xyz 2024-09-25 09:46:06 -07:00
parent d3b8b3bb16
commit 5a0579f03a
2 changed files with 81 additions and 2 deletions

View file

@ -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"]

78
entrypoint.sh Normal file
View file

@ -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}