2024-09-25 17:46:06 +01:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
2024-09-25 21:34:18 +01:00
|
|
|
# Check if the script is run as root
|
|
|
|
if [ "$EUID" -ne 0 ]; then
|
|
|
|
echo "This script must be run as root"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Initial setup
|
2024-09-25 17:46:06 +01:00
|
|
|
if [[ ! -d /data ]]; then
|
|
|
|
mkdir -p /data
|
|
|
|
fi
|
|
|
|
cd /data
|
|
|
|
|
2024-09-25 21:00:54 +01:00
|
|
|
RUNNER_USERID="${RUNNER_USERID:-1000}"
|
2024-09-25 17:46:06 +01:00
|
|
|
|
2024-09-25 21:34:18 +01:00
|
|
|
# Setup User
|
2024-09-25 21:00:54 +01:00
|
|
|
if id "forgejo-runner" &>/dev/null; then
|
|
|
|
if [[ ! -z "${RUNNER_USERID}" ]]; then
|
|
|
|
echo "Changing UID of forgejo-runner to ${RUNNER_USERID}"
|
|
|
|
sed -i "s/^forgejo-runner:[^:]*:[^:]*:/forgejo-runner:x:${RUNNER_USERID}:/" /etc/passwd
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "Creating user forgejo-runner with UID ${RUNNER_USERID}"
|
|
|
|
adduser --uid "${RUNNER_USERID}" --home /home/forgejo-runner --disabled-password --gecos "" forgejo-runner
|
2024-09-25 17:46:06 +01:00
|
|
|
fi
|
2024-09-25 21:34:18 +01:00
|
|
|
|
|
|
|
# Ensure /data is owned by the runner user
|
2024-09-25 21:00:54 +01:00
|
|
|
chown -R forgejo-runner:forgejo-runner /data
|
2024-09-25 17:46:06 +01:00
|
|
|
|
2024-09-25 21:34:18 +01:00
|
|
|
# Handle and alter the config file
|
2024-09-25 17:46:06 +01:00
|
|
|
if [[ -z "${CONFIG_FILE}" ]]; then
|
|
|
|
CONFIG_FILE="/data/config.yml"
|
|
|
|
fi
|
|
|
|
CONFIG_ARG="--config ${CONFIG_FILE}"
|
|
|
|
|
2024-09-25 21:00:54 +01:00
|
|
|
DOCKER_HOST=${DOCKER_HOST:-docker}
|
|
|
|
echo "DOCKER_HOST: ${DOCKER_HOST}"
|
2024-09-25 17:46:06 +01:00
|
|
|
if [[ ! -f "${CONFIG_FILE}" ]]; then
|
2024-09-25 21:00:54 +01:00
|
|
|
su -c "forgejo-runner generate-config > ${CONFIG_FILE}" forgejo-runner
|
|
|
|
|
|
|
|
# Remove test environment variables if they exist in the config file
|
|
|
|
sed -i "/^ A_TEST_ENV_NAME_1:/d" ${CONFIG_FILE}
|
|
|
|
sed -i "/^ A_TEST_ENV_NAME_2:/d" ${CONFIG_FILE}
|
|
|
|
|
2024-09-25 21:34:18 +01:00
|
|
|
# Apply default values for docker
|
2024-09-25 21:00:54 +01:00
|
|
|
sed -i "/\"labels\":/c\ \"labels\": [\"docker:docker://code.forgejo.org/oci/node:20-bookworm\", \"ubuntu-22.04:docker://catthehacker/ubuntu:act-22.04\"]" ${CONFIG_FILE}
|
|
|
|
|
2024-09-25 21:34:18 +01:00
|
|
|
sed -i "/^ network:/c\ network: host" ${CONFIG_FILE}
|
2024-09-25 21:00:54 +01:00
|
|
|
sed -i "/^ privileged:/c\ privileged: true" ${CONFIG_FILE}
|
2024-09-25 21:34:18 +01:00
|
|
|
sed -i "/^ options:/c\ options: -v /certs/client:/certs/client" ${CONFIG_FILE}
|
2024-09-25 21:00:54 +01:00
|
|
|
sed -i "/^ docker_host:/c\ docker_host: tcp://${DOCKER_HOST}:2376" ${CONFIG_FILE}
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ ! -z "${ENV_FILE}" ]]; then
|
|
|
|
sed -i "/^ env_file:/c\ env_file: ${ENV_FILE}" ${CONFIG_FILE}
|
|
|
|
else
|
|
|
|
ENV_FILE="/data/.env"
|
|
|
|
fi
|
2024-09-25 21:34:18 +01:00
|
|
|
|
2024-09-25 21:00:54 +01:00
|
|
|
if [[ ! -f "${ENV_FILE}" ]]; then
|
|
|
|
echo "Creating ${ENV_FILE} and populating with default values"
|
|
|
|
cat <<EOF > ${ENV_FILE}
|
|
|
|
DOCKER_TLS_VERIFY: 1
|
|
|
|
DOCKER_CERT_PATH: /certs/client
|
|
|
|
EOF
|
2024-09-25 17:46:06 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
EXTRA_ARGS=""
|
|
|
|
if [[ ! -z "${RUNNER_LABELS}" ]]; then
|
|
|
|
EXTRA_ARGS="${EXTRA_ARGS} --labels ${RUNNER_LABELS}"
|
|
|
|
fi
|
|
|
|
|
2024-09-25 21:00:54 +01:00
|
|
|
# Set the runner file
|
2024-09-25 17:46:06 +01:00
|
|
|
if [[ -z "${RUNNER_FILE}" ]]; then
|
2024-09-25 21:00:54 +01:00
|
|
|
RUNNER_FILE=".runner.json" # use json so editors know how to highlight
|
2024-09-25 17:46:06 +01:00
|
|
|
fi
|
|
|
|
sed -i "/^ file:/c\ file: ${RUNNER_FILE}" ${CONFIG_FILE}
|
|
|
|
|
|
|
|
if [[ ! -s "${RUNNER_FILE}" ]]; then
|
2024-09-25 21:00:54 +01:00
|
|
|
touch ${RUNNER_FILE}
|
2024-09-25 17:46:06 +01:00
|
|
|
try=$((try + 1))
|
|
|
|
success=0
|
|
|
|
if [[ -z "${RUNNER_TOKEN}" ]]; then
|
|
|
|
echo "RUNNER_TOKEN is not set"
|
|
|
|
exit 1
|
|
|
|
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
|
2024-09-25 21:00:54 +01:00
|
|
|
su -c "forgejo-runner register \
|
|
|
|
--instance \"${FORGEJO_URL:-http://forgejo:3000}\" \
|
|
|
|
--token \"${RUNNER_TOKEN}\" \
|
|
|
|
--name \"${RUNNER_NAME:-$(hostname)}\" \
|
|
|
|
${CONFIG_ARG} ${EXTRA_ARGS} --no-interactive 2>&1 | tee /tmp/reg.log" forgejo-runner
|
2024-09-25 17:46:06 +01:00
|
|
|
|
|
|
|
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
|
2024-09-25 21:34:18 +01:00
|
|
|
|
2024-09-25 17:46:06 +01:00
|
|
|
# Prevent reading the token from the forgejo-runner process
|
|
|
|
unset RUNNER_TOKEN
|
|
|
|
|
2024-09-25 21:00:54 +01:00
|
|
|
su -c "forgejo-runner daemon ${CONFIG_ARG}" forgejo-runner
|