mirror of
https://code.forgejo.org/actions/setup-forgejo.git
synced 2024-11-24 00:40:58 -05:00
168 lines
3.5 KiB
Bash
Executable file
168 lines
3.5 KiB
Bash
Executable file
#!/bin/bash
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
set -ex
|
|
|
|
: ${LOOPS:=40}
|
|
: ${LOOP_DELAY:=10}
|
|
DIR=$(mktemp -d)
|
|
|
|
trap "rm -fr $DIR" EXIT
|
|
|
|
function dependency_go() {
|
|
if ! which go > /dev/null ; then
|
|
apt-get update
|
|
apt-get install -y -qq wget tar
|
|
wget --quiet https://go.dev/dl/go1.20.5.linux-amd64.tar.gz
|
|
tar zxf go1.20.5.linux-amd64.tar.gz
|
|
export PATH=$PATH:$(pwd)/go/bin
|
|
fi
|
|
}
|
|
|
|
function checkout() {
|
|
local git="$1"
|
|
rm -fr forgejo-runner
|
|
git clone $git forgejo-runner
|
|
}
|
|
|
|
function build_runner() {
|
|
local git="$1"
|
|
local version="${2:-main}"
|
|
|
|
(
|
|
checkout "$git"
|
|
dependency_go
|
|
cd forgejo-runner
|
|
git checkout "$version"
|
|
make build
|
|
)
|
|
export PATH=$PATH:$(pwd)/forgejo-runner
|
|
forgejo-runner --version
|
|
}
|
|
|
|
function api() {
|
|
method=$1
|
|
shift
|
|
url=$1
|
|
shift
|
|
path=$1
|
|
shift
|
|
token=$1
|
|
shift
|
|
|
|
curl --fail -X $method -sS -H "Content-Type: application/json" -H "Authorization: token $token" "$@" $url/api/v1/$path
|
|
}
|
|
|
|
function check_status() {
|
|
local url="$1"
|
|
local repo="$2"
|
|
local sha="$3"
|
|
|
|
local state=$(api GET $url 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
|
|
api GET $url repos/$repo/commits/$sha/status | jq .
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
function push() {
|
|
local directory="$1"
|
|
local url="$2"
|
|
local owner="$3"
|
|
local project="$4"
|
|
local self_action="$5"
|
|
local token="$6"
|
|
|
|
local dir="$DIR/$project"
|
|
rsync -a --exclude .git $directory/ $dir/
|
|
|
|
local workflows=$dir/.forgejo/workflows/*.yml
|
|
if test "$(echo $workflows)" != "$workflows"; then
|
|
sed -i \
|
|
-e "s|SELF|$url/$owner/$self_action|g" \
|
|
-e "s|FORGEJO_TOKEN|$token|g" \
|
|
$dir/.forgejo/workflows/*.yml
|
|
fi
|
|
(
|
|
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
|
|
sha=$(git rev-parse HEAD)
|
|
echo $sha > SHA
|
|
echo sha=$sha
|
|
)
|
|
}
|
|
|
|
function run_workflow() {
|
|
local directory="$1"
|
|
local url="$2"
|
|
local owner="$3"
|
|
local project="$4"
|
|
local self_action="$5"
|
|
local token="$6"
|
|
|
|
if test -z "$token"; then
|
|
echo missing token argument
|
|
return 1
|
|
fi
|
|
|
|
push "$directory" "$url" "$owner" "$project" "$self_action" "$token"
|
|
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
|
|
)
|
|
}
|
|
|
|
function dependencies() {
|
|
if ! which jq curl > /dev/null ; then
|
|
apt-get -qq install -y jq curl
|
|
fi
|
|
}
|
|
|
|
dependencies
|
|
|
|
"$@"
|