mirror of
https://code.forgejo.org/actions/setup-forgejo.git
synced 2025-02-22 07:51:03 -05:00
Merge pull request 'chore(cleanup): shfmt and .editorconfig applied to all scripts' (#296) from earl-warren/setup-forgejo:wip-shfmt into main
Reviewed-on: https://code.forgejo.org/actions/setup-forgejo/pulls/296
This commit is contained in:
commit
3897b689c2
7 changed files with 151 additions and 141 deletions
10
.editorconfig
Normal file
10
.editorconfig
Normal file
|
@ -0,0 +1,10 @@
|
|||
root = true
|
||||
|
||||
[*]
|
||||
indent_style = space
|
||||
indent_size = 4
|
||||
tab_width = 4
|
||||
end_of_line = lf
|
||||
charset = utf-8
|
||||
trim_trailing_whitespace = true
|
||||
insert_final_newline = true
|
|
@ -3,19 +3,13 @@
|
|||
|
||||
set -e
|
||||
|
||||
SELF_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||
SELF_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
source $SELF_DIR/forgejo-lib.sh
|
||||
|
||||
: ${DIR:=/tmp/forgejo-binary}
|
||||
|
||||
if ${VERBOSE:-false}; then set -x; fi
|
||||
|
||||
function maybe_sudo() {
|
||||
if test $(id -u) != 0 ; then
|
||||
SUDO=sudo
|
||||
fi
|
||||
}
|
||||
|
||||
function ensure_user() {
|
||||
local user=$1
|
||||
|
||||
|
@ -28,7 +22,7 @@ function ensure_user() {
|
|||
|
||||
function dependencies() {
|
||||
maybe_sudo
|
||||
if ! which sudo curl daemon jq git-lfs ip sqlite3 envsubst unzip > /dev/null ; then
|
||||
if ! which sudo curl daemon jq git-lfs ip sqlite3 envsubst unzip >/dev/null; then
|
||||
$SUDO apt-get update -qq
|
||||
$SUDO apt-get -q install -y -qq sudo curl daemon jq git-lfs iproute2 sqlite3 gettext-base unzip
|
||||
fi
|
||||
|
@ -44,9 +38,9 @@ function cleanup_logs() {
|
|||
function download() {
|
||||
local url="$1"
|
||||
|
||||
if ! test -f $DIR/forgejo ; then
|
||||
curl -sS $url > $DIR/forgejo.tmp
|
||||
if file $DIR/forgejo.tmp | grep --quiet 'Zip archive' ; then
|
||||
if ! test -f $DIR/forgejo; then
|
||||
curl -sS $url >$DIR/forgejo.tmp
|
||||
if file $DIR/forgejo.tmp | grep --quiet 'Zip archive'; then
|
||||
unzip $DIR/forgejo.tmp -d $DIR
|
||||
rm -f $DIR/forgejo.tmp
|
||||
else
|
||||
|
@ -68,10 +62,10 @@ function create_user() {
|
|||
fi
|
||||
|
||||
$DIR/forgejocli admin user create --admin --username "$user" --password "$password" --email "$user@example.com"
|
||||
$DIR/forgejocli admin user generate-access-token -u "$user" --raw --scopes all > $DIR/forgejo-token
|
||||
$DIR/forgejocli admin user generate-access-token -u "$user" --raw --scopes all >$DIR/forgejo-token
|
||||
local url="http://$(cat $DIR/forgejo-ip):3000"
|
||||
echo "$url" > $DIR/forgejo-url
|
||||
echo "http://${user}:${password}@$(cat $DIR/forgejo-ip):3000" > $DIR/forgejo-auth-url
|
||||
echo "$url" >$DIR/forgejo-url
|
||||
echo "http://${user}:${password}@$(cat $DIR/forgejo-ip):3000" >$DIR/forgejo-auth-url
|
||||
retry forgejo-curl.sh --user "$user" --password "$password" --token @$DIR/forgejo-token login "$url"
|
||||
echo forgejo-curl.sh api_json $url/api/forgejo/v1/version
|
||||
forgejo-curl.sh api_json $url/api/forgejo/v1/version
|
||||
|
@ -84,14 +78,14 @@ function myip() {
|
|||
function start_forgejo() {
|
||||
local work_path=$DIR/forgejo-work-path
|
||||
daemon --chdir=$DIR --unsafe --env="TERM=$TERM" --env="HOME=$HOME" --env="PATH=$PATH" --pidfile=$DIR/forgejo-pid --errlog=$DIR/forgejo-err.log --output=$DIR/forgejo-out.log -- $DIR/forgejo --config $work_path/app.ini --work-path $work_path
|
||||
if ! retry grep --no-messages --quiet 'Starting server on' $work_path/log/forgejo.log ; then
|
||||
if ! retry grep --no-messages --quiet 'Starting server on' $work_path/log/forgejo.log; then
|
||||
cat $DIR/*.log
|
||||
cat $work_path/log/*.log
|
||||
return 1
|
||||
fi
|
||||
echo "$DIR/forgejo --config $work_path/app.ini --work-path $work_path" '"$@"' > $DIR/forgejocli
|
||||
echo "$DIR/forgejo --config $work_path/app.ini --work-path $work_path" '"$@"' >$DIR/forgejocli
|
||||
chmod +x $DIR/forgejocli
|
||||
myip > $DIR/forgejo-ip
|
||||
myip >$DIR/forgejo-ip
|
||||
}
|
||||
|
||||
function reset_forgejo() {
|
||||
|
@ -99,7 +93,7 @@ function reset_forgejo() {
|
|||
local work_path=$DIR/forgejo-work-path
|
||||
rm -fr $work_path
|
||||
mkdir -p $work_path
|
||||
IP=$(myip) WORK_PATH=$work_path envsubst < $SELF_DIR/$config-app.ini > $work_path/app.ini
|
||||
IP=$(myip) WORK_PATH=$work_path envsubst <$SELF_DIR/$config-app.ini >$work_path/app.ini
|
||||
}
|
||||
|
||||
function teardown() {
|
||||
|
|
|
@ -15,7 +15,7 @@ function install_docker() {
|
|||
|
||||
function install_other() {
|
||||
local packages="sudo git"
|
||||
if ! which $packages ; then
|
||||
if ! which $packages; then
|
||||
apt-get update -qq
|
||||
apt-get install -y -qq $packages
|
||||
fi
|
||||
|
|
|
@ -11,8 +11,8 @@ function retry() {
|
|||
local tmp=/tmp
|
||||
rm -f $tmp/retry.out
|
||||
success=false
|
||||
for delay in $RETRY_DELAYS ; do
|
||||
if "$@" >> $tmp/retry.out 2>&1 ; then
|
||||
for delay in $RETRY_DELAYS; do
|
||||
if "$@" >>$tmp/retry.out 2>&1; then
|
||||
success=true
|
||||
break
|
||||
fi
|
||||
|
@ -20,7 +20,7 @@ function retry() {
|
|||
echo waiting $delay
|
||||
sleep $delay
|
||||
done
|
||||
if test $success = false ; then
|
||||
if test $success = false; then
|
||||
cat $tmp/retry.out
|
||||
return 1
|
||||
fi
|
||||
|
@ -30,12 +30,12 @@ function stop_daemon() {
|
|||
local daemon=$1
|
||||
local dir=${2:-.}
|
||||
|
||||
if test -f $dir/$daemon-pid ; then
|
||||
if test -f $dir/$daemon-pid; then
|
||||
local pid=$(cat $dir/$daemon-pid)
|
||||
kill -TERM $pid
|
||||
pidwait $pid || true
|
||||
for delay in 1 1 2 2 5 5 ; do
|
||||
if ! test -f $dir/$daemon-pid ; then
|
||||
for delay in 1 1 2 2 5 5; do
|
||||
if ! test -f $dir/$daemon-pid; then
|
||||
break
|
||||
fi
|
||||
sleep $delay
|
||||
|
@ -47,11 +47,11 @@ function stop_daemon() {
|
|||
function dependency_go() {
|
||||
go_version="1.21.4.linux-amd64" # Set the desired Go version here
|
||||
|
||||
if ! which wget tar > /dev/null ; then
|
||||
if ! which wget tar >/dev/null; then
|
||||
apt-get install -y -qq wget tar
|
||||
fi
|
||||
|
||||
if ! which go > /dev/null ; then
|
||||
if ! which go >/dev/null; then
|
||||
wget --quiet "https://go.dev/dl/go$go_version.tar.gz"
|
||||
tar zxf "go$go_version.tar.gz"
|
||||
export PATH="$(pwd)/go/bin:$PATH"
|
||||
|
|
|
@ -4,14 +4,14 @@
|
|||
set -e
|
||||
|
||||
PS4='${BASH_SOURCE[0]}:$LINENO: ${FUNCNAME[0]}: '
|
||||
SELF_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||
SELF_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
source $SELF_DIR/forgejo-lib.sh
|
||||
|
||||
: ${FORGEJO_RUNNER_CONFIG:=$SELF_DIR/runner-config.yaml}
|
||||
: ${DIR:=$(pwd)}
|
||||
|
||||
function dependencies() {
|
||||
if ! which make curl daemon > /dev/null ; then
|
||||
if ! which make curl daemon >/dev/null; then
|
||||
apt-get install -y -qq make curl daemon
|
||||
fi
|
||||
}
|
||||
|
@ -20,10 +20,10 @@ function download() {
|
|||
local runner_repository="$1"
|
||||
local version="$2"
|
||||
|
||||
if ! which $DIR/forgejo-runner > /dev/null; then
|
||||
if ! curl -L --fail -sS $runner_repository/releases/download/$version/forgejo-runner-${version#v}-linux-amd64 > $DIR/forgejo-runner ; then
|
||||
if ! which $DIR/forgejo-runner >/dev/null; then
|
||||
if ! curl -L --fail -sS $runner_repository/releases/download/$version/forgejo-runner-${version#v}-linux-amd64 >$DIR/forgejo-runner; then
|
||||
# backward compatibility for for the naming scheme prior to 3.0.0
|
||||
curl -L --fail -sS $runner_repository/releases/download/$version/forgejo-runner-linux-amd64 > $DIR/forgejo-runner
|
||||
curl -L --fail -sS $runner_repository/releases/download/$version/forgejo-runner-linux-amd64 >$DIR/forgejo-runner
|
||||
fi
|
||||
chmod 755 $DIR/forgejo-runner
|
||||
fi
|
||||
|
@ -31,7 +31,7 @@ function download() {
|
|||
|
||||
function register() {
|
||||
local forgejo="$1"
|
||||
mktemp -d > $DIR/forgejo-runner-home
|
||||
mktemp -d >$DIR/forgejo-runner-home
|
||||
$DIR/forgejocli actions generate-runner-token | tee $DIR/forgejo-runner-token
|
||||
(
|
||||
cd $DIR
|
||||
|
@ -63,7 +63,7 @@ function setup() {
|
|||
local forgejo="${3:-$(cat $DIR/forgejo-url)}"
|
||||
|
||||
dependencies
|
||||
if [[ "$version" =~ ^@ ]] ; then
|
||||
if [[ "$version" =~ ^@ ]]; then
|
||||
local branch=${version##@}
|
||||
build_runner $runner_repository $branch $DIR
|
||||
else
|
||||
|
@ -75,7 +75,7 @@ function setup() {
|
|||
|
||||
function teardown() {
|
||||
stop_daemon forgejo-runner $DIR
|
||||
if test -f $DIR/forgejo-runner-home ; then
|
||||
if test -f $DIR/forgejo-runner-home; then
|
||||
rm -fr $(cat $DIR/forgejo-runner-home)
|
||||
fi
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
set -ex
|
||||
|
||||
SELF_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||
SELF_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
source $SELF_DIR/forgejo-lib.sh
|
||||
|
||||
: ${LOOPS:=80}
|
||||
|
@ -18,7 +18,7 @@ function branch_tip() {
|
|||
local repo="$2"
|
||||
local branch="$3"
|
||||
|
||||
forgejo.sh retry forgejo-curl.sh api_json $url/api/v1/repos/$repo/branches/$branch >& /dev/null
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -37,9 +37,9 @@ function check_status() {
|
|||
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)"
|
||||
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
|
||||
|
@ -69,13 +69,13 @@ function wait_status() {
|
|||
local description="$5"
|
||||
|
||||
for i in $(seq $LOOPS); do
|
||||
if test $(check_status "$url" "$repo" "$sha" "$status" "$description") != RETRY ; then
|
||||
if test $(check_status "$url" "$repo" "$sha" "$status" "$description") != RETRY; then
|
||||
break
|
||||
fi
|
||||
test "$FORGEJO_RUNNER_LOGS" && tail $FORGEJO_RUNNER_LOGS
|
||||
sleep $LOOP_DELAY
|
||||
done
|
||||
if test $(check_status "$url" "$repo" "$sha" "$status" "$description") != "OK" ; then
|
||||
if test $(check_status "$url" "$repo" "$sha" "$status" "$description") != "OK"; then
|
||||
get_status $url $repo $sha | jq .statuses
|
||||
test "$FORGEJO_RUNNER_LOGS" && cat $FORGEJO_RUNNER_LOGS
|
||||
return 1
|
||||
|
@ -111,7 +111,7 @@ function push() {
|
|||
git remote add origin $url/$owner/$project
|
||||
git push --force -u origin main
|
||||
sha=$(git rev-parse HEAD)
|
||||
echo $sha > SHA
|
||||
echo $sha >SHA
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -144,7 +144,7 @@ function run_workflow() {
|
|||
}
|
||||
|
||||
function clear_runner_cache() {
|
||||
if test -f $DIR/forgejo-runner-home ; then
|
||||
if test -f $DIR/forgejo-runner-home; then
|
||||
rm -fr $(cat $DIR/forgejo-runner-home)/.cache/act
|
||||
fi
|
||||
}
|
||||
|
@ -177,7 +177,7 @@ function push_self_action() {
|
|||
}
|
||||
|
||||
function dependencies() {
|
||||
if ! which jq curl > /dev/null ; then
|
||||
if ! which jq curl >/dev/null; then
|
||||
apt-get -qq install -y jq curl
|
||||
fi
|
||||
}
|
||||
|
|
28
forgejo.sh
28
forgejo.sh
|
@ -3,7 +3,7 @@
|
|||
|
||||
set -e
|
||||
|
||||
SELF_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||
SELF_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
source $SELF_DIR/forgejo-lib.sh
|
||||
|
||||
if ${VERBOSE:-false}; then set -x; fi
|
||||
|
@ -32,10 +32,10 @@ function run() {
|
|||
-e "FORGEJO__repository__DEFAULT_REPO_UNITS=repo.code,repo.releases,repo.issues,repo.pulls,repo.wiki,repo.projects,repo.packages,$actions_unit" \
|
||||
-d $image:$version
|
||||
local ip="$(docker inspect -f "{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}" ${CONTAINER})"
|
||||
echo 'docker exec --user 1000 '${CONTAINER}' forgejo "$@"' > $DIR/forgejocli
|
||||
echo 'docker exec --user 1000 '${CONTAINER}' forgejo "$@"' >$DIR/forgejocli
|
||||
chmod +x $DIR/forgejocli
|
||||
echo $ip > $DIR/forgejo-ip
|
||||
echo "http://$(cat $DIR/forgejo-ip):3000" > $DIR/forgejo-url
|
||||
echo $ip >$DIR/forgejo-ip
|
||||
echo "http://$(cat $DIR/forgejo-ip):3000" >$DIR/forgejo-url
|
||||
docker exec ${CONTAINER} sed -i -e "s|localhost|$ip|" /data/gitea/conf/app.ini
|
||||
docker restart ${CONTAINER}
|
||||
}
|
||||
|
@ -49,7 +49,7 @@ function setup() {
|
|||
run $image $version
|
||||
|
||||
sleep 5 # for some reason trying to run "forgejo admin" while forgejo is booting will permanently break everything
|
||||
if $DIR/forgejocli admin user list --admin | grep "$user" ; then
|
||||
if $DIR/forgejocli admin user list --admin | grep "$user"; then
|
||||
$DIR/forgejocli admin user change-password --username "$user" --password "$password"
|
||||
else
|
||||
retry $DIR/forgejocli admin user create --admin --username "$user" --password "$password" --email "$user@example.com"
|
||||
|
@ -57,19 +57,25 @@ function setup() {
|
|||
#
|
||||
# The 'sudo' scope was removed in Forgejo v1.20 and is ignored
|
||||
#
|
||||
echo "http://${user}:${password}@$(cat $DIR/forgejo-ip):3000" > $DIR/forgejo-auth-url
|
||||
$DIR/forgejocli admin user generate-access-token -u $user --raw --scopes 'all,sudo' > $DIR/forgejo-token
|
||||
echo "http://${user}:${password}@$(cat $DIR/forgejo-ip):3000" >$DIR/forgejo-auth-url
|
||||
$DIR/forgejocli admin user generate-access-token -u $user --raw --scopes 'all,sudo' >$DIR/forgejo-token
|
||||
retry forgejo-curl.sh --user "$user" --password "$password" --token @$DIR/forgejo-token login http://$(cat $DIR/forgejo-ip):3000
|
||||
#
|
||||
# Redundant with forgejo-curl.sh, kept around for backward compatibility 09/2023
|
||||
#
|
||||
( echo -n 'Authorization: token ' ; cat $DIR/forgejo-token ) > $DIR/forgejo-header
|
||||
( echo "#!/bin/sh" ; echo 'curl -sS -H "Content-Type: application/json" -H @'$DIR/forgejo-header' "$@"' ) > $DIR/forgejo-api && chmod +x $DIR/forgejo-api
|
||||
(
|
||||
echo -n 'Authorization: token '
|
||||
cat $DIR/forgejo-token
|
||||
) >$DIR/forgejo-header
|
||||
(
|
||||
echo "#!/bin/sh"
|
||||
echo 'curl -sS -H "Content-Type: application/json" -H @'$DIR/forgejo-header' "$@"'
|
||||
) >$DIR/forgejo-api && chmod +x $DIR/forgejo-api
|
||||
}
|
||||
|
||||
function teardown() {
|
||||
docker stop ${CONTAINER} >& /dev/null || true
|
||||
docker rm -f ${CONTAINER} >& /dev/null || true
|
||||
docker stop ${CONTAINER} >&/dev/null || true
|
||||
docker rm -f ${CONTAINER} >&/dev/null || true
|
||||
}
|
||||
|
||||
"$@"
|
||||
|
|
Loading…
Add table
Reference in a new issue