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 -ex
|
|
|
|
|
2023-10-11 09:44:25 -04:00
|
|
|
SELF_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
|
|
|
: ${FORGEJO_RUNNER_CONFIG:=$SELF_DIR/runner-config.yaml}
|
2023-05-29 11:12:23 -04:00
|
|
|
|
2023-03-24 17:42:52 -04:00
|
|
|
function dependencies() {
|
2023-04-04 06:51:16 -04:00
|
|
|
if ! which curl daemon > /dev/null ; then
|
|
|
|
apt-get install -y -qq 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-04-04 12:24:21 -04:00
|
|
|
if ! which forgejo-runner > /dev/null; then
|
2023-10-04 11:01:08 -04:00
|
|
|
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
|
2023-04-04 12:24:21 -04:00
|
|
|
chmod 755 /bin/forgejo-runner
|
|
|
|
fi
|
2023-03-24 17:42:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function register() {
|
|
|
|
local forgejo="$1"
|
2023-10-12 09:47:32 -04:00
|
|
|
mktemp -d > forgejo-runner-home
|
2023-04-30 03:21:22 -04:00
|
|
|
docker exec --user 1000 forgejo forgejo actions generate-runner-token > forgejo-runner-token
|
2023-09-29 13:17:38 -04:00
|
|
|
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
|
2023-03-24 17:42:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function run() {
|
2023-04-04 12:24:21 -04:00
|
|
|
rm -f forgejo-runner.log
|
2023-10-04 13:20:35 -04:00
|
|
|
daemon --chdir=$(pwd) --unsafe \
|
2023-10-12 09:47:32 -04:00
|
|
|
--env="TERM=$TERM" --env="HOME=$(cat forgejo-runner-home)" --env="PATH=$PATH" --env="USER=$USER" --env="TERM=dumb" --env="USERNAME=$USERNAME" --env="LANG=$LANG" \
|
2023-10-04 13:20:35 -04:00
|
|
|
--pidfile=$(pwd)/forgejo-runner-pid --errlog=$(pwd)/forgejo-runner.log --output=$(pwd)/forgejo-runner.log -- \
|
|
|
|
forgejo-runner --config $FORGEJO_RUNNER_CONFIG daemon
|
2023-03-27 13:39:19 -04:00
|
|
|
sleep 1
|
|
|
|
cat forgejo-runner.log
|
2023-03-24 17:42:52 -04:00
|
|
|
}
|
|
|
|
|
2023-07-08 08:06:13 -04:00
|
|
|
function reload() {
|
|
|
|
teardown
|
2023-10-04 13:20:35 -04:00
|
|
|
rm -f forgejo-runner.log
|
2023-07-08 08:06:13 -04:00
|
|
|
run
|
|
|
|
}
|
|
|
|
|
2023-03-24 17:42:52 -04:00
|
|
|
function setup() {
|
2023-04-04 06:51:16 -04:00
|
|
|
local runner_repository="${1:-https://code.forgejo.org/forgejo/runner}"
|
2023-10-04 10:54:14 -04:00
|
|
|
local version="${2:-v3.0.1}"
|
2023-03-27 13:39:19 -04:00
|
|
|
local forgejo="${3:-http://$(cat forgejo-ip):3000/}"
|
2023-03-24 17:42:52 -04:00
|
|
|
|
|
|
|
dependencies
|
2023-04-04 06:51:16 -04:00
|
|
|
download $runner_repository $version
|
2023-03-24 17:42:52 -04:00
|
|
|
register $forgejo
|
|
|
|
run
|
|
|
|
}
|
|
|
|
|
2023-10-04 13:20:35 -04:00
|
|
|
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
|
2023-03-24 17:42:52 -04:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2023-10-04 13:20:35 -04:00
|
|
|
function teardown() {
|
|
|
|
stop_daemon forgejo-runner
|
2023-10-12 09:47:32 -04:00
|
|
|
rm -fr $(cat forgejo-runner-home)
|
2023-10-04 13:20:35 -04:00
|
|
|
}
|
|
|
|
|
2023-03-24 17:42:52 -04:00
|
|
|
"$@"
|