setup-forgejo/forgejo-runner.sh

95 lines
2.9 KiB
Bash
Executable file

#!/bin/bash
# SPDX-License-Identifier: MIT
set -ex
PS4='${BASH_SOURCE[0]}:$LINENO: ${FUNCNAME[0]}: '
SELF_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source $SELF_DIR/forgejo-runner-lib.sh
: ${FORGEJO_RUNNER_CONFIG:=$SELF_DIR/runner-config.yaml}
function dependencies() {
if ! which make curl daemon > /dev/null ; then
apt-get install -y -qq make curl daemon
fi
}
function download() {
local runner_repository="$1"
local version="$2"
if ! which forgejo-runner > /dev/null; then
if ! curl -L --fail -sS $runner_repository/releases/download/$version/forgejo-runner-${version#v}-linux-amd64 > /bin/forgejo-runner ; then
# backward compatibility for for the naming scheme prior to 3.0.0
curl -L --fail -sS $runner_repository/releases/download/$version/forgejo-runner-linux-amd64 > /bin/forgejo-runner
fi
chmod 755 /bin/forgejo-runner
fi
}
function register() {
local forgejo="$1"
mktemp -d > forgejo-runner-home
docker exec --user 1000 forgejo forgejo actions generate-runner-token > forgejo-runner-token
timeout --signal=KILL 30 forgejo-runner register --no-interactive --instance "$forgejo" --name runner --token $(cat forgejo-runner-token) --labels docker:docker://code.forgejo.org/oci/node:16-bullseye,ubuntu-latest:docker://code.forgejo.org/oci/node:16-buster,self-hosted
}
function run() {
rm -f forgejo-runner.log
daemon --chdir=$(pwd) --unsafe \
--env="TERM=$TERM" --env="HOME=$(cat forgejo-runner-home)" --env="PATH=$PATH" --env="USER=$USER" --env="TERM=dumb" --env="USERNAME=$USERNAME" --env="LANG=$LANG" \
--pidfile=$(pwd)/forgejo-runner-pid --errlog=$(pwd)/forgejo-runner.log --output=$(pwd)/forgejo-runner.log -- \
forgejo-runner --config $FORGEJO_RUNNER_CONFIG daemon
sleep 1
cat forgejo-runner.log
}
function reload() {
teardown
rm -f forgejo-runner.log
run
}
function setup() {
local default_runner_repository="$(cat $SELF_DIR/RUNNER_REPOSITORY)"
local runner_repository="${1:-${default_runner_repository}}"
local default_version="$(cat $SELF_DIR/RUNNER_VERSION)"
local version="${2:-${default_version}}"
local forgejo="${3:-http://$(cat forgejo-ip):3000/}"
dependencies
if [[ "$version" =~ ^@ ]] ; then
local branch=${version##@}
build_runner $runner_repository $branch
else
download $runner_repository $version
fi
register $forgejo
run
}
function stop_daemon() {
local daemon=$1
local DIR=.
if test -f $DIR/$daemon-pid ; then
local pid=$(cat $DIR/$daemon-pid)
kill -TERM $pid
pidwait $pid || true
for delay in 1 1 2 2 5 5 ; do
if ! test -f $DIR/$daemon-pid ; then
break
fi
sleep $delay
done
! test -f $DIR/$daemon-pid
fi
}
function teardown() {
stop_daemon forgejo-runner
rm -fr $(cat forgejo-runner-home)
}
"$@"