2023-03-29 15:23:09 +02:00
|
|
|
#!/bin/bash
|
2023-04-01 11:08:06 +02:00
|
|
|
# SPDX-License-Identifier: MIT
|
2023-03-29 15:23:09 +02:00
|
|
|
|
|
|
|
set -ex
|
|
|
|
|
|
|
|
: ${LOOPS:=40}
|
|
|
|
: ${LOOP_DELAY:=10}
|
|
|
|
DIR=$(mktemp -d)
|
|
|
|
|
|
|
|
trap "rm -fr $DIR" EXIT
|
|
|
|
|
2023-04-05 14:49:04 +02:00
|
|
|
function dependency_go() {
|
2023-08-14 16:56:30 +01:00
|
|
|
go_version="1.21.0.linux-amd64" # Set the desired Go version here
|
|
|
|
|
2023-04-05 14:49:04 +02:00
|
|
|
if ! which go > /dev/null ; then
|
2023-08-14 16:56:30 +01:00
|
|
|
apt-get update
|
2023-04-05 14:49:04 +02:00
|
|
|
apt-get install -y -qq wget tar
|
2023-08-14 16:56:30 +01:00
|
|
|
wget --quiet "https://go.dev/dl/go$go_version.tar.gz"
|
|
|
|
tar zxf "go$go_version.tar.gz"
|
|
|
|
export PATH="$PATH:$(pwd)/go/bin"
|
2023-04-05 14:49:04 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkout() {
|
|
|
|
local git="$1"
|
2023-04-05 16:48:22 +02:00
|
|
|
rm -fr forgejo-runner
|
|
|
|
git clone $git forgejo-runner
|
2023-04-05 14:49:04 +02:00
|
|
|
}
|
|
|
|
|
2023-04-05 16:48:22 +02:00
|
|
|
function build_runner() {
|
2023-04-05 14:49:04 +02:00
|
|
|
local git="$1"
|
2023-04-05 16:48:22 +02:00
|
|
|
local version="${2:-main}"
|
2023-04-05 14:49:04 +02:00
|
|
|
|
|
|
|
(
|
|
|
|
checkout "$git"
|
2023-04-05 16:48:22 +02:00
|
|
|
dependency_go
|
2023-04-05 14:49:04 +02:00
|
|
|
cd forgejo-runner
|
|
|
|
git checkout "$version"
|
|
|
|
make build
|
|
|
|
)
|
2023-04-05 16:48:22 +02:00
|
|
|
export PATH=$PATH:$(pwd)/forgejo-runner
|
|
|
|
forgejo-runner --version
|
2023-04-05 14:49:04 +02:00
|
|
|
}
|
|
|
|
|
2023-03-29 15:23:09 +02:00
|
|
|
function check_status() {
|
|
|
|
local url="$1"
|
|
|
|
local repo="$2"
|
|
|
|
local sha="$3"
|
|
|
|
|
2023-09-24 16:54:52 +02:00
|
|
|
local state=$(forgejo-curl.sh api_json $url/api/v1/repos/$repo/commits/$sha/status | jq --raw-output .state)
|
2023-03-29 15:23:09 +02:00
|
|
|
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
|
2023-09-24 16:54:52 +02:00
|
|
|
forgejo-curl.sh api_json $url/api/v1/repos/$repo/commits/$sha/status | jq .
|
2023-03-29 15:23:09 +02:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function push() {
|
|
|
|
local directory="$1"
|
|
|
|
local url="$2"
|
|
|
|
local owner="$3"
|
|
|
|
local project="$4"
|
|
|
|
local self_action="$5"
|
2023-03-30 16:11:04 +02:00
|
|
|
local token="$6"
|
2023-03-29 15:23:09 +02:00
|
|
|
|
|
|
|
local dir="$DIR/$project"
|
2023-04-03 09:07:46 +02:00
|
|
|
rsync -a --exclude .git $directory/ $dir/
|
2023-03-29 16:50:46 +02:00
|
|
|
|
|
|
|
local workflows=$dir/.forgejo/workflows/*.yml
|
|
|
|
if test "$(echo $workflows)" != "$workflows"; then
|
2023-03-30 09:11:45 +02:00
|
|
|
sed -i \
|
2023-03-30 16:11:04 +02:00
|
|
|
-e "s|SELF|$url/$owner/$self_action|g" \
|
|
|
|
-e "s|FORGEJO_TOKEN|$token|g" \
|
2023-03-30 09:11:45 +02:00
|
|
|
$dir/.forgejo/workflows/*.yml
|
2023-03-29 16:50:46 +02:00
|
|
|
fi
|
2023-03-29 15:23:09 +02: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 23:33:49 +02:00
|
|
|
sha=$(git rev-parse HEAD)
|
|
|
|
echo $sha > SHA
|
|
|
|
echo sha=$sha
|
2023-03-29 15:23:09 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-09-24 20:32:40 +02:00
|
|
|
function push_workflow() {
|
2023-03-29 15:23:09 +02:00
|
|
|
local directory="$1"
|
|
|
|
local url="$2"
|
|
|
|
local owner="$3"
|
|
|
|
local project="$4"
|
|
|
|
local self_action="$5"
|
2023-03-30 16:11:04 +02:00
|
|
|
local token="$6"
|
2023-03-29 15:23:09 +02:00
|
|
|
|
2023-04-02 15:30:29 +02:00
|
|
|
if test -z "$token"; then
|
|
|
|
echo missing token argument
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2023-03-30 16:11:04 +02:00
|
|
|
push "$directory" "$url" "$owner" "$project" "$self_action" "$token"
|
2023-09-24 20:32:40 +02: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 15:23:09 +02: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 22:58:27 +02:00
|
|
|
git clone . $dir
|
2023-03-29 15:23:09 +02:00
|
|
|
(
|
|
|
|
cd $dir
|
2023-05-21 22:58:27 +02:00
|
|
|
rm -fr .forgejo .git
|
2023-03-29 15:23:09 +02: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 23:18:02 +02:00
|
|
|
function dependencies() {
|
|
|
|
if ! which jq curl > /dev/null ; then
|
|
|
|
apt-get -qq install -y jq curl
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
dependencies
|
|
|
|
|
2023-03-29 15:23:09 +02:00
|
|
|
"$@"
|