Files
actions-runner-controller/runner/entrypoint.sh
T

173 lines
4.9 KiB
Bash
Raw Normal View History

2020-01-28 21:56:54 +09:00
#!/bin/bash
2021-08-02 10:11:48 +01:00
RUNNER_HOME=${RUNNER_HOME:-/runner}
LIGHTGREEN="\e[0;32m"
LIGHTRED="\e[0;31m"
WHITE="\e[0;97m"
RESET="\e[0m"
log(){
2021-08-02 10:11:48 +01:00
printf "${WHITE}${@}${RESET}\n" 1>&2
}
success(){
2021-08-02 10:11:48 +01:00
printf "${LIGHTGREEN}${@}${RESET}\n" 1>&2
}
error(){
2021-08-02 10:11:48 +01:00
printf "${LIGHTRED}${@}${RESET}\n" 1>&2
}
2021-07-30 11:58:04 +01:00
if [ ! -z "${STARTUP_DELAY_IN_SECONDS}" ]; then
log "Delaying startup by ${STARTUP_DELAY_IN_SECONDS} seconds"
2021-07-03 11:51:07 +01:00
sleep ${STARTUP_DELAY_IN_SECONDS}
fi
if [[ "${DISABLE_WAIT_FOR_DOCKER}" != "true" ]] && [[ "${DOCKER_ENABLED}" == "true" ]]; then
log "Docker enabled runner detected and Docker daemon wait is enabled"
log "Waiting until Docker is avaliable or the timeout is reached"
timeout 120s bash -c 'until docker ps ;do sleep 1; done'
else
log "Docker wait check skipped. Either Docker is disabled or the wait is disabled, continuing with entrypoint"
fi
2021-08-02 10:11:48 +01:00
if [ -z "${GITHUB_URL}" ]; then
log "Working with public GitHub"
GITHUB_URL="https://github.com/"
else
length=${#GITHUB_URL}
last_char=${GITHUB_URL:length-1:1}
[[ $last_char != "/" ]] && GITHUB_URL="$GITHUB_URL/"; :
log "Github endpoint URL ${GITHUB_URL}"
fi
2020-01-28 21:56:54 +09:00
if [ -z "${RUNNER_NAME}" ]; then
error "RUNNER_NAME must be set"
2020-01-28 21:56:54 +09:00
exit 1
fi
2021-02-05 02:31:06 +02:00
if [ -n "${RUNNER_ORG}" ] && [ -n "${RUNNER_REPO}" ] && [ -n "${RUNNER_ENTERPRISE}" ]; then
2020-04-23 16:36:40 +02:00
ATTACH="${RUNNER_ORG}/${RUNNER_REPO}"
2020-04-24 07:17:09 +02:00
elif [ -n "${RUNNER_ORG}" ]; then
ATTACH="${RUNNER_ORG}"
elif [ -n "${RUNNER_REPO}" ]; then
ATTACH="${RUNNER_REPO}"
2021-02-05 02:31:06 +02:00
elif [ -n "${RUNNER_ENTERPRISE}" ]; then
ATTACH="enterprises/${RUNNER_ENTERPRISE}"
2020-04-24 07:17:09 +02:00
else
error "At least one of RUNNER_ORG or RUNNER_REPO or RUNNER_ENTERPRISE must be set"
2020-04-24 07:17:09 +02:00
exit 1
2020-04-23 16:36:40 +02:00
fi
2020-01-28 21:56:54 +09:00
if [ -z "${RUNNER_TOKEN}" ]; then
error "RUNNER_TOKEN must be set"
2020-01-28 21:56:54 +09:00
exit 1
fi
if [ -z "${RUNNER_REPO}" ] && [ -n "${RUNNER_GROUP}" ];then
RUNNER_GROUPS=${RUNNER_GROUP}
fi
2021-06-22 17:55:06 +09:00
# Hack due to https://github.com/actions-runner-controller/actions-runner-controller/issues/252#issuecomment-758338483
if [ ! -d "${RUNNER_HOME}" ]; then
2021-08-02 10:11:48 +01:00
error "${RUNNER_HOME} should be an emptyDir mount. Please fix the pod spec."
exit 1
fi
2021-08-02 10:11:48 +01:00
# if this is not a testing environment
if [ -z "${UNITTEST:-}" ]; then
2021-08-02 10:11:48 +01:00
sudo chown -R runner:docker ${RUNNER_HOME}
2022-01-29 08:24:52 +11:00
# use cp over mv to avoid issues when /runnertmp and {RUNNER_HOME} are on different devices
cp -r /runnertmp/* ${RUNNER_HOME}/
2021-08-02 10:11:48 +01:00
fi
2021-08-02 10:11:48 +01:00
cd ${RUNNER_HOME}
# past that point, it's all relative pathes from /runner
config_args=()
if [ "${RUNNER_FEATURE_FLAG_EPHEMERAL:-}" == "true" -a "${RUNNER_EPHEMERAL}" != "false" ]; then
config_args+=(--ephemeral)
echo "Passing --ephemeral to config.sh to enable the ephemeral runner."
fi
if [ "${DISABLE_RUNNER_UPDATE:-}" == "true" ]; then
config_args+=(--disableupdate)
echo "Passing --disableupdate to config.sh to disable automatic runner updates."
fi
retries_left=10
while [[ ${retries_left} -gt 0 ]]; do
log "Configuring the runner."
./config.sh --unattended --replace \
--name "${RUNNER_NAME}" \
--url "${GITHUB_URL}${ATTACH}" \
--token "${RUNNER_TOKEN}" \
--runnergroup "${RUNNER_GROUPS}" \
--labels "${RUNNER_LABELS}" \
--work "${RUNNER_WORKDIR}" "${config_args[@]}"
2021-08-02 10:11:48 +01:00
if [ -f .runner ]; then
success "Runner successfully configured."
break
fi
2021-08-02 10:11:48 +01:00
error "Configuration failed. Retrying"
retries_left=$((retries_left - 1))
sleep 1
done
2021-08-02 10:11:48 +01:00
if [ ! -f .runner ]; then
# we couldn't configure and register the runner; no point continuing
error "Configuration failed!"
exit 2
fi
cat .runner
# Note: the `.runner` file's content should be something like the below:
#
# $ cat /runner/.runner
# {
# "agentId": 117, #=> corresponds to the ID of the runner
# "agentName": "THE_RUNNER_POD_NAME",
# "poolId": 1,
# "poolName": "Default",
# "serverUrl": "https://pipelines.actions.githubusercontent.com/SOME_RANDOM_ID",
# "gitHubUrl": "https://github.com/USER/REPO",
# "workFolder": "/some/work/dir" #=> corresponds to Runner.Spec.WorkDir
# }
#
# Especially `agentId` is important, as other than listing all the runners in the repo,
# this is the only change we could get the exact runnner ID which can be useful for further
# GitHub API call like the below. Note that 171 is the agentId seen above.
# curl \
# -H "Accept: application/vnd.github.v3+json" \
# -H "Authorization: bearer ${GITHUB_TOKEN}"
# https://api.github.com/repos/USER/REPO/actions/runners/171
if [ -n "${RUNNER_REGISTRATION_ONLY}" ]; then
success "This runner is configured to be registration-only. Exiting without starting the runner service..."
exit 0
fi
if [ -z "${UNITTEST:-}" ]; then
2021-08-02 10:11:48 +01:00
mkdir ./externals
# Hack due to the DinD volumes
mv ./externalstmp/* ./externals/
2021-08-02 10:11:48 +01:00
for f in runsvc.sh RunnerService.js; do
diff {bin,patched}/${f} || :
sudo mv bin/${f}{,.bak}
sudo mv {patched,bin}/${f}
done
fi
2020-10-05 01:58:20 +02:00
args=()
if [ "${RUNNER_FEATURE_FLAG_EPHEMERAL:-}" != "true" -a "${RUNNER_EPHEMERAL}" != "false" ]; then
args+=(--once)
echo "Passing --once to runsvc.sh to enable the legacy ephemeral runner."
fi
unset RUNNER_NAME RUNNER_REPO RUNNER_TOKEN
exec ./bin/runsvc.sh "${args[@]}"