setup-forgejo/forgejo-lib.sh

83 lines
1.7 KiB
Bash

#!/bin/bash
# SPDX-License-Identifier: MIT
set -e
export DEBIAN_FRONTEND=noninteractive
: ${RETRY_DELAYS:=1 1 5 5 15 30}
function retry() {
local tmp=/tmp
rm -f $tmp/retry.out
success=false
for delay in $RETRY_DELAYS ; do
if "$@" >> $tmp/retry.out 2>&1 ; then
success=true
break
fi
cat $tmp/retry.out
echo waiting $delay
sleep $delay
done
if test $success = false ; then
cat $tmp/retry.out
return 1
fi
}
function stop_daemon() {
local daemon=$1
local dir=${2:-.}
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 dependency_go() {
go_version="1.21.4.linux-amd64" # Set the desired Go version here
if ! which wget tar > /dev/null ; then
apt-get install -y -qq wget tar
fi
if ! which go > /dev/null ; then
wget --quiet "https://go.dev/dl/go$go_version.tar.gz"
tar zxf "go$go_version.tar.gz"
export PATH="$(pwd)/go/bin:$PATH"
fi
}
function checkout() {
local git="$1"
git clone $git forgejo-runner
}
function build_runner() {
local git="$1"
local version="${2:-main}"
local dir="$3"
local tmp=$(mktemp -d)
(
cd $tmp
checkout "$git"
dependency_go
cd forgejo-runner
git checkout "$version"
make build
)
mv $tmp/forgejo-runner/forgejo-runner $dir
$dir/forgejo-runner --version
rm -fr "$tmp"
}