#!/bin/bash # SPDX-License-Identifier: MIT # RUNNER_HOME=${RUNNER_HOME:-/data} set -ex set -o pipefail mkdir -p ${RUNNER_HOME} if [ ! -d "${RUNNER_HOME}" ]; then log.error "$RUNNER_HOME should be an emptyDir mount. Please fix the pod spec." exit 1 fi if [ -z "$METADATA_URL" ]; then echo "no token is available and METADATA_URL is not set" exit 1 fi if [ -z "$CALLBACK_URL" ]; then echo "CALLBACK_URL is not set" exit 1 fi if [ -z "$GITHUB_URL" ]; then echo "GITHUB_URL is not set" exit 1 fi if [ -z "$RUNNER_NAME" ]; then echo "RUNNER_NAME is not set" exit 1 fi if [ -z "$BEARER_TOKEN" ]; then echo "BEARER_TOKEN is not set" exit 1 fi function success() { MSG="$1" ID=${2:-null} call "{\"status\": \"idle\", \"message\": \"$MSG\", \"agent_id\": $ID}" } function call() { PAYLOAD="$1" local cb_url=$CALLBACK_URL [[ $cb_url =~ ^(.*)/status(/)?$ ]] || cb_url="${cb_url}/status" curl --retry 5 --retry-delay 5 --retry-connrefused --fail -s -X POST -d "${PAYLOAD}" -H 'Accept: application/json' -H "Authorization: Bearer ${BEARER_TOKEN}" "${cb_url}" || echo "failed to call home: exit code ($?)" } set +e # retrieve runner secret from garm RUNNER_SECRET=$(curl --retry 5 --retry-delay 5 --retry-connrefused --fail -v -X GET -H 'Accept: application/json' -H "Authorization: Bearer ${BEARER_TOKEN}" "${METADATA_URL}/runner-registration-token/") if [ -z "$RUNNER_SECRET" ]; then echo Failed to retrieve runner secret from GARM >&2 sleep 30 exit 1 fi set -e forgejo-runner \ register \ --no-interactive \ --token ${RUNNER_SECRET} \ --name ${RUNNER_NAME} \ --instance ${GITHUB_URL} \ --labels "${RUNNER_LABELS//,/:host,}:host" forgejo-runner generate-config > config.yml; success "runner should be working" unset RUNNER_SECRET unset BEARER_TOKEN docker buildx create --use --name sidecar unix:///run/user/1000/buildkit/buildkitd.sock /bin/forgejo-runner --config config.yml one-job