setup-forgejo/forgejo.sh
2023-07-27 20:51:40 +02:00

76 lines
2.6 KiB
Bash
Executable file

#!/bin/bash
# SPDX-License-Identifier: MIT
set -e
if ${VERBOSE:-false}; then set -x; fi
: ${CONTAINER:=forgejo}
function wait_for() {
rm -f /tmp/setup-forgejo.out
success=false
for delay in 1 1 5 5 15 15 15 30 30 30 30 ; do
if "$@" >> /tmp/setup-forgejo.out 2>&1 ; then
success=true
break
fi
cat /tmp/setup-forgejo.out
echo waiting $delay
sleep $delay
done
if test $success = false ; then
cat /tmp/setup-forgejo.out
return 1
fi
}
function run() {
local image="$1"
local version="$2"
if test "$version" != "1.19" && dpkg --compare-versions "$version" "lt" "1.19.0-3"; then
actions_unit="actions.actions"
else
actions_unit="repo.actions"
fi
docker run --restart unless-stopped --name ${CONTAINER} \
-e "RUN_MODE=dev" \
-e "FORGEJO__security__INSTALL_LOCK=true" \
-e "FORGEJO__log__LEVEL=debug" \
-e "FORGEJO__actions__ENABLED=true" \
-e "FORGEJO__repository__ENABLE_PUSH_CREATE_USER=true" \
-e "FORGEJO__repository__DEFAULT_PUSH_CREATE_PRIVATE=false" \
-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 $ip > forgejo-ip
docker exec ${CONTAINER} sed -i -e "s|localhost|$ip|" /data/gitea/conf/app.ini
docker restart ${CONTAINER}
}
function setup() {
local user="${1:-root}"
local password="${2:-admin1234}"
local image="${3:-codeberg.org/forgejo/forgejo}"
local version="${4:-1.20}"
run $image $version
sleep 5 # for some reason trying to run "forgejo admin" while forgejo is booting will permanently break everything
if docker exec --user 1000 ${CONTAINER} forgejo admin user list --admin | grep "$user" ; then
docker exec --user 1000 ${CONTAINER} forgejo admin user change-password --username "$user" --password "$password"
else
wait_for docker exec --user 1000 ${CONTAINER} forgejo admin user create --admin --username "$user" --password "$password" --email "$user@example.com"
fi
docker exec --user 1000 ${CONTAINER} forgejo admin user generate-access-token -u $user --raw --scopes 'all,sudo' > forgejo-token
( echo -n 'Authorization: token ' ; cat forgejo-token ) > forgejo-header
( echo "#!/bin/sh" ; echo 'curl -sS -H "Content-Type: application/json" -H @'$(pwd)/forgejo-header' "$@"' ) > forgejo-api && chmod +x forgejo-api
}
function teardown() {
docker stop ${CONTAINER} >& /dev/null || true
docker rm -f ${CONTAINER} >& /dev/null || true
}
"$@"