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
|
|
|
|
2023-12-17 16:21:10 -05:00
|
|
|
set -e
|
2023-03-24 17:42:52 -04:00
|
|
|
|
2023-10-14 15:59:11 -04:00
|
|
|
PS4='${BASH_SOURCE[0]}:$LINENO: ${FUNCNAME[0]}: '
|
2023-10-11 09:44:25 -04:00
|
|
|
SELF_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
2023-12-17 16:21:10 -05:00
|
|
|
source $SELF_DIR/forgejo-lib.sh
|
2023-10-14 15:59:11 -04:00
|
|
|
|
2023-10-11 09:44:25 -04:00
|
|
|
: ${FORGEJO_RUNNER_CONFIG:=$SELF_DIR/runner-config.yaml}
|
2023-12-17 16:21:10 -05:00
|
|
|
: ${DIR:=$(pwd)}
|
2023-05-29 11:12:23 -04:00
|
|
|
|
2023-03-24 17:42:52 -04:00
|
|
|
function dependencies() {
|
2023-10-14 15:59:11 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-04-04 06:51:16 -04:00
|
|
|
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
|
2023-10-04 11:01:08 -04:00
|
|
|
# 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
|
2023-10-04 11:01:08 -04:00
|
|
|
fi
|
2023-12-20 16:22:14 -05:00
|
|
|
chmod 755 $DIR/forgejo-runner
|
2023-04-04 12:24:21 -04:00
|
|
|
fi
|
2023-03-24 17:42:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function register() {
|
|
|
|
local forgejo="$1"
|
2023-12-17 16:21:10 -05:00
|
|
|
mktemp -d > $DIR/forgejo-runner-home
|
|
|
|
$DIR/forgejocli actions generate-runner-token | tee $DIR/forgejo-runner-token
|
|
|
|
(
|
|
|
|
cd $DIR
|
2024-12-06 12:05:18 -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,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
|
2023-12-17 16:21:10 -05:00
|
|
|
)
|
2023-03-24 17:42:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function run() {
|
2023-12-17 16:21:10 -05:00
|
|
|
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
|
2023-12-17 16:21:10 -05:00
|
|
|
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() {
|
2023-10-14 15:59:11 -04:00
|
|
|
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
|
2023-10-14 15:59:11 -04:00
|
|
|
if [[ "$version" =~ ^@ ]] ; then
|
|
|
|
local branch=${version##@}
|
2023-12-20 16:22:14 -05:00
|
|
|
build_runner $runner_repository $branch $DIR
|
2023-10-14 15:59:11 -04:00
|
|
|
else
|
|
|
|
download $runner_repository $version
|
|
|
|
fi
|
2023-03-24 17:42:52 -04:00
|
|
|
register $forgejo
|
|
|
|
run
|
|
|
|
}
|
|
|
|
|
2023-10-04 13:20:35 -04:00
|
|
|
function teardown() {
|
2023-12-19 12:38:38 -05:00
|
|
|
stop_daemon forgejo-runner $DIR
|
2023-12-20 16:24:23 -05:00
|
|
|
if test -f $DIR/forgejo-runner-home ; then
|
|
|
|
rm -fr $(cat $DIR/forgejo-runner-home)
|
|
|
|
fi
|
2023-10-04 13:20:35 -04:00
|
|
|
}
|
|
|
|
|
2023-03-24 17:42:52 -04:00
|
|
|
"$@"
|