setup-forgejo/forgejo-runner.sh

81 lines
2.5 KiB
Bash
Raw Normal View History

2023-03-24 17:42:52 -04:00
#!/bin/bash
2023-04-01 05:08:06 -04:00
# SPDX-License-Identifier: MIT
2023-03-24 17:42:52 -04:00
set -e
2023-03-24 17:42:52 -04:00
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)}
2023-03-24 17:42:52 -04:00
function dependencies() {
if ! which make curl daemon > /dev/null ; then
apt-get install -y -qq make curl daemon
2023-03-24 17:42:52 -04:00
fi
}
function download() {
local runner_repository="$1"
local version="$2"
2023-03-24 17:42:52 -04:00
2023-12-20 16:22:14 -05:00
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
2023-12-20 16:22:14 -05:00
curl -L --fail -sS $runner_repository/releases/download/$version/forgejo-runner-linux-amd64 > $DIR/forgejo-runner
fi
2023-12-20 16:22:14 -05:00
chmod 755 $DIR/forgejo-runner
fi
2023-03-24 17:42:52 -04:00
}
function register() {
local forgejo="$1"
mktemp -d > $DIR/forgejo-runner-home
$DIR/forgejocli actions generate-runner-token | tee $DIR/forgejo-runner-token
(
cd $DIR
2023-12-20 16:22:14 -05:00
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,ubuntu-latest:docker://code.forgejo.org/oci/node:20-bookworm,self-hosted:lxc://debian:bookworm
)
2023-03-24 17:42:52 -04:00
}
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 -- \
2023-12-20 16:22:14 -05:00
$DIR/forgejo-runner --config $FORGEJO_RUNNER_CONFIG daemon
2023-03-27 13:39:19 -04:00
sleep 1
cat $DIR/forgejo-runner.log
2023-03-24 17:42:52 -04:00
}
2023-07-08 08:06:13 -04:00
function reload() {
teardown
run
}
2023-03-24 17:42:52 -04:00
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}}"
2023-12-19 12:07:00 -05:00
local forgejo="${3:-$(cat $DIR/forgejo-url)}"
2023-03-24 17:42:52 -04:00
dependencies
if [[ "$version" =~ ^@ ]] ; then
local branch=${version##@}
2023-12-20 16:22:14 -05:00
build_runner $runner_repository $branch $DIR
else
download $runner_repository $version
fi
2023-03-24 17:42:52 -04:00
register $forgejo
run
}
function teardown() {
stop_daemon forgejo-runner $DIR
rm -fr $(cat $DIR/forgejo-runner-home)
}
2023-03-24 17:42:52 -04:00
"$@"