ensure all files are created in a designated directory

for backward compatibility DIR defaults to $(pwd)

also create the additional convenience files:

* forgejocli
* forgejo-url
* forgejo-auth-url which includes the user and password
This commit is contained in:
Earl Warren 2023-12-17 22:21:10 +01:00
parent 02ca9d26bf
commit 0dee755317
No known key found for this signature in database
GPG key ID: 0579CB2928A78A00
3 changed files with 43 additions and 30 deletions

View file

@ -1,13 +1,14 @@
#!/bin/bash #!/bin/bash
# SPDX-License-Identifier: MIT # SPDX-License-Identifier: MIT
set -ex set -e
PS4='${BASH_SOURCE[0]}:$LINENO: ${FUNCNAME[0]}: ' PS4='${BASH_SOURCE[0]}:$LINENO: ${FUNCNAME[0]}: '
SELF_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" SELF_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source $SELF_DIR/forgejo-runner-lib.sh source $SELF_DIR/forgejo-lib.sh
: ${FORGEJO_RUNNER_CONFIG:=$SELF_DIR/runner-config.yaml} : ${FORGEJO_RUNNER_CONFIG:=$SELF_DIR/runner-config.yaml}
: ${DIR:=$(pwd)}
function dependencies() { function dependencies() {
if ! which make curl daemon > /dev/null ; then if ! which make curl daemon > /dev/null ; then
@ -30,24 +31,27 @@ function download() {
function register() { function register() {
local forgejo="$1" local forgejo="$1"
mktemp -d > forgejo-runner-home mktemp -d > $DIR/forgejo-runner-home
docker exec --user 1000 forgejo forgejo actions generate-runner-token > forgejo-runner-token $DIR/forgejocli actions generate-runner-token | tee $DIR/forgejo-runner-token
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:20-bookworm,ubuntu-latest:docker://code.forgejo.org/oci/node:20-buster,self-hosted:lxc://debian:bookworm (
cd $DIR
timeout --signal=KILL 30 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
)
} }
function run() { function run() {
rm -f forgejo-runner.log rm -f $DIR/forgejo-runner.log
daemon --chdir=$(pwd) --unsafe \ daemon --chdir=$DIR --unsafe \
--env="TERM=$TERM" --env="HOME=$(cat forgejo-runner-home)" --env="PATH=$PATH" --env="USER=$USER" --env="TERM=dumb" --env="USERNAME=$USERNAME" --env="LANG=$LANG" \ --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=$(pwd)/forgejo-runner-pid --errlog=$(pwd)/forgejo-runner.log --output=$(pwd)/forgejo-runner.log -- \ --pidfile=$DIR/forgejo-runner-pid --errlog=$DIR/forgejo-runner.log --output=$DIR/forgejo-runner.log -- \
forgejo-runner --config $FORGEJO_RUNNER_CONFIG daemon forgejo-runner --config $FORGEJO_RUNNER_CONFIG daemon
sleep 1 sleep 1
cat forgejo-runner.log cat $DIR/forgejo-runner.log
} }
function reload() { function reload() {
teardown teardown
rm -f forgejo-runner.log rm -f $DIR/forgejo-runner.log
run run
} }
@ -56,7 +60,7 @@ function setup() {
local runner_repository="${1:-${default_runner_repository}}" local runner_repository="${1:-${default_runner_repository}}"
local default_version="$(cat $SELF_DIR/RUNNER_VERSION)" local default_version="$(cat $SELF_DIR/RUNNER_VERSION)"
local version="${2:-${default_version}}" local version="${2:-${default_version}}"
local forgejo="${3:-http://$(cat forgejo-ip):3000/}" local forgejo="${3:-http://$(cat $DIR/forgejo-ip):3000/}"
dependencies dependencies
if [[ "$version" =~ ^@ ]] ; then if [[ "$version" =~ ^@ ]] ; then
@ -71,7 +75,7 @@ function setup() {
function teardown() { function teardown() {
stop_daemon forgejo-runner stop_daemon forgejo-runner
rm -fr $(cat forgejo-runner-home) rm -fr $(cat $DIR/forgejo-runner-home)
} }
"$@" "$@"

View file

@ -4,13 +4,15 @@
set -ex set -ex
SELF_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" SELF_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source $SELF_DIR/forgejo-runner-lib.sh source $SELF_DIR/forgejo-lib.sh
: ${LOOPS:=80} : ${LOOPS:=80}
: ${LOOP_DELAY:=5} : ${LOOP_DELAY:=5}
DIR=$(mktemp -d) WORKDIR=$(mktemp -d)
: ${DIR:=$(pwd)}
export DOT=$DIR
trap "rm -fr $DIR" EXIT trap "rm -fr $WORKDIR" EXIT
function branch_tip() { function branch_tip() {
local url="$1" local url="$1"
@ -89,7 +91,7 @@ function push() {
local self_action="$5" local self_action="$5"
local token="$6" local token="$6"
local dir="$DIR/$project" local dir="$WORKDIR/$project"
rsync -a --exclude .git $directory/ $dir/ rsync -a --exclude .git $directory/ $dir/
local workflows=$dir/.forgejo/workflows/*.yml local workflows=$dir/.forgejo/workflows/*.yml
@ -139,12 +141,12 @@ function run_workflow() {
local token="$6" local token="$6"
push_workflow "$directory" "$url" "$owner" "$project" "$self_action" "$token" push_workflow "$directory" "$url" "$owner" "$project" "$self_action" "$token"
wait_success "$url" "$owner/$project" $(cat $DIR/$project/SHA) wait_success "$url" "$owner/$project" $(cat $WORKDIR/$project/SHA)
} }
function clear_runner_cache() { function clear_runner_cache() {
if test -f forgejo-runner-home ; then if test -f $DIR/forgejo-runner-home ; then
rm -fr $(cat forgejo-runner-home)/.cache/act rm -fr $(cat $DIR/forgejo-runner-home)/.cache/act
fi fi
} }
@ -156,7 +158,7 @@ function push_self_action() {
clear_runner_cache clear_runner_cache
local dir="$DIR/self" local dir="$WORKDIR/self"
git diff --exit-code git diff --exit-code
git clone . $dir git clone . $dir
( (

View file

@ -3,9 +3,12 @@
set -e set -e
DIR=/tmp SELF_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source $SELF_DIR/forgejo-lib.sh
if ${VERBOSE:-false}; then set -x; fi if ${VERBOSE:-false}; then set -x; fi
: ${CONTAINER:=forgejo} : ${CONTAINER:=forgejo}
: ${DIR:=$(pwd)}
function run() { function run() {
local image="$1" local image="$1"
@ -29,7 +32,10 @@ function run() {
-e "FORGEJO__repository__DEFAULT_REPO_UNITS=repo.code,repo.releases,repo.issues,repo.pulls,repo.wiki,repo.projects,repo.packages,$actions_unit" \ -e "FORGEJO__repository__DEFAULT_REPO_UNITS=repo.code,repo.releases,repo.issues,repo.pulls,repo.wiki,repo.projects,repo.packages,$actions_unit" \
-d $image:$version -d $image:$version
local ip="$(docker inspect -f "{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}" ${CONTAINER})" local ip="$(docker inspect -f "{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}" ${CONTAINER})"
echo $ip > forgejo-ip echo 'docker exec --user 1000 '${CONTAINER}' forgejo "$@"' > $DIR/forgejocli
chmod +x $DIR/forgejocli
echo $ip > $DIR/forgejo-ip
echo "http://$(cat $DIR/forgejo-ip):3000" > $DIR/forgejo-url
docker exec ${CONTAINER} sed -i -e "s|localhost|$ip|" /data/gitea/conf/app.ini docker exec ${CONTAINER} sed -i -e "s|localhost|$ip|" /data/gitea/conf/app.ini
docker restart ${CONTAINER} docker restart ${CONTAINER}
} }
@ -43,21 +49,22 @@ function setup() {
run $image $version run $image $version
sleep 5 # for some reason trying to run "forgejo admin" while forgejo is booting will permanently break everything sleep 5 # for some reason trying to run "forgejo admin" while forgejo is booting will permanently break everything
if docker exec --user 1000 ${CONTAINER} forgejo admin user list --admin | grep "$user" ; then if $DIR/forgejocli admin user list --admin | grep "$user" ; then
docker exec --user 1000 ${CONTAINER} forgejo admin user change-password --username "$user" --password "$password" $DIR/forgejocli admin user change-password --username "$user" --password "$password"
else else
retry docker exec --user 1000 ${CONTAINER} forgejo admin user create --admin --username "$user" --password "$password" --email "$user@example.com" retry $DIR/forgejocli admin user create --admin --username "$user" --password "$password" --email "$user@example.com"
fi fi
# #
# The 'sudo' scope was removed in Forgejo v1.20 and is ignored # The 'sudo' scope was removed in Forgejo v1.20 and is ignored
# #
docker exec --user 1000 ${CONTAINER} forgejo admin user generate-access-token -u $user --raw --scopes 'all,sudo' > forgejo-token echo "http://${user}:${password}@$(cat $DIR/forgejo-ip):3000" > $DIR/forgejo-auth-url
retry forgejo-curl.sh --user "$user" --password "$password" --token @forgejo-token login http://$(cat forgejo-ip):3000 $DIR/forgejocli admin user generate-access-token -u $user --raw --scopes 'all,sudo' > $DIR/forgejo-token
retry forgejo-curl.sh --user "$user" --password "$password" --token @$DIR/forgejo-token login http://$(cat $DIR/forgejo-ip):3000
# #
# Redundant with forgejo-curl.sh, kept around for backward compatibility 09/2023 # Redundant with forgejo-curl.sh, kept around for backward compatibility 09/2023
# #
( echo -n 'Authorization: token ' ; cat forgejo-token ) > forgejo-header ( echo -n 'Authorization: token ' ; cat $DIR/forgejo-token ) > $DIR/forgejo-header
( echo "#!/bin/sh" ; echo 'curl -sS -H "Content-Type: application/json" -H @'$(pwd)/forgejo-header' "$@"' ) > forgejo-api && chmod +x forgejo-api ( echo "#!/bin/sh" ; echo 'curl -sS -H "Content-Type: application/json" -H @'$DIR/forgejo-header' "$@"' ) > $DIR/forgejo-api && chmod +x $DIR/forgejo-api
} }
function teardown() { function teardown() {