Adding retry after config and formatted logging

Adding a basic retry loop during configuration. If configuration fails,
the runner will just straight into a retry loop and will continuously
fail until it dies after a while.

This change will retry 10 times and will exit if the configuration
wasn't successful.

Also, changed the logging format, adding a bit of color in the event of
success or failure.
This commit is contained in:
Sebastien Le Digabel 2021-07-27 15:11:43 +01:00 committed by Yusuke Kuoka
parent 6f27b4920e
commit d355f05ac0
1 changed files with 70 additions and 39 deletions

View File

@ -1,23 +1,40 @@
#!/bin/bash #!/bin/bash
LIGHTGREEN="\e[0;32m"
LIGHTRED="\e[0;31m"
WHITE="\e[0;97m"
RESET="\e[0m"
log(){
echo -e "${WHITE}${@}${RESET}" 1>&2
}
success(){
echo -e "${LIGHTGREEN}${@}${RESET}" 1>&2
}
error(){
echo -e "${LIGHTRED}${@}${RESET}" 1>&2
}
if [ ! -z "${STARTUP_DELAY_IN_SECONDS}" ]; then if [ ! -z "${STARTUP_DELAY_IN_SECONDS}" ]; then
echo "Delaying startup by ${STARTUP_DELAY_IN_SECONDS} seconds" 1>&2 log "Delaying startup by ${STARTUP_DELAY_IN_SECONDS} seconds"
sleep ${STARTUP_DELAY_IN_SECONDS} sleep ${STARTUP_DELAY_IN_SECONDS}
fi fi
if [ -z "${GITHUB_URL}" ]; then if [ -z "${GITHUB_URL}" ]; then
echo "Working with public GitHub" 1>&2 log "Working with public GitHub"
GITHUB_URL="https://github.com/" GITHUB_URL="https://github.com/"
else else
length=${#GITHUB_URL} length=${#GITHUB_URL}
last_char=${GITHUB_URL:length-1:1} last_char=${GITHUB_URL:length-1:1}
[[ $last_char != "/" ]] && GITHUB_URL="$GITHUB_URL/"; : [[ $last_char != "/" ]] && GITHUB_URL="$GITHUB_URL/"; :
echo "Github endpoint URL ${GITHUB_URL}" log "Github endpoint URL ${GITHUB_URL}"
fi fi
if [ -z "${RUNNER_NAME}" ]; then if [ -z "${RUNNER_NAME}" ]; then
echo "RUNNER_NAME must be set" 1>&2 error "RUNNER_NAME must be set"
exit 1 exit 1
fi fi
@ -30,12 +47,12 @@ elif [ -n "${RUNNER_REPO}" ]; then
elif [ -n "${RUNNER_ENTERPRISE}" ]; then elif [ -n "${RUNNER_ENTERPRISE}" ]; then
ATTACH="enterprises/${RUNNER_ENTERPRISE}" ATTACH="enterprises/${RUNNER_ENTERPRISE}"
else else
echo "At least one of RUNNER_ORG or RUNNER_REPO or RUNNER_ENTERPRISE must be set" 1>&2 error "At least one of RUNNER_ORG or RUNNER_REPO or RUNNER_ENTERPRISE must be set"
exit 1 exit 1
fi fi
if [ -z "${RUNNER_TOKEN}" ]; then if [ -z "${RUNNER_TOKEN}" ]; then
echo "RUNNER_TOKEN must be set" 1>&2 error "RUNNER_TOKEN must be set"
exit 1 exit 1
fi fi
@ -45,7 +62,7 @@ fi
# Hack due to https://github.com/actions-runner-controller/actions-runner-controller/issues/252#issuecomment-758338483 # Hack due to https://github.com/actions-runner-controller/actions-runner-controller/issues/252#issuecomment-758338483
if [ ! -d /runner ]; then if [ ! -d /runner ]; then
echo "/runner should be an emptyDir mount. Please fix the pod spec." 1>&2 error "/runner should be an emptyDir mount. Please fix the pod spec."
exit 1 exit 1
fi fi
@ -60,7 +77,10 @@ if [ "${RUNNER_FEATURE_FLAG_EPHEMERAL:-}" == "true" -a "${RUNNER_EPHEMERAL}" !=
echo "Passing --ephemeral to config.sh to enable the ephemeral runner." echo "Passing --ephemeral to config.sh to enable the ephemeral runner."
fi fi
./config.sh --unattended --replace \ retries_left=10
while [[ ${retries_left} -gt 0 ]]; do
log "Configuring the runner."
./config.sh --unattended --replace \
--name "${RUNNER_NAME}" \ --name "${RUNNER_NAME}" \
--url "${GITHUB_URL}${ATTACH}" \ --url "${GITHUB_URL}${ATTACH}" \
--token "${RUNNER_TOKEN}" \ --token "${RUNNER_TOKEN}" \
@ -68,34 +88,45 @@ fi
--labels "${RUNNER_LABELS}" \ --labels "${RUNNER_LABELS}" \
--work "${RUNNER_WORKDIR}" "${config_args[@]}" --work "${RUNNER_WORKDIR}" "${config_args[@]}"
if [ -f /runner/.runner ]; then if [ -f /runner/.runner ]; then
echo Runner has successfully been configured with the following data. success "Runner successfully configured."
cat /runner/.runner break
# Note: the `.runner` file's content should be something like the below: fi
#
# $ cat /runner/.runner retries_left=$((retries_left - 1))
# { sleep 1
# "agentId": 117, #=> corresponds to the ID of the runner done
# "agentName": "THE_RUNNER_POD_NAME",
# "poolId": 1, if [ ! -f /runner/.runner ]; then
# "poolName": "Default", # we couldn't configure and register the runner; no point continuing
# "serverUrl": "https://pipelines.actions.githubusercontent.com/SOME_RANDOM_ID", error "Configuration failed!"
# "gitHubUrl": "https://github.com/USER/REPO", exit 2
# "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
fi 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 if [ -n "${RUNNER_REGISTRATION_ONLY}" ]; then
echo success "This runner is configured to be registration-only. Exiting without starting the runner service..."
echo "This runner is configured to be registration-only. Exiting without starting the runner service..."
exit 0 exit 0
fi fi