setup-forgejo/testdata/run.sh

95 lines
2 KiB
Bash
Raw Normal View History

2023-03-25 09:34:41 -04:00
#!/bin/bash
set -ex
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 180); do
if check_status "$forgejo" "$repo" "$sha"; then
break
fi
sleep 1
done
2023-03-25 12:36:27 -04:00
if ! test "$(check_status "$forgejo" "$repo" "$sha")" = "success" ; then
cat forgejo-runner.log
return 1
fi
2023-03-25 09:34:41 -04:00
}
function push() {
local forgejo="$1"
local owner="$2"
local workflow="$3"
2023-03-25 11:06:50 -04:00
local dir="$DIR/$workflow"
mkdir -p $dir/.forgejo/workflows
2023-03-25 11:57:11 -04:00
sed -e "s|SELF|$forgejo/$owner|" \
< $DATA/$workflow.yml > $dir/.forgejo/workflows/$workflow.yml
2023-03-25 09:34:41 -04:00
(
2023-03-25 11:06:50 -04:00
cd $dir
2023-03-25 09:34:41 -04:00
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
)
}
2023-03-25 11:06:50 -04:00
function workflow() {
2023-03-25 09:34:41 -04:00
local forgejo="${1:-http://root:admin1234@$(forgejo-ip):3000}"
local owner="${2:-root}"
local workflow="${3:-demo}"
push "$forgejo" "$owner" "$workflow"
2023-03-25 11:06:50 -04:00
wait_success "$forgejo" "$owner/$workflow" $(cat $DIR/$workflow/SHA)
}
2023-03-25 11:20:38 -04:00
function push_self() {
2023-03-25 11:06:50 -04:00
local forgejo="$1"
local owner="$2"
local dir="$DIR/self"
2023-03-25 11:20:38 -04:00
git clone . $dir
(
cd $dir
rm -fr .forgejo .git
git init
git checkout -b main
2023-03-25 11:20:38 -04:00
git remote add origin $forgejo/$owner/setup-forgejo
git config user.email root@example.com
git config user.name username
2023-03-25 13:35:31 -04:00
git add .
git commit -m 'initial commit'
git push --force origin main
2023-03-25 12:09:01 -04:00
git tag --force vTest HEAD
git push --force origin vTest
2023-03-25 11:20:38 -04:00
)
2023-03-25 09:34:41 -04:00
}
"$@"