From 5e7482370699dd0b14f3541d1c2ca9319eff366c Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Wed, 29 Mar 2023 15:23:09 +0200 Subject: [PATCH] refactor and publish forgejo-test-helper.sh --- .forgejo/workflows/integration.yml | 7 +- README.md | 22 +++- action.yml | 22 +++- forgejo-test-helper.sh | 101 ++++++++++++++++++ testdata/demo.yml | 18 ---- testdata/demo/.forgejo/workflows/test.yml | 9 ++ testdata/run.sh | 96 ----------------- .../.forgejo/workflows/test.yml} | 2 +- 8 files changed, 156 insertions(+), 121 deletions(-) create mode 100755 forgejo-test-helper.sh delete mode 100644 testdata/demo.yml create mode 100644 testdata/demo/.forgejo/workflows/test.yml delete mode 100755 testdata/run.sh rename testdata/{try-setup-forgejo.yml => sanity-checks/.forgejo/workflows/test.yml} (90%) diff --git a/.forgejo/workflows/integration.yml b/.forgejo/workflows/integration.yml index 71d0d36..8f1f2a3 100644 --- a/.forgejo/workflows/integration.yml +++ b/.forgejo/workflows/integration.yml @@ -9,6 +9,7 @@ jobs: LXC_IP_RANGE=10.0.9 ./dependencies.sh ./forgejo.sh setup root admin1234 codeberg.org/forgejo/forgejo:1.19 ./forgejo-runner.sh setup - # testdata/run.sh workflow http://root:admin1234@$(cat forgejo-ip):3000 root demo - testdata/run.sh push_self http://root:admin1234@$(cat forgejo-ip):3000 root - testdata/run.sh workflow http://root:admin1234@$(cat forgejo-ip):3000 root try-setup-forgejo + export FORGEJO_RUNNER_LOGS=forgejo-runner.log + ./forgejo-test-helper.sh run_workflow testdata/demo http://root:admin1234@$(cat forgejo-ip):3000 root demo setup-forgejo + ./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 diff --git a/README.md b/README.md index f342ed2..d4e6fd6 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,25 @@ ## Description -Setup Forgejo and a runner +Setup Forgejo and a runner. + +The forgejo-test-helper.sh script is available to help test and debug actions. + +forgejo=http://root:admin1234@${{ steps.forgejo.outputs.host-port }} + +* forgejo-test-helper.sh push_self_action $forgejo root myaction vTest + Creates the repository `$forgejo/root/myaction` and populate it with the + content of the repository under test, except for the `.forgejo` directory + (it would otherwise create an infinite recursion loop). The tag `vTest` is + set to the SHA under test. +* forgejo-test-helper.sh run_workflow testrepo $forgejo root testrepo myaction + Create the repository `$forgejo/root/testrepo` and populate it with the + content of the `testrepo` directory. All occurrences of `SELF` in + `testrepo/.forgejo/workflows/*.yml` are replaced with `$forgejo/root/myaction`. + +This combination allows to run Forgejo Actions workflows from +`testrepo` that use the action under test (`myaction`) to verify it +works as intended. It can only be run on the `self-hosted` platform, running on a host with LXC installed. @@ -17,7 +35,7 @@ It can only be run on the `self-hosted` platform, running on a host with LXC ins | image-version | Container image version | `false` | 1.19 | | user | Administrator user name | `false` | root | | password | Administrator password | `false` | admin1234 | -| runner | Runner git repository | `false` | https://code.forgejo.org/fogejo/runner | +| runner | Runner git repository | `false` | https://code.forgejo.org/forgejo/runner | | runner-version | Runner version | `false` | v1.4.1 | diff --git a/action.yml b/action.yml index 46f5a29..dd3b7cd 100644 --- a/action.yml +++ b/action.yml @@ -1,6 +1,26 @@ name: 'Setup Forgejo' author: 'Forgejo authors' -description: 'Setup Forgejo and a runner' +description: | + Setup Forgejo and a runner. + + The forgejo-test-helper.sh script is available to help test and debug actions. + + forgejo=http://root:admin1234@${{ steps.forgejo.outputs.host-port }} + + * forgejo-test-helper.sh push_self_action $forgejo root myaction vTest + Creates the repository `$forgejo/root/myaction` and populate it with the + content of the repository under test, except for the `.forgejo` directory + (it would otherwise create an infinite recursion loop). The tag `vTest` is + set to the SHA under test. + * forgejo-test-helper.sh run_workflow testrepo $forgejo root testrepo myaction + Create the repository `$forgejo/root/testrepo` and populate it with the + content of the `testrepo` directory. All occurrences of `SELF` in + `testrepo/.forgejo/workflows/*.yml` are replaced with `$forgejo/root/myaction`. + + This combination allows to run Forgejo Actions workflows from + `testrepo` that use the action under test (`myaction`) to verify it + works as intended. + inputs: image: description: 'Container image' diff --git a/forgejo-test-helper.sh b/forgejo-test-helper.sh new file mode 100755 index 0000000..d45817e --- /dev/null +++ b/forgejo-test-helper.sh @@ -0,0 +1,101 @@ +#!/bin/bash + +set -ex + +: ${LOOPS:=40} +: ${LOOP_DELAY:=10} +DIR=$(mktemp -d) + +trap "rm -fr $DIR" EXIT + +function check_status() { + local url="$1" + local repo="$2" + local sha="$3" + + if ! which jq > /dev/null ; then + apt-get install -y -qq jq + fi + local state=$(curl --fail -sS "$url/api/v1/repos/$repo/commits/$sha/status" | jq --raw-output .state) + echo $state + test "$state" != "" -a "$state" != "pending" -a "$state" != "running" -a "$state" != "null" +} + +function wait_success() { + local url="$1" + local repo="$2" + local sha="$3" + + for i in $(seq $LOOPS); do + if check_status "$url" "$repo" "$sha"; then + break + fi + test "$FORGEJO_RUNNER_LOGS" && tail $FORGEJO_RUNNER_LOGS + sleep $LOOP_DELAY + done + if ! test "$(check_status "$url" "$repo" "$sha")" = "success" ; then + test "$FORGEJO_RUNNER_LOGS" && cat $FORGEJO_RUNNER_LOGS + return 1 + fi +} + +function push() { + local directory="$1" + local url="$2" + local owner="$3" + local project="$4" + local self_action="$5" + + local dir="$DIR/$project" + rsync -a $directory/ $dir/ + sed -i -e "s|SELF|$url/$owner/$self_action|" $dir/.forgejo/workflows/*.yml + ( + cd $dir + git init + git checkout -b main + git config user.email root@example.com + git config user.name username + git add . + git commit -m 'initial commit' + git remote add origin $url/$owner/$project + git push --force -u origin main + git rev-parse HEAD > SHA + ) +} + +function run_workflow() { + local directory="$1" + local url="$2" + local owner="$3" + local project="$4" + local self_action="$5" + + push "$directory" "$url" "$owner" "$project" "$self_action" + wait_success "$url" "$owner/$project" $(cat $DIR/$project/SHA) +} + +function push_self_action() { + local url="$1" + local owner="$2" + local self_action="$3" + local tag="$4" + + local dir="$DIR/self" + git clone . $dir + ( + cd $dir + rm -fr .forgejo .git + git init + git checkout -b main + git remote add origin "$url/$owner/$self_action" + git config user.email root@example.com + git config user.name username + git add . + git commit -m 'initial commit' + git push --force origin main + git tag --force $tag HEAD + git push --force origin $tag + ) +} + +"$@" diff --git a/testdata/demo.yml b/testdata/demo.yml deleted file mode 100644 index 876684c..0000000 --- a/testdata/demo.yml +++ /dev/null @@ -1,18 +0,0 @@ -name: Demo -run-name: ${{ github.actor }} is testing -on: [push] -jobs: - Explore-CI: - runs-on: ubuntu-latest - steps: - - run: echo "The job was automatically triggered by a ${{ github.event_name }} event." - - run: echo "This job is now running on a ${{ runner.os }} server." - - run: echo "The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}." - - name: Check out repository code - uses: actions/checkout@v3 - - run: echo "The ${{ github.repository }} repository has been cloned to the runner." - - run: echo "The workflow is now ready to test your code on the runner." - - name: List files in the repository - run: | - ls ${{ github.workspace }} - - run: echo "This job's status is ${{ job.status }}." diff --git a/testdata/demo/.forgejo/workflows/test.yml b/testdata/demo/.forgejo/workflows/test.yml new file mode 100644 index 0000000..bd05c51 --- /dev/null +++ b/testdata/demo/.forgejo/workflows/test.yml @@ -0,0 +1,9 @@ +on: [push] +jobs: + ls: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - run: | + ls ${{ github.workspace }} + diff --git a/testdata/run.sh b/testdata/run.sh deleted file mode 100755 index 4b66810..0000000 --- a/testdata/run.sh +++ /dev/null @@ -1,96 +0,0 @@ -#!/bin/bash - -set -ex - -: ${FORGEJO_RUNNER_LOGS:=forgejo-runner.log} -DATA=$(dirname $0) -DIR=$(mktemp -d) - -trap "rm -fr $DIR" EXIT - -function check_status() { - local forgejo="$1" - local repo="$2" - local sha="$3" - - if ! which jq > /dev/null ; then - apt-get install -y -qq jq - fi - local state=$(curl --fail -sS "$forgejo/api/v1/repos/$repo/commits/$sha/status" | jq --raw-output .state) - echo $state - test "$state" != "" -a "$state" != "pending" -a "$state" != "running" -a "$state" != "null" -} - -function wait_success() { - local forgejo="$1" - local repo="$2" - local sha="$3" - - for i in $(seq 40); do - if check_status "$forgejo" "$repo" "$sha"; then - break - fi - tail $FORGEJO_RUNNER_LOGS - sleep 5 - done - if ! test "$(check_status "$forgejo" "$repo" "$sha")" = "success" ; then - cat $FORGEJO_RUNNER_LOGS - return 1 - fi -} - -function push() { - local forgejo="$1" - local owner="$2" - local workflow="$3" - - local dir="$DIR/$workflow" - mkdir -p $dir/.forgejo/workflows - sed -e "s|SELF|$forgejo/$owner|" \ - < $DATA/$workflow.yml > $dir/.forgejo/workflows/$workflow.yml - ( - cd $dir - git init - git checkout -b main - git config user.email root@example.com - git config user.name username - git add . - git commit -m 'initial commit' - git remote add origin $forgejo/$owner/$workflow - git push --force -u origin main - git rev-parse HEAD > SHA - ) -} - -function workflow() { - local forgejo="${1:-http://root:admin1234@$(forgejo-ip):3000}" - local owner="${2:-root}" - local workflow="${3:-demo}" - - push "$forgejo" "$owner" "$workflow" - wait_success "$forgejo" "$owner/$workflow" $(cat $DIR/$workflow/SHA) -} - -function push_self() { - local forgejo="$1" - local owner="$2" - - local dir="$DIR/self" - git clone . $dir - ( - cd $dir - rm -fr .forgejo .git - git init - git checkout -b main - git remote add origin $forgejo/$owner/setup-forgejo - git config user.email root@example.com - git config user.name username - git add . - git commit -m 'initial commit' - git push --force origin main - git tag --force vTest HEAD - git push --force origin vTest - ) -} - -"$@" diff --git a/testdata/try-setup-forgejo.yml b/testdata/sanity-checks/.forgejo/workflows/test.yml similarity index 90% rename from testdata/try-setup-forgejo.yml rename to testdata/sanity-checks/.forgejo/workflows/test.yml index e948e63..f3586e1 100644 --- a/testdata/try-setup-forgejo.yml +++ b/testdata/sanity-checks/.forgejo/workflows/test.yml @@ -7,7 +7,7 @@ jobs: steps: - uses: actions/checkout@v3 - id: forgejo - uses: SELF/setup-forgejo@vTest + uses: SELF@vTest with: image-version: 1.19 - run: |