1
0
Fork 0
mirror of https://code.forgejo.org/actions/setup-forgejo synced 2024-09-19 01:46:16 +00:00

refactor and publish forgejo-test-helper.sh

This commit is contained in:
Earl Warren 2023-03-29 15:23:09 +02:00
parent 202709a555
commit 5e74823706
No known key found for this signature in database
GPG key ID: 0579CB2928A78A00
8 changed files with 156 additions and 121 deletions

View file

@ -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

View file

@ -3,7 +3,25 @@
<!-- action-docs-description -->
## 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.
<!-- action-docs-description -->
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 |
<!-- action-docs-inputs -->

View file

@ -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'

101
forgejo-test-helper.sh Executable file
View file

@ -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
)
}
"$@"

18
testdata/demo.yml vendored
View file

@ -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 }}."

View file

@ -0,0 +1,9 @@
on: [push]
jobs:
ls:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: |
ls ${{ github.workspace }}

96
testdata/run.sh vendored
View file

@ -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
)
}
"$@"

View file

@ -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: |