SHA256
forked from course-team/tjhlp-gitea
docker: 添加使用docker部署的方法用于快速测试 (#2)
添加使用docker部署的方法**用于快速测试** 路径于`deploy\compose`,目录下有相应部署指导文档 本次改动还将一些配置置为 example,在 git 中取消追踪**以免误操作将敏感信息提交** Reviewed-on: https://cpp.tongji.sh:2080/jkljkluiouio/tjhlp-gitea/pulls/2 Co-authored-by: oierxjn <[email protected]> Co-committed-by: oierxjn <[email protected]>
This commit was merged in pull request #2.
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
#!/usr/bin/env sh
|
||||
|
||||
set -eu
|
||||
|
||||
SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
|
||||
ENV_FILE="$SCRIPT_DIR/.env"
|
||||
COMPOSE_FILE="$SCRIPT_DIR/docker-compose.yml"
|
||||
|
||||
log() {
|
||||
printf '%s\n' "$*"
|
||||
}
|
||||
|
||||
fail() {
|
||||
printf 'Error: %s\n' "$*" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
require_command() {
|
||||
command -v "$1" >/dev/null 2>&1 || fail "missing required command: $1"
|
||||
}
|
||||
|
||||
require_command docker
|
||||
|
||||
[ -f "$COMPOSE_FILE" ] || fail "docker-compose.yml not found in $SCRIPT_DIR"
|
||||
[ -f "$ENV_FILE" ] || fail ".env not found in $SCRIPT_DIR; copy .env.example to .env first"
|
||||
|
||||
set -a
|
||||
# shellcheck disable=SC1090
|
||||
. "$ENV_FILE"
|
||||
set +a
|
||||
|
||||
for var_name in GITEA_ADMIN_USERNAME GITEA_ADMIN_PASSWORD GITEA_ADMIN_EMAIL GITEA_ROOT_URL GITEA_HTTP_PORT PUID PGID; do
|
||||
# 固定列表,非用户输入,无注入风险
|
||||
eval "var_value=\${$var_name:-}"
|
||||
[ -n "$var_value" ] || fail "required variable $var_name is missing or empty in .env"
|
||||
done
|
||||
|
||||
dc() {
|
||||
docker compose --file "$COMPOSE_FILE" --env-file "$ENV_FILE" "$@"
|
||||
}
|
||||
|
||||
check_gitea_ready() {
|
||||
dc exec -T gitea sh -c "
|
||||
if command -v wget >/dev/null 2>&1; then
|
||||
wget -q -O - 'http://127.0.0.1:${GITEA_HTTP_PORT}/api/v1/version' >/dev/null 2>&1
|
||||
elif command -v curl >/dev/null 2>&1; then
|
||||
curl -fsS 'http://127.0.0.1:${GITEA_HTTP_PORT}/api/v1/version' >/dev/null 2>&1
|
||||
else
|
||||
exit 1
|
||||
fi
|
||||
"
|
||||
}
|
||||
|
||||
dc version >/dev/null 2>&1 || fail "docker compose is not available"
|
||||
|
||||
gitea_state=$(dc ps -q gitea 2>/dev/null || true)
|
||||
[ -n "$gitea_state" ] || fail "gitea service is not created; run 'docker compose up -d' first"
|
||||
|
||||
db_state=$(dc ps -q postgres 2>/dev/null || true)
|
||||
[ -n "$db_state" ] || fail "postgres service is not created; run 'docker compose up -d' first"
|
||||
|
||||
log "Waiting for Gitea to become ready at ${GITEA_ROOT_URL%/}/api/v1/version ..."
|
||||
|
||||
attempt=0
|
||||
max_attempts=30
|
||||
ready=0
|
||||
|
||||
# 最长约 60 秒,每 2 秒检查一次
|
||||
while [ "$attempt" -lt "$max_attempts" ]; do
|
||||
attempt=$((attempt + 1))
|
||||
if check_gitea_ready; then
|
||||
ready=1
|
||||
break
|
||||
fi
|
||||
sleep 2
|
||||
done
|
||||
|
||||
[ "$ready" -eq 1 ] || fail "Gitea did not become ready after $((max_attempts * 2)) seconds"
|
||||
|
||||
log "Creating admin user '${GITEA_ADMIN_USERNAME}' if needed ..."
|
||||
|
||||
create_output=$(
|
||||
dc exec -T --user "${PUID}:${PGID}" gitea gitea admin user create \
|
||||
--admin \
|
||||
--username "$GITEA_ADMIN_USERNAME" \
|
||||
--password "$GITEA_ADMIN_PASSWORD" \
|
||||
--email "$GITEA_ADMIN_EMAIL" 2>&1
|
||||
) || {
|
||||
case "$create_output" in
|
||||
*"already exists"*)
|
||||
log "Admin user '${GITEA_ADMIN_USERNAME}' already exists; skipping."
|
||||
exit 0
|
||||
;;
|
||||
*)
|
||||
printf '%s\n' "$create_output" >&2
|
||||
fail "failed to create admin user"
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
log "Admin user '${GITEA_ADMIN_USERNAME}' created successfully."
|
||||
Reference in New Issue
Block a user