From 9d5363f5f7ae45b16afecc11705358430f8a0cff Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Sun, 17 Dec 2023 15:13:06 +0100 Subject: [PATCH 1/8] upgrade tests to use node:20-bookworm instead of node:16-buster --- forgejo-runner.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/forgejo-runner.sh b/forgejo-runner.sh index ade2d1f..d46856c 100755 --- a/forgejo-runner.sh +++ b/forgejo-runner.sh @@ -32,7 +32,7 @@ function register() { local forgejo="$1" mktemp -d > forgejo-runner-home docker exec --user 1000 forgejo forgejo actions generate-runner-token > 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:16-bullseye,ubuntu-latest:docker://code.forgejo.org/oci/node:16-buster,self-hosted:lxc://debian:bullseye + 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 } function run() { From 03cebf0f41e5ee67ff7cc10a3d7333290f6e9827 Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Sun, 17 Dec 2023 22:18:19 +0100 Subject: [PATCH 2/8] forgejo-runner-lib.sh -> forgejo-lib.sh take stop_daemon from forgejo-runner.sh take retry from forgejo.sh --- forgejo-runner-lib.sh => forgejo-lib.sh | 41 ++++++++++++++++++++++++- forgejo-runner.sh | 18 ----------- forgejo.sh | 19 ------------ 3 files changed, 40 insertions(+), 38 deletions(-) rename forgejo-runner-lib.sh => forgejo-lib.sh (50%) diff --git a/forgejo-runner-lib.sh b/forgejo-lib.sh similarity index 50% rename from forgejo-runner-lib.sh rename to forgejo-lib.sh index 56bfc6d..7269a4c 100644 --- a/forgejo-runner-lib.sh +++ b/forgejo-lib.sh @@ -1,10 +1,49 @@ #!/bin/bash # SPDX-License-Identifier: MIT -set -ex +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 diff --git a/forgejo-runner.sh b/forgejo-runner.sh index d46856c..167e38c 100755 --- a/forgejo-runner.sh +++ b/forgejo-runner.sh @@ -69,24 +69,6 @@ function setup() { run } -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 - fi -} - function teardown() { stop_daemon forgejo-runner rm -fr $(cat forgejo-runner-home) diff --git a/forgejo.sh b/forgejo.sh index a54d90f..844ef1f 100755 --- a/forgejo.sh +++ b/forgejo.sh @@ -6,25 +6,6 @@ set -e DIR=/tmp if ${VERBOSE:-false}; then set -x; fi : ${CONTAINER:=forgejo} -: ${RETRY_DELAYS:=1 1 5 5 15 30} - -function retry() { - rm -f $DIR/retry.out - success=false - for delay in $RETRY_DELAYS ; do - if "$@" >> $DIR/retry.out 2>&1 ; then - success=true - break - fi - cat $DIR/retry.out - echo waiting $delay - sleep $delay - done - if test $success = false ; then - cat $DIR/retry.out - return 1 - fi -} function run() { local image="$1" From 02ca9d26bff15fb2da8ae8913789d1cbc7680ee3 Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Sun, 17 Dec 2023 22:19:32 +0100 Subject: [PATCH 3/8] .forgejo/workflows/forgejo.sh is deadcode --- .forgejo/workflows/forgejo.sh | 28 ---------------------------- 1 file changed, 28 deletions(-) delete mode 100644 .forgejo/workflows/forgejo.sh diff --git a/.forgejo/workflows/forgejo.sh b/.forgejo/workflows/forgejo.sh deleted file mode 100644 index 38b6106..0000000 --- a/.forgejo/workflows/forgejo.sh +++ /dev/null @@ -1,28 +0,0 @@ -on: - push: - paths: - - forgejo.sh - - .forgejo/workflows/forgejo-sh.yml - pull_request: - paths: - - forgejo.sh - - .forgejo/workflows/forgejo-sh.yml - -jobs: - forgejo-sh: - runs-on: self-hosted - steps: - - uses: actions/checkout@v3 - - run: | - set -x - export PATH=$(pwd):$PATH - forgejo-dependencies.sh install_docker - ( - echo codeberg.org/forgejo/forgejo 1.19.4-0 - echo codeberg.org/forgejo/forgejo 1.20.5-0 - echo codeberg.org/forgejo-experimental/forgejo 1.21.0-7-rc2 - ) | while read url version ; do - echo "=========================== launching forgejo v$version ==========" - forgejo.sh setup root admin1234 $url $version - forgejo.sh teardown - done From 0dee75531706c1a7883c3d15b8e9b308e5864531 Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Sun, 17 Dec 2023 22:21:10 +0100 Subject: [PATCH 4/8] 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 --- forgejo-runner.sh | 30 +++++++++++++++++------------- forgejo-test-helper.sh | 18 ++++++++++-------- forgejo.sh | 25 ++++++++++++++++--------- 3 files changed, 43 insertions(+), 30 deletions(-) diff --git a/forgejo-runner.sh b/forgejo-runner.sh index 167e38c..12a820e 100755 --- a/forgejo-runner.sh +++ b/forgejo-runner.sh @@ -1,13 +1,14 @@ #!/bin/bash # SPDX-License-Identifier: MIT -set -ex +set -e PS4='${BASH_SOURCE[0]}:$LINENO: ${FUNCNAME[0]}: ' 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} +: ${DIR:=$(pwd)} function dependencies() { if ! which make curl daemon > /dev/null ; then @@ -30,24 +31,27 @@ function download() { function register() { local forgejo="$1" - mktemp -d > forgejo-runner-home - docker exec --user 1000 forgejo forgejo actions generate-runner-token > 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 + mktemp -d > $DIR/forgejo-runner-home + $DIR/forgejocli actions generate-runner-token | tee $DIR/forgejo-runner-token + ( + 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() { - rm -f forgejo-runner.log - daemon --chdir=$(pwd) --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" \ - --pidfile=$(pwd)/forgejo-runner-pid --errlog=$(pwd)/forgejo-runner.log --output=$(pwd)/forgejo-runner.log -- \ + 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 -- \ forgejo-runner --config $FORGEJO_RUNNER_CONFIG daemon sleep 1 - cat forgejo-runner.log + cat $DIR/forgejo-runner.log } function reload() { teardown - rm -f forgejo-runner.log + rm -f $DIR/forgejo-runner.log run } @@ -56,7 +60,7 @@ function setup() { local runner_repository="${1:-${default_runner_repository}}" local default_version="$(cat $SELF_DIR/RUNNER_VERSION)" local version="${2:-${default_version}}" - local forgejo="${3:-http://$(cat forgejo-ip):3000/}" + local forgejo="${3:-http://$(cat $DIR/forgejo-ip):3000/}" dependencies if [[ "$version" =~ ^@ ]] ; then @@ -71,7 +75,7 @@ function setup() { function teardown() { stop_daemon forgejo-runner - rm -fr $(cat forgejo-runner-home) + rm -fr $(cat $DIR/forgejo-runner-home) } "$@" diff --git a/forgejo-test-helper.sh b/forgejo-test-helper.sh index 7283edf..85da371 100755 --- a/forgejo-test-helper.sh +++ b/forgejo-test-helper.sh @@ -4,13 +4,15 @@ set -ex SELF_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -source $SELF_DIR/forgejo-runner-lib.sh +source $SELF_DIR/forgejo-lib.sh : ${LOOPS:=80} : ${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() { local url="$1" @@ -89,7 +91,7 @@ function push() { local self_action="$5" local token="$6" - local dir="$DIR/$project" + local dir="$WORKDIR/$project" rsync -a --exclude .git $directory/ $dir/ local workflows=$dir/.forgejo/workflows/*.yml @@ -139,12 +141,12 @@ function run_workflow() { local token="$6" 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() { - if test -f forgejo-runner-home ; then - rm -fr $(cat forgejo-runner-home)/.cache/act + if test -f $DIR/forgejo-runner-home ; then + rm -fr $(cat $DIR/forgejo-runner-home)/.cache/act fi } @@ -156,7 +158,7 @@ function push_self_action() { clear_runner_cache - local dir="$DIR/self" + local dir="$WORKDIR/self" git diff --exit-code git clone . $dir ( diff --git a/forgejo.sh b/forgejo.sh index 844ef1f..b2bf535 100755 --- a/forgejo.sh +++ b/forgejo.sh @@ -3,9 +3,12 @@ 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 : ${CONTAINER:=forgejo} +: ${DIR:=$(pwd)} function run() { 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" \ -d $image:$version 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 restart ${CONTAINER} } @@ -43,21 +49,22 @@ function setup() { run $image $version 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 - docker exec --user 1000 ${CONTAINER} forgejo admin user change-password --username "$user" --password "$password" + if $DIR/forgejocli admin user list --admin | grep "$user" ; then + $DIR/forgejocli admin user change-password --username "$user" --password "$password" 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 # # 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 - retry forgejo-curl.sh --user "$user" --password "$password" --token @forgejo-token login http://$(cat forgejo-ip):3000 + echo "http://${user}:${password}@$(cat $DIR/forgejo-ip):3000" > $DIR/forgejo-auth-url + $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 # - ( echo -n 'Authorization: token ' ; cat forgejo-token ) > forgejo-header - ( echo "#!/bin/sh" ; echo 'curl -sS -H "Content-Type: application/json" -H @'$(pwd)/forgejo-header' "$@"' ) > forgejo-api && chmod +x forgejo-api + ( echo -n 'Authorization: token ' ; cat $DIR/forgejo-token ) > $DIR/forgejo-header + ( 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() { From c18ce57fad230d0de4ed7857de0d6a88ae0b9829 Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Sun, 17 Dec 2023 22:22:55 +0100 Subject: [PATCH 5/8] forgejo-binary runs forgejo from binary instead of OCI --- .forgejo/workflows/integration.yml | 36 +++++++++ README.md | 9 ++- action.yml | 23 +++++- forgejo-binary-app.ini | 39 ++++++++++ forgejo-binary.sh | 121 +++++++++++++++++++++++++++++ 5 files changed, 223 insertions(+), 5 deletions(-) create mode 100644 forgejo-binary-app.ini create mode 100755 forgejo-binary.sh diff --git a/.forgejo/workflows/integration.yml b/.forgejo/workflows/integration.yml index 1558dc2..d2768ad 100644 --- a/.forgejo/workflows/integration.yml +++ b/.forgejo/workflows/integration.yml @@ -31,3 +31,39 @@ jobs: forgejo-test-helper.sh run_workflow testdata/demo $url root demo setup-forgejo $token echo "============================ IPv6 ===================" forgejo-test-helper.sh run_workflow testdata/ipv6 $url root ipv6 setup-forgejo $token + + binary: + runs-on: self-hosted + strategy: + matrix: + - binary: https://codeberg.org/forgejo/forgejo/releases/download/v1.21.2-1/forgejo-1.21.2-1-linux-amd64 + steps: + - uses: actions/checkout@v4 + - shell: bash + run: | + set -x + export DIR=$(mktemp -d) + export PATH=$(pwd):$PATH + LXC_IP_PREFIX=10.0.10 forgejo-dependencies.sh + + forgejo-binary.sh ensure_user forgejo + chown forgejo $DIR /usr/local/bin + su -c "DIR=$DIR bash -x forgejo-binary.sh setup root admin1234 ${{ matrix.binary }}" forgejo + + su -c "DIR=$DIR bash -x forgejo-runner.sh setup" forgejo + export FORGEJO_RUNNER_LOGS=$DIR/forgejo-runner.log + url=$(cat $DIR/forgejo-auth-url) + token=$(cat $DIR/forgejo-token) + + echo "============================ demo ===================" + FORGEJO_RUNNER_DIR=$DIR forgejo-test-helper.sh run_workflow testdata/demo $url root demo setup-forgejo $token + + su -c "DIR=$DIR forgejo-binary.sh teardown" forgejo + + echo "============================ verify zip downloading ===================" + export DEBIAN_FRONTEND=noninteractive + apt -q install -qq -y zip + mv $DIR/forgejo /tmp/forgejo + ( cd /tmp ; zip forgejo.zip forgejo ) + forgejo-binary.sh download file:///tmp/forgejo.zip + cmp --quiet /tmp/forgejo $DIR/forgejo diff --git a/README.md b/README.md index 40b7c3a..a70cb03 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,12 @@ Setup Forgejo and a runner. +If `binary` is specified, Forgejo downloads the binary from the URL and uses +it to start an instance. + +Otherwise, `image` and `image-version` must be specified and Forgejo +runs from the corresponding container image using docker. + The forgejo-test-helper.sh script is available to help test and debug actions. `forgejo=http://root:admin1234@${{ steps.forgejo.outputs.host-port }}` @@ -40,10 +46,11 @@ that [provides a LXC container](https://forgejo.org/docs/next/user/actions/#jobs | --- | --- | --- | --- | | image | Container image | `false` | codeberg.org/forgejo/forgejo | | image-version | Container image version | `false` | 1.20 | +| binary | URL to a Forgejo binary | `false` | | | user | Administrator user name | `false` | root | | password | Administrator password | `false` | admin1234 | | runner | Runner repository | `false` | https://code.forgejo.org/forgejo/runner | -| runner-version | Runner version. If it starts with @ (for instance @featurebranch), the runner will be built from source using the specified branch. | `false` | v3.0.1 | +| runner-version | Runner version. If it starts with @ (for instance @featurebranch), the runner will be built from source using the specified branch. | `false` | v3.3.0 | | container | Name of the container running the Forgejo instance | `false` | forgejo | | lxc-ip-prefix | Class C IP prefix used by LXC | `false` | 10.0.23 | | install-only | Only install Forgejo and the Forgejo runner, do not launch them | `false` | false | diff --git a/action.yml b/action.yml index 2b7df44..44e27e3 100644 --- a/action.yml +++ b/action.yml @@ -4,6 +4,12 @@ author: 'Forgejo authors' description: | Setup Forgejo and a runner. + If `binary` is specified, Forgejo downloads the binary from the URL and uses + it to start an instance. + + Otherwise, `image` and `image-version` must be specified and Forgejo + runs from the corresponding container image using docker. + The forgejo-test-helper.sh script is available to help test and debug actions. `forgejo=http://root:admin1234@${{ steps.forgejo.outputs.host-port }}` @@ -35,6 +41,8 @@ inputs: image-version: description: 'Container image version' default: '1.20' + binary: + description: 'URL to a Forgejo binary' user: description: 'Administrator user name' default: 'root' @@ -88,10 +96,17 @@ runs: echo "install-only is true, do not run Forgejo" exit 0 fi - export CONTAINER=${{ inputs.container }} - forgejo.sh setup ${{ inputs.user }} "${{ inputs.password }}" ${{ inputs.image }} ${{ inputs.image-version }} - forgejo-runner.sh setup ${{ inputs.runner }} ${{ inputs.runner-version }} http://$(cat forgejo-ip):3000/ - echo url="http://$(cat forgejo-ip):3000" >> $GITHUB_OUTPUT + if test "${{ inputs.binary }}"; then + forgejo-binary.sh ensure_user forgejo + chown forgejo $(pwd) /usr/local/bin + su -c "DIR=$(pwd) forgejo-binary.sh setup ${{ inputs.user }} ${{ inputs.password }} ${{ inputs.binary }}" forgejo + su -c "DIR=$(pwd) forgejo-runner.sh setup ${{ inputs.runner }} ${{ inputs.runner-version }} $(cat forgejo-url)" forgejo + else + export CONTAINER=${{ inputs.container }} + forgejo.sh setup ${{ inputs.user }} "${{ inputs.password }}" ${{ inputs.image }} ${{ inputs.image-version }} + forgejo-runner.sh setup ${{ inputs.runner }} ${{ inputs.runner-version }} $(cat forgejo-url) + fi + echo url="$(cat forgejo-url)" >> $GITHUB_OUTPUT echo host-port="$(cat forgejo-ip):3000" >> $GITHUB_OUTPUT echo token=$(cat forgejo-token) >> $GITHUB_OUTPUT echo runner-logs="$(pwd)/forgejo-runner.log" >> $GITHUB_OUTPUT diff --git a/forgejo-binary-app.ini b/forgejo-binary-app.ini new file mode 100644 index 0000000..b2a6922 --- /dev/null +++ b/forgejo-binary-app.ini @@ -0,0 +1,39 @@ +RUN_MODE = prod +WORK_PATH = ${WORK_PATH} + +[server] +APP_DATA_PATH = ${WORK_PATH}/data +HTTP_PORT = 3000 +LFS_START_SERVER = true + +[database] +DB_TYPE = sqlite3 +PATH = ${WORK_PATH}/forgejo.db + +[log] +MODE = file +LEVEL = trace +ROUTER = file + +[log.file] +FILE_NAME = forgejo.log + +[security] +INSTALL_LOCK = true + +[repository] +ENABLE_PUSH_CREATE_USER = true +DEFAULT_PUSH_CREATE_PRIVATE = false +DEFAULT_REPO_UNITS = repo.code,repo.releases,repo.issues,repo.pulls,repo.wiki,repo.projects,repo.packages,repo.actions + +[actions] +ENABLED = true + +[queue] +TYPE = immediate + +[queue.code_indexer] +TYPE = immediate + +[queue.push_update] +TYPE = immediate diff --git a/forgejo-binary.sh b/forgejo-binary.sh new file mode 100755 index 0000000..e667c48 --- /dev/null +++ b/forgejo-binary.sh @@ -0,0 +1,121 @@ +#!/bin/bash +# SPDX-License-Identifier: MIT + +set -e + +SELF_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +source $SELF_DIR/forgejo-lib.sh + +: ${DIR:=/tmp/forgejo-binary} + +if ${VERBOSE:-false}; then set -x; fi + +function maybe_sudo() { + if test $(id -u) != 0 ; then + SUDO=sudo + fi +} + +function ensure_user() { + local user=$1 + + dependencies + adduser --quiet --comment $user --disabled-password $user + adduser $user sudo + adduser $user docker + sed -i -e 's/^%sudo.*/%sudo ALL=(ALL:ALL) NOPASSWD:ALL/' /etc/sudoers +} + +function dependencies() { + maybe_sudo + if ! which sudo curl daemon jq git-lfs ip sqlite3 envsubst unzip > /dev/null ; then + $SUDO apt-get update -qq + $SUDO apt-get -q install -y -qq sudo curl daemon jq git-lfs iproute2 sqlite3 gettext-base unzip + fi +} + +function cleanup_logs() { + local work_path=$DIR/forgejo-work-path + + rm -f $DIR/*.log + rm -f $work_path/log/*.log +} + +function download() { + local url="$1" + + if ! test -f $DIR/forgejo ; then + curl -sS $url > $DIR/forgejo.tmp + if file $DIR/forgejo.tmp | grep --quiet 'Zip archive' ; then + unzip $DIR/forgejo.tmp -d $DIR + rm -f $DIR/forgejo.tmp + else + mv $DIR/forgejo.tmp $DIR/forgejo + fi + chmod +x $DIR/forgejo + fi + echo $DIR/forgejo --version + $DIR/forgejo --version +} + +function create_user() { + local user="$1" + local password="$2" + local work_path=$DIR/forgejo-work-path + + if test -f $work_path/forgejo-token; then + return + fi + + $DIR/forgejocli admin user create --admin --username "$user" --password "$password" --email "$user@example.com" + $DIR/forgejocli admin user generate-access-token -u "$user" --raw --scopes all > $DIR/forgejo-token + export DOT=$DIR + local url="http://$(cat $DIR/forgejo-ip):3000" + echo "$url" > $DIR/forgejo-url + echo "http://${user}:${password}@$(cat $DIR/forgejo-ip):3000" > $DIR/forgejo-auth-url + retry forgejo-curl.sh --user "$user" --password "$password" --token @$DIR/forgejo-token login "$url" + echo forgejo-curl.sh api_json $url/api/forgejo/v1/version + forgejo-curl.sh api_json $url/api/forgejo/v1/version +} + +function start_forgejo() { + local work_path=$DIR/forgejo-work-path + daemon --chdir=$DIR --unsafe --env="TERM=$TERM" --env="HOME=$HOME" --env="PATH=$PATH" --pidfile=$DIR/forgejo-pid --errlog=$DIR/forgejo-err.log --output=$DIR/forgejo-out.log -- $DIR/forgejo --config $work_path/app.ini --work-path $work_path + if ! retry grep 'Starting server on' $work_path/log/forgejo.log ; then + cat $DIR/*.log + cat $work_path/log/*.log + return 1 + fi + echo "$DIR/forgejo --config $work_path/app.ini --work-path $work_path" '"$@"' > $DIR/forgejocli + chmod +x $DIR/forgejocli + hostname -I | cut -f1 -d' ' > $DIR/forgejo-ip +} + +function reset_forgejo() { + local config=$1 + local work_path=$DIR/forgejo-work-path + rm -fr $work_path + mkdir -p $work_path + WORK_PATH=$work_path envsubst < $SELF_DIR/$config-app.ini > $work_path/app.ini +} + +function teardown() { + stop_daemon forgejo $DIR + cleanup_logs +} + +function setup() { + local user="${1:-root}" + local password="${2:-admin1234}" + local url="${3:-https://codeberg.org/forgejo/forgejo/releases/download/v1.21.2-1/forgejo-1.21.2-1-linux-amd64}" + + dependencies + download "$url" + reset_forgejo forgejo-binary + start_forgejo + create_user "$user" "$password" +} + +mkdir -p $DIR + +"$@" From b2ce91a59385d1977cc37678dbeb54eeeaf8197d Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Sun, 17 Dec 2023 22:34:58 +0100 Subject: [PATCH 6/8] use alpine image instead of the heavier default for testing --- testdata/demo/.forgejo/workflows/test.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/testdata/demo/.forgejo/workflows/test.yml b/testdata/demo/.forgejo/workflows/test.yml index 11654b9..96320f7 100644 --- a/testdata/demo/.forgejo/workflows/test.yml +++ b/testdata/demo/.forgejo/workflows/test.yml @@ -2,8 +2,8 @@ on: [push] jobs: ls: runs-on: docker + container: + image: code.forgejo.org/oci/alpine:3.19 steps: - - uses: actions/checkout@v3 - run: | - ls ${{ github.workspace }} - + echo ${{ github.workspace }} From 6b431ecf9bacbda6cf67659085bd0f20cf8b17d3 Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Sun, 17 Dec 2023 23:32:55 +0100 Subject: [PATCH 7/8] add nested test for action.yml binary support --- .forgejo/workflows/integration-action.yml | 33 +++++++++++++++++++ .forgejo/workflows/integration-nested.yml | 22 ------------- .../.forgejo/workflows/test.yml | 26 +++++++++++++++ .../.forgejo/workflows/test.yml | 0 4 files changed, 59 insertions(+), 22 deletions(-) create mode 100644 .forgejo/workflows/integration-action.yml delete mode 100644 .forgejo/workflows/integration-nested.yml create mode 100644 testdata/lxc-run-forgejo-binary/.forgejo/workflows/test.yml rename testdata/{sanity-checks => lxc-run-forgejo-oci}/.forgejo/workflows/test.yml (100%) diff --git a/.forgejo/workflows/integration-action.yml b/.forgejo/workflows/integration-action.yml new file mode 100644 index 0000000..1e68912 --- /dev/null +++ b/.forgejo/workflows/integration-action.yml @@ -0,0 +1,33 @@ +on: + push: + branches: + - 'main' + - 'v*' + pull_request: + +# +# Testing that action.yml works as intended, the other tests do not do hat. +# +jobs: + integration-action: + runs-on: self-hosted + steps: + - uses: actions/checkout@v3 + - name: forgejo.sh & forgejo-runner.sh + run: | + LXC_IP_PREFIX=10.0.9 ./forgejo-dependencies.sh + export PATH=$(pwd):$PATH + forgejo.sh setup root admin1234 codeberg.org/forgejo/forgejo 1.20 + forgejo-runner.sh setup + echo "============================ upload setup-forgejo ===================" + forgejo-test-helper.sh push_self_action $(cat forgejo-auth-url) root setup-forgejo vTest + - name: lxc-run-forgejo-binary + run: | + export PATH=$(pwd):$PATH + export FORGEJO_RUNNER_LOGS=forgejo-runner.log + forgejo-test-helper.sh run_workflow testdata/lxc-run-forgejo-binary $(cat forgejo-auth-url) root lxc-run-forgejo-binary setup-forgejo $(cat forgejo-token) + - name: lxc-run-forgejo-oci + run: | + export PATH=$(pwd):$PATH + export FORGEJO_RUNNER_LOGS=forgejo-runner.log + forgejo-test-helper.sh run_workflow testdata/lxc-run-forgejo-oci $(cat forgejo-auth-url) root lxc-run-forgejo-oci setup-forgejo $(cat forgejo-token) diff --git a/.forgejo/workflows/integration-nested.yml b/.forgejo/workflows/integration-nested.yml deleted file mode 100644 index 0ede967..0000000 --- a/.forgejo/workflows/integration-nested.yml +++ /dev/null @@ -1,22 +0,0 @@ -on: - push: - branches: - - 'main' - - 'v*' - pull_request: - -jobs: - integration-nested: - runs-on: self-hosted - steps: - - uses: actions/checkout@v3 - - run: | - set -x - LXC_IP_PREFIX=10.0.9 ./forgejo-dependencies.sh - export PATH=$(pwd):$PATH - forgejo.sh setup root admin1234 codeberg.org/forgejo/forgejo 1.20 - forgejo-runner.sh setup - export FORGEJO_RUNNER_LOGS=forgejo-runner.log - echo "============================ sanity-check ===================" - forgejo-test-helper.sh push_self_action http://root:admin1234@$(cat forgejo-ip):3000 root setup-forgejo vTest - forgejo-test-helper.sh run_workflow testdata/sanity-checks http://root:admin1234@$(cat forgejo-ip):3000 root sanity-check setup-forgejo $(cat forgejo-token) diff --git a/testdata/lxc-run-forgejo-binary/.forgejo/workflows/test.yml b/testdata/lxc-run-forgejo-binary/.forgejo/workflows/test.yml new file mode 100644 index 0000000..08bfdf5 --- /dev/null +++ b/testdata/lxc-run-forgejo-binary/.forgejo/workflows/test.yml @@ -0,0 +1,26 @@ +# SPDX-License-Identifier: MIT +on: [push] +jobs: + setup-binary: + runs-on: self-hosted + steps: + - run: | + echo +++++++++++++++++++++++++++++++++++++++++++ + echo about to actions/checkout@v3 for SELF@vTest + - uses: actions/checkout@v3 + - id: forgejo + uses: SELF@vTest + with: + binary: https://codeberg.org/forgejo/forgejo/releases/download/v1.20.6-1/forgejo-1.20.6-1-linux-amd64 + - run: | + echo +++++++++++++++++++++++++++++++++++++++++++ + echo sanity checking the reset of setup-forgejo + set -ex + test "${{ steps.forgejo.outputs.token }}" + test "FORGEJO_TOKEN" -a "FORGEJO_TOKEN" != F'O'RGEJO_TOKEN + curl ${{ steps.forgejo.outputs.url }}/api/forgejo/v1/version >& version.out + if ! grep --quiet 1.20 version.out ; then + cat version.out + exit 1 + fi + test -f ${{ steps.forgejo.outputs.runner-file }} diff --git a/testdata/sanity-checks/.forgejo/workflows/test.yml b/testdata/lxc-run-forgejo-oci/.forgejo/workflows/test.yml similarity index 100% rename from testdata/sanity-checks/.forgejo/workflows/test.yml rename to testdata/lxc-run-forgejo-oci/.forgejo/workflows/test.yml From 6b89f49230225e070e5fab32a5513c7fde6dbdb0 Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Sun, 17 Dec 2023 23:35:54 +0100 Subject: [PATCH 8/8] also test v1.20 binary --- .forgejo/workflows/{integration.yml => integration-scripts.yml} | 1 + 1 file changed, 1 insertion(+) rename .forgejo/workflows/{integration.yml => integration-scripts.yml} (95%) diff --git a/.forgejo/workflows/integration.yml b/.forgejo/workflows/integration-scripts.yml similarity index 95% rename from .forgejo/workflows/integration.yml rename to .forgejo/workflows/integration-scripts.yml index d2768ad..876fbfe 100644 --- a/.forgejo/workflows/integration.yml +++ b/.forgejo/workflows/integration-scripts.yml @@ -37,6 +37,7 @@ jobs: strategy: matrix: - binary: https://codeberg.org/forgejo/forgejo/releases/download/v1.21.2-1/forgejo-1.21.2-1-linux-amd64 + - binary: https://codeberg.org/forgejo/forgejo/releases/download/v1.20.6-1/forgejo-1.20.6-1-linux-amd64 steps: - uses: actions/checkout@v4 - shell: bash