mirror of
https://github.com/hanzoai/docker-github-actions-runner.git
synced 2026-09-30 21:42:55 +00:00
120 lines
3.4 KiB
Bash
120 lines
3.4 KiB
Bash
#!/usr/bin/dumb-init /bin/bash
|
|
|
|
export RUNNER_ALLOW_RUNASROOT=1
|
|
export PATH=$PATH:/actions-runner
|
|
|
|
# Un-export these, so that they must be passed explicitly to the environment of
|
|
# any command that needs them. This may help prevent leaks.
|
|
export -n ACCESS_TOKEN
|
|
export -n RUNNER_TOKEN
|
|
|
|
deregister_runner() {
|
|
echo "Caught SIGTERM. Deregistering runner"
|
|
if [[ -n "${ACCESS_TOKEN}" ]]; then
|
|
_TOKEN=$(ACCESS_TOKEN="${ACCESS_TOKEN}" bash /token.sh)
|
|
RUNNER_TOKEN=$(echo "${_TOKEN}" | jq -r .token)
|
|
fi
|
|
./config.sh remove --token "${RUNNER_TOKEN}"
|
|
exit
|
|
}
|
|
|
|
_DISABLE_AUTOMATIC_DEREGISTRATION=${DISABLE_AUTOMATIC_DEREGISTRATION:-false}
|
|
|
|
_RUNNER_NAME=${RUNNER_NAME:-${RUNNER_NAME_PREFIX:-github-runner}-$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 13 ; echo '')}
|
|
_RUNNER_WORKDIR=${RUNNER_WORKDIR:-/_work}
|
|
_LABELS=${LABELS:-default}
|
|
_RUNNER_GROUP=${RUNNER_GROUP:-Default}
|
|
_GITHUB_HOST=${GITHUB_HOST:="github.com"}
|
|
|
|
# ensure backwards compatibility
|
|
if [[ -z $RUNNER_SCOPE ]]; then
|
|
if [[ ${ORG_RUNNER} == "true" ]]; then
|
|
echo 'ORG_RUNNER is now deprecated. Please use RUNNER_SCOPE="org" instead.'
|
|
export RUNNER_SCOPE="org"
|
|
else
|
|
export RUNNER_SCOPE="repo"
|
|
fi
|
|
fi
|
|
|
|
RUNNER_SCOPE="${RUNNER_SCOPE,,}" # to lowercase
|
|
|
|
case ${RUNNER_SCOPE} in
|
|
org*)
|
|
[[ -z ${ORG_NAME} ]] && ( echo "ORG_NAME required for org runners"; exit 1 )
|
|
_SHORT_URL="https://${_GITHUB_HOST}/${ORG_NAME}"
|
|
RUNNER_SCOPE="org"
|
|
;;
|
|
|
|
ent*)
|
|
[[ -z ${ENTERPRISE_NAME} ]] && ( echo "ENTERPRISE_NAME required for enterprise runners"; exit 1 )
|
|
_SHORT_URL="https://${_GITHUB_HOST}/enterprises/${ENTERPRISE_NAME}"
|
|
RUNNER_SCOPE="enterprise"
|
|
;;
|
|
|
|
*)
|
|
[[ -z ${REPO_URL} ]] && ( echo "REPO_URL required for repo runners"; exit 1 )
|
|
_SHORT_URL=${REPO_URL}
|
|
RUNNER_SCOPE="repo"
|
|
;;
|
|
esac
|
|
|
|
configure_runner() {
|
|
if [[ -n "${ACCESS_TOKEN}" ]]; then
|
|
echo "Obtaining the token of the runner"
|
|
_TOKEN=$(ACCESS_TOKEN="${ACCESS_TOKEN}" bash /token.sh)
|
|
RUNNER_TOKEN=$(echo "${_TOKEN}" | jq -r .token)
|
|
fi
|
|
|
|
if [ -n "${EPHEMERAL}" ]; then
|
|
echo "Ephemeral option is enabled"
|
|
_EPHEMERAL="--ephemeral"
|
|
else
|
|
_EPHEMERAL=""
|
|
fi
|
|
|
|
echo "Configuring"
|
|
./config.sh \
|
|
--url "${_SHORT_URL}" \
|
|
--token "${RUNNER_TOKEN}" \
|
|
--name "${_RUNNER_NAME}" \
|
|
--work "${_RUNNER_WORKDIR}" \
|
|
--labels "${_LABELS}" \
|
|
--runnergroup "${_RUNNER_GROUP}" \
|
|
--unattended \
|
|
--replace ${_EPHEMERAL}
|
|
}
|
|
|
|
|
|
# Opt into runner reusage because a value was given
|
|
if [[ -n "${CONFIGURED_ACTIONS_RUNNER_FILES_DIR}" ]]; then
|
|
echo "Runner reusage is enabled"
|
|
|
|
# directory exists, copy the data
|
|
if [[ -d "${CONFIGURED_ACTIONS_RUNNER_FILES_DIR}" ]]; then
|
|
echo "Copying previous data"
|
|
cp -p -r "${CONFIGURED_ACTIONS_RUNNER_FILES_DIR}/." "/actions-runner"
|
|
fi
|
|
|
|
if [ -f "/actions-runner/.runner" ]; then
|
|
echo "The runner has already been configured"
|
|
else
|
|
configure_runner
|
|
fi
|
|
else
|
|
echo "Runner reusage is disabled"
|
|
configure_runner
|
|
fi
|
|
|
|
if [[ -n "${CONFIGURED_ACTIONS_RUNNER_FILES_DIR}" ]]; then
|
|
echo "Reusage is enabled. Storing data to ${CONFIGURED_ACTIONS_RUNNER_FILES_DIR}"
|
|
# Quoting (even with double-quotes) the regexp brokes the copying
|
|
cp -p -r "/actions-runner/_diag" "/actions-runner/svc.sh" /actions-runner/.[^.]* "${CONFIGURED_ACTIONS_RUNNER_FILES_DIR}"
|
|
fi
|
|
|
|
if [[ ${_DISABLE_AUTOMATIC_DEREGISTRATION} == "false" ]]; then
|
|
trap deregister_runner SIGINT SIGQUIT SIGTERM INT TERM QUIT
|
|
fi
|
|
|
|
# Container's command (CMD) execution
|
|
"$@"
|