setup-forgejo/forgejo-runner.sh
2024-12-07 15:22:38 +01:00

83 lines
2.7 KiB
Bash
Executable file

#!/bin/bash
# SPDX-License-Identifier: MIT
set -e
PS4='${BASH_SOURCE[0]}:$LINENO: ${FUNCNAME[0]}: '
SELF_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source $SELF_DIR/forgejo-lib.sh
: ${FORGEJO_RUNNER_CONFIG:=$SELF_DIR/runner-config.yaml}
: ${DIR:=$(pwd)}
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 $DIR/forgejo-runner > /dev/null; then
if ! curl -L --fail -sS $runner_repository/releases/download/$version/forgejo-runner-${version#v}-linux-amd64 > $DIR/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 > $DIR/forgejo-runner
fi
chmod 755 $DIR/forgejo-runner
fi
}
function register() {
local forgejo="$1"
mktemp -d > $DIR/forgejo-runner-home
$DIR/forgejocli actions generate-runner-token | tee $DIR/forgejo-runner-token
(
cd $DIR
timeout --signal=KILL 30 $DIR/forgejo-runner register --no-interactive --instance "$forgejo" --name runner --token $(cat $DIR/forgejo-runner-token) \
--labels docker:docker://code.forgejo.org/oci/node:20-bookworm,docker-bookworm:docker://code.forgejo.org/oci/node:20-bookworm,lxc-bookworm:lxc://debian:bookworm,ubuntu-latest:docker://code.forgejo.org/oci/node:20-bookworm,self-hosted:lxc://debian:bookworm,lxc-bullseye:lxc://debian:bullseye
)
}
function run() {
rm -f $DIR/forgejo-runner.log
daemon --chdir=$DIR --unsafe \
--env="TERM=$TERM" --env="HOME=$(cat $DIR/forgejo-runner-home)" --env="PATH=$PATH" --env="USER=$USER" --env="TERM=dumb" --env="USERNAME=$USERNAME" --env="LANG=$LANG" \
--pidfile=$DIR/forgejo-runner-pid --errlog=$DIR/forgejo-runner.log --output=$DIR/forgejo-runner.log -- \
$DIR/forgejo-runner --config $FORGEJO_RUNNER_CONFIG daemon
sleep 1
cat $DIR/forgejo-runner.log
}
function reload() {
teardown
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:-$(cat $DIR/forgejo-url)}"
dependencies
if [[ "$version" =~ ^@ ]] ; then
local branch=${version##@}
build_runner $runner_repository $branch $DIR
else
download $runner_repository $version
fi
register $forgejo
run
}
function teardown() {
stop_daemon forgejo-runner $DIR
if test -f $DIR/forgejo-runner-home ; then
rm -fr $(cat $DIR/forgejo-runner-home)
fi
}
"$@"