2023-03-29 09:23:09 -04:00
|
|
|
#!/bin/bash
|
2023-04-01 05:08:06 -04:00
|
|
|
# SPDX-License-Identifier: MIT
|
2023-03-29 09:23:09 -04:00
|
|
|
|
|
|
|
set -ex
|
|
|
|
|
2023-09-26 15:53:30 -04:00
|
|
|
export DEBIAN_FRONTEND=noninteractive
|
|
|
|
|
2023-10-07 15:38:23 -04:00
|
|
|
: ${LOOPS:=80}
|
|
|
|
: ${LOOP_DELAY:=5}
|
2023-03-29 09:23:09 -04:00
|
|
|
DIR=$(mktemp -d)
|
|
|
|
|
|
|
|
trap "rm -fr $DIR" EXIT
|
|
|
|
|
2023-04-05 08:49:04 -04:00
|
|
|
function dependency_go() {
|
2023-10-04 09:18:17 -04:00
|
|
|
go_version="1.21.1.linux-amd64" # Set the desired Go version here
|
2023-08-14 11:56:30 -04:00
|
|
|
|
2023-04-05 08:49:04 -04:00
|
|
|
if ! which go > /dev/null ; then
|
2023-08-14 11:56:30 -04:00
|
|
|
apt-get update
|
2023-04-05 08:49:04 -04:00
|
|
|
apt-get install -y -qq wget tar
|
2023-08-14 11:56:30 -04:00
|
|
|
wget --quiet "https://go.dev/dl/go$go_version.tar.gz"
|
|
|
|
tar zxf "go$go_version.tar.gz"
|
2023-10-04 09:18:17 -04:00
|
|
|
export PATH="$(pwd)/go/bin:$PATH"
|
2023-04-05 08:49:04 -04:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkout() {
|
|
|
|
local git="$1"
|
2023-04-05 10:48:22 -04:00
|
|
|
rm -fr forgejo-runner
|
|
|
|
git clone $git forgejo-runner
|
2023-04-05 08:49:04 -04:00
|
|
|
}
|
|
|
|
|
2023-04-05 10:48:22 -04:00
|
|
|
function build_runner() {
|
2023-04-05 08:49:04 -04:00
|
|
|
local git="$1"
|
2023-04-05 10:48:22 -04:00
|
|
|
local version="${2:-main}"
|
2023-04-05 08:49:04 -04:00
|
|
|
|
|
|
|
(
|
|
|
|
checkout "$git"
|
2023-04-05 10:48:22 -04:00
|
|
|
dependency_go
|
2023-04-05 08:49:04 -04:00
|
|
|
cd forgejo-runner
|
|
|
|
git checkout "$version"
|
|
|
|
make build
|
|
|
|
)
|
2023-04-05 10:48:22 -04:00
|
|
|
export PATH=$PATH:$(pwd)/forgejo-runner
|
|
|
|
forgejo-runner --version
|
2023-04-05 08:49:04 -04:00
|
|
|
}
|
|
|
|
|
2023-10-07 17:27:24 -04:00
|
|
|
function branch_tip() {
|
|
|
|
local url="$1"
|
|
|
|
local repo="$2"
|
|
|
|
local branch="$3"
|
|
|
|
|
|
|
|
forgejo.sh retry forgejo-curl.sh api_json $url/api/v1/repos/$repo/branches/$branch >& /dev/null
|
|
|
|
forgejo-curl.sh api_json $url/api/v1/repos/$repo/branches/$branch | jq --raw-output .commit.id
|
|
|
|
}
|
|
|
|
|
2023-09-29 16:57:05 -04:00
|
|
|
function get_status() {
|
|
|
|
local url="$1"
|
|
|
|
local repo="$2"
|
|
|
|
local sha="$3"
|
|
|
|
|
|
|
|
forgejo-curl.sh api_json $url/api/v1/repos/$repo/commits/$sha/status
|
|
|
|
}
|
|
|
|
|
2023-03-29 09:23:09 -04:00
|
|
|
function check_status() {
|
|
|
|
local url="$1"
|
|
|
|
local repo="$2"
|
|
|
|
local sha="$3"
|
2023-10-07 15:38:23 -04:00
|
|
|
local expected_status="$4"
|
|
|
|
local expected_description="$5"
|
|
|
|
|
|
|
|
get_status $url $repo $sha > /tmp/status.json
|
|
|
|
local status="$(jq --raw-output .state < /tmp/status.json)"
|
|
|
|
local description="$(jq --raw-output .statuses[0].description < /tmp/status.json)"
|
|
|
|
if test "$status" = "$expected_status" && test -z "$expected_description" -o "$description" = "$expected_description"; then
|
|
|
|
echo OK
|
|
|
|
elif test "$status" = "failure" -o "$status" = "success"; then
|
|
|
|
echo NOK
|
|
|
|
else
|
|
|
|
echo RETRY
|
|
|
|
fi
|
2023-03-29 09:23:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function wait_success() {
|
2023-10-07 15:38:23 -04:00
|
|
|
wait_status success "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
function wait_failure() {
|
|
|
|
wait_status failure "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
function wait_running() {
|
|
|
|
wait_status pending "$@" "Has started running"
|
|
|
|
}
|
|
|
|
|
|
|
|
function wait_status() {
|
|
|
|
local status="$1"
|
|
|
|
local url="$2"
|
|
|
|
local repo="$3"
|
|
|
|
local sha="$4"
|
|
|
|
local description="$5"
|
2023-03-29 09:23:09 -04:00
|
|
|
|
|
|
|
for i in $(seq $LOOPS); do
|
2023-10-07 15:38:23 -04:00
|
|
|
if test $(check_status "$url" "$repo" "$sha" "$status" "$description") != RETRY ; then
|
2023-03-29 09:23:09 -04:00
|
|
|
break
|
|
|
|
fi
|
|
|
|
test "$FORGEJO_RUNNER_LOGS" && tail $FORGEJO_RUNNER_LOGS
|
|
|
|
sleep $LOOP_DELAY
|
|
|
|
done
|
2023-10-07 15:38:23 -04:00
|
|
|
if test $(check_status "$url" "$repo" "$sha" "$status" "$description") != "OK" ; then
|
2023-09-29 16:57:05 -04:00
|
|
|
get_status $url $repo $sha | jq .statuses
|
2023-03-29 09:23:09 -04:00
|
|
|
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"
|
2023-03-30 10:11:04 -04:00
|
|
|
local token="$6"
|
2023-03-29 09:23:09 -04:00
|
|
|
|
|
|
|
local dir="$DIR/$project"
|
2023-04-03 03:07:46 -04:00
|
|
|
rsync -a --exclude .git $directory/ $dir/
|
2023-03-29 10:50:46 -04:00
|
|
|
|
|
|
|
local workflows=$dir/.forgejo/workflows/*.yml
|
|
|
|
if test "$(echo $workflows)" != "$workflows"; then
|
2023-03-30 03:11:45 -04:00
|
|
|
sed -i \
|
2023-03-30 10:11:04 -04:00
|
|
|
-e "s|SELF|$url/$owner/$self_action|g" \
|
|
|
|
-e "s|FORGEJO_TOKEN|$token|g" \
|
2023-03-30 03:11:45 -04:00
|
|
|
$dir/.forgejo/workflows/*.yml
|
2023-03-29 10:50:46 -04:00
|
|
|
fi
|
2023-03-29 09:23:09 -04:00
|
|
|
(
|
|
|
|
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
|
2023-03-29 17:33:49 -04:00
|
|
|
sha=$(git rev-parse HEAD)
|
|
|
|
echo $sha > SHA
|
2023-03-29 09:23:09 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-09-24 14:32:40 -04:00
|
|
|
function push_workflow() {
|
2023-03-29 09:23:09 -04:00
|
|
|
local directory="$1"
|
|
|
|
local url="$2"
|
|
|
|
local owner="$3"
|
|
|
|
local project="$4"
|
|
|
|
local self_action="$5"
|
2023-03-30 10:11:04 -04:00
|
|
|
local token="$6"
|
2023-03-29 09:23:09 -04:00
|
|
|
|
2023-04-02 09:30:29 -04:00
|
|
|
if test -z "$token"; then
|
|
|
|
echo missing token argument
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2023-03-30 10:11:04 -04:00
|
|
|
push "$directory" "$url" "$owner" "$project" "$self_action" "$token"
|
2023-09-24 14:32:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function run_workflow() {
|
|
|
|
local directory="$1"
|
|
|
|
local url="$2"
|
|
|
|
local owner="$3"
|
|
|
|
local project="$4"
|
|
|
|
local self_action="$5"
|
|
|
|
local token="$6"
|
|
|
|
|
|
|
|
push_workflow "$directory" "$url" "$owner" "$project" "$self_action" "$token"
|
2023-03-29 09:23:09 -04:00
|
|
|
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"
|
2023-05-21 16:58:27 -04:00
|
|
|
git clone . $dir
|
2023-03-29 09:23:09 -04:00
|
|
|
(
|
|
|
|
cd $dir
|
2023-05-21 16:58:27 -04:00
|
|
|
rm -fr .forgejo .git
|
2023-03-29 09:23:09 -04:00
|
|
|
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
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-04-05 17:18:02 -04:00
|
|
|
function dependencies() {
|
|
|
|
if ! which jq curl > /dev/null ; then
|
|
|
|
apt-get -qq install -y jq curl
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
dependencies
|
|
|
|
|
2023-03-29 09:23:09 -04:00
|
|
|
"$@"
|