2024-09-25 17:46:06 +01:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
2024-09-25 23:21:53 +01:00
|
|
|
run_command() {
|
|
|
|
local cmd="$1"
|
2024-09-27 03:00:54 +01:00
|
|
|
|
|
|
|
# Replace any --token <value> or --secret <value> with [REDACTED]
|
|
|
|
local safe_cmd=$(echo "$cmd" | sed -E 's/--(token|secret) [^ ]+/--\1 [REDACTED]/g')
|
|
|
|
|
|
|
|
decho "Running command: $safe_cmd"
|
|
|
|
|
2024-09-25 23:21:53 +01:00
|
|
|
if [[ "$ISROOT" == true ]]; then
|
2024-09-26 23:08:31 +01:00
|
|
|
decho "Running as forgejo-runner"
|
2024-09-25 23:21:53 +01:00
|
|
|
su -c "$cmd" forgejo-runner
|
|
|
|
else
|
2024-09-27 03:00:54 +01:00
|
|
|
decho "Running as $(whoami)"
|
2024-09-25 23:21:53 +01:00
|
|
|
eval "$cmd"
|
|
|
|
fi
|
|
|
|
}
|
2024-09-25 21:34:18 +01:00
|
|
|
|
2024-09-27 03:00:54 +01:00
|
|
|
makeUser() {
|
|
|
|
adduser -u ${RUNNER_USER} -h /data -s /bin/bash -D forgejo-runner
|
|
|
|
}
|
|
|
|
makeGroup() {
|
|
|
|
addgroup -g ${RUNNER_USER} forgejo-runner
|
|
|
|
}
|
|
|
|
|
2024-09-26 23:08:31 +01:00
|
|
|
decho() {
|
|
|
|
if [[ "${DEBUG}" == "true" ]]; then
|
|
|
|
echo "[entrypoint] $@"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2024-09-25 21:34:18 +01:00
|
|
|
# Initial setup
|
2024-09-25 17:46:06 +01:00
|
|
|
cd /data
|
|
|
|
|
2024-09-26 23:08:31 +01:00
|
|
|
decho "RUNNER_USER: ${RUNNER_USER}"
|
|
|
|
RUNNER_USER="${RUNNER_USER:-1000}"
|
2024-09-25 23:21:53 +01:00
|
|
|
# Check if the script is running as root
|
|
|
|
if [[ $(id -u) -eq 0 ]]; then
|
|
|
|
ISROOT=true
|
2024-09-26 23:08:31 +01:00
|
|
|
decho "Running as root"
|
2024-09-25 17:46:06 +01:00
|
|
|
fi
|
2024-09-25 21:34:18 +01:00
|
|
|
|
2024-09-25 23:21:53 +01:00
|
|
|
if [[ "$ISROOT" == true ]]; then
|
2024-09-27 03:00:54 +01:00
|
|
|
# Check if the forgejo-runner user exists, if not, create it
|
|
|
|
if ! id -u forgejo-runner >/dev/null 2>&1; then
|
|
|
|
decho "Creating user forgejo-runner with UID ${RUNNER_USER}"
|
|
|
|
makeUser
|
2024-09-25 23:21:53 +01:00
|
|
|
else
|
2024-09-27 03:00:54 +01:00
|
|
|
CURRENT_UID=$(id -u forgejo-runner)
|
|
|
|
if [[ "${CURRENT_UID}" -ne "${RUNNER_USER}" ]]; then
|
|
|
|
decho "Changing UID of forgejo-runner from ${CURRENT_UID} to ${RUNNER_USER}"
|
|
|
|
deluser forgejo-runner
|
|
|
|
makeUser
|
|
|
|
fi
|
2024-09-25 23:21:53 +01:00
|
|
|
fi
|
|
|
|
|
2024-09-27 03:00:54 +01:00
|
|
|
# Check if the forgejo-runner group exists, if not, create it
|
|
|
|
if ! getent group forgejo-runner >/dev/null 2>&1; then
|
|
|
|
decho "Creating group forgejo-runner with GID ${RUNNER_USER}"
|
|
|
|
makeGroup
|
|
|
|
else
|
|
|
|
CURRENT_GID=$(getent group forgejo-runner | cut -d: -f3)
|
|
|
|
if [[ "${CURRENT_GID}" -ne "${RUNNER_USER}" ]]; then
|
|
|
|
decho "Changing GID of forgejo-runner from ${CURRENT_GID} to ${RUNNER_USER}"
|
|
|
|
delgroup forgejo-runner
|
|
|
|
makeGroup
|
|
|
|
fi
|
2024-09-26 23:08:31 +01:00
|
|
|
fi
|
2024-09-27 03:00:54 +01:00
|
|
|
|
|
|
|
# Ensure /data is owned by the runner user
|
|
|
|
# yes this can slow things down but is 100% nessecary for the runner to function
|
|
|
|
# when running as a root user, because for some reason the runner create files as
|
|
|
|
# root and then cant access them
|
|
|
|
chown -R forgejo-runner:forgejo-runner /data
|
2024-09-25 23:21:53 +01:00
|
|
|
fi
|
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
|
2024-09-25 23:21:53 +01:00
|
|
|
echo "CONFIG_FILE is not set"
|
2024-09-25 17:46:06 +01:00
|
|
|
CONFIG_FILE="/data/config.yml"
|
|
|
|
fi
|
|
|
|
CONFIG_ARG="--config ${CONFIG_FILE}"
|
2024-09-26 23:08:31 +01:00
|
|
|
decho "CONFIG: ${CONFIG_ARG}"
|
2024-09-25 17:46:06 +01:00
|
|
|
|
2024-09-25 21:00:54 +01:00
|
|
|
DOCKER_HOST=${DOCKER_HOST:-docker}
|
2024-09-27 03:00:54 +01:00
|
|
|
DOCKER_CERT_PATH=${DOCKER_CERT_PATH:-"/certs/client"}
|
2024-09-26 23:08:31 +01:00
|
|
|
DOCKER_TLS_VERIFY=${DOCKER_TLS_VERIFY:-0}
|
|
|
|
decho "DOCKER_HOST: ${DOCKER_HOST}"
|
2024-09-27 03:00:54 +01:00
|
|
|
decho "DOCKER_CERT_PATH: ${DOCKER_CERT_PATH}"
|
2024-09-26 23:08:31 +01:00
|
|
|
decho "DOCKER_TLS_VERIFY: ${DOCKER_TLS_VERIFY}"
|
2024-09-25 17:46:06 +01:00
|
|
|
if [[ ! -f "${CONFIG_FILE}" ]]; then
|
2024-09-26 23:08:31 +01:00
|
|
|
echo "Creating ${CONFIG_FILE}"
|
2024-09-27 03:00:54 +01:00
|
|
|
run_command "forgejo-runner generate-config > ${CONFIG_FILE}"
|
2024-09-25 21:00:54 +01:00
|
|
|
|
|
|
|
# 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-27 03: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-26 01:10:50 +01:00
|
|
|
|
2024-09-26 23:08:31 +01:00
|
|
|
|
2024-09-26 01:10:50 +01:00
|
|
|
if [[ "${DOCKER_TLS_VERIFY}" -ne 1 ]]; then
|
2024-09-26 23:08:31 +01:00
|
|
|
decho "Docker TLS diabled"
|
2024-09-27 03:00:54 +01:00
|
|
|
sed -i "/^ docker_host:/c\ docker_host: ${DOCKER_HOST}" ${CONFIG_FILE}
|
2024-09-26 01:10:50 +01:00
|
|
|
else
|
2024-09-26 23:08:31 +01:00
|
|
|
decho "Docker TLS enabled"
|
2024-09-27 03:00:54 +01:00
|
|
|
sed -i "/^ docker_host:/c\ docker_host: ${DOCKER_HOST}" ${CONFIG_FILE}
|
|
|
|
sed -i "/^ valid_volumes:/c\ valid_volumes:\n - ${DOCKER_CERT_PATH}" ${CONFIG_FILE}
|
|
|
|
sed -i "/^ options:/c\ options: -v ${DOCKER_CERT_PATH}:${DOCKER_CERT_PATH}" ${CONFIG_FILE}
|
2024-09-26 01:10:50 +01:00
|
|
|
fi
|
2024-09-25 21:00:54 +01:00
|
|
|
fi
|
|
|
|
|
2024-09-26 23:08:31 +01:00
|
|
|
ENV_FILE=${ENV_FILE:-"/data/.env"}
|
|
|
|
decho "ENV_FILE: ${ENV_FILE}"
|
|
|
|
sed -i "/^ env_file:/c\ env_file: ${ENV_FILE}" ${CONFIG_FILE}
|
2024-09-25 21:34:18 +01:00
|
|
|
|
2024-09-25 21:00:54 +01:00
|
|
|
if [[ ! -f "${ENV_FILE}" ]]; then
|
2024-09-26 01:10:50 +01:00
|
|
|
echo "Creating ${ENV_FILE}"
|
|
|
|
touch ${ENV_FILE}
|
2024-09-26 23:08:31 +01:00
|
|
|
echo "DOCKER_HOST=${DOCKER_HOST}" >> ${ENV_FILE}
|
|
|
|
echo "DOCKER_TLS_VERIFY=${DOCKER_TLS_VERIFY}" >> ${ENV_FILE}
|
2024-09-27 03:00:54 +01:00
|
|
|
echo "DOCKER_CERT_PATH=${DOCKER_CERT_PATH}" >> ${ENV_FILE}
|
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-26 23:08:31 +01:00
|
|
|
decho "EXTRA_ARGS: ${EXTRA_ARGS}"
|
2024-09-25 17:46:06 +01:00
|
|
|
|
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-26 23:08:31 +01:00
|
|
|
RUNNER_FILE="runner.json" # use json so editors know how to highlight
|
2024-09-25 17:46:06 +01:00
|
|
|
fi
|
2024-09-26 23:08:31 +01:00
|
|
|
decho "RUNNER_FILE: ${RUNNER_FILE}"
|
2024-09-25 17:46:06 +01:00
|
|
|
sed -i "/^ file:/c\ file: ${RUNNER_FILE}" ${CONFIG_FILE}
|
|
|
|
|
2024-09-26 23:08:31 +01:00
|
|
|
if [[ "${SKIP_WAIT}" != "true" ]]; then
|
2024-09-26 23:16:51 +01:00
|
|
|
echo "Waiting 10s to allow other services to start up..."
|
2024-09-26 23:08:31 +01:00
|
|
|
sleep 10
|
|
|
|
fi
|
|
|
|
|
2024-09-25 17:46:06 +01:00
|
|
|
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
|
2024-09-26 23:08:31 +01:00
|
|
|
decho "try: ${try}, success: ${success}"
|
2024-09-25 23:21:53 +01:00
|
|
|
|
2024-09-25 17:46:06 +01:00
|
|
|
# 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-27 03:00:54 +01:00
|
|
|
if [[ ! -z "${FORGEJO_SECRET}" ]]; then
|
2024-09-25 23:21:53 +01:00
|
|
|
run_command "forgejo-runner create-runner-file --connect \
|
|
|
|
--instance \"${FORGEJO_URL:-http://forgejo:3000}\" \
|
|
|
|
--name \"${RUNNER_NAME:-$(hostname)}\" \
|
2024-09-27 03:00:54 +01:00
|
|
|
--secret \"${FORGEJO_SECRET}\" \
|
|
|
|
${CONFIG_ARG}\
|
|
|
|
${EXTRA_ARGS} 2>&1 | tee /tmp/reg.log"
|
|
|
|
else
|
|
|
|
run_command "forgejo-runner register \
|
|
|
|
--instance \"${FORGEJO_URL:-http://forgejo:3000}\" \
|
|
|
|
--name \"${RUNNER_NAME:-$(hostname)}\" \
|
|
|
|
--token \"${RUNNER_TOKEN}\" \
|
|
|
|
--no-interactive \
|
|
|
|
${CONFIG_ARG}\
|
|
|
|
${EXTRA_ARGS} 2>&1 | tee /tmp/reg.log"
|
|
|
|
fi
|
|
|
|
cat /tmp/reg.log | grep -E 'connection successful|registered successfully' >/dev/null
|
2024-09-25 17:46:06 +01:00
|
|
|
if [[ $? -eq 0 ]]; then
|
|
|
|
echo "SUCCESS"
|
|
|
|
success=1
|
|
|
|
else
|
|
|
|
echo "Waiting to retry ..."
|
|
|
|
sleep 5
|
|
|
|
fi
|
2024-09-26 23:08:31 +01:00
|
|
|
decho "try: ${try}, success: ${success}"
|
2024-09-25 17:46:06 +01:00
|
|
|
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-26 23:08:31 +01:00
|
|
|
unset FORGEJO_SECRET
|
2024-09-25 17:46:06 +01:00
|
|
|
|
2024-09-25 23:21:53 +01:00
|
|
|
run_command "forgejo-runner daemon ${CONFIG_ARG}"
|