From 7df77db7b3d699f7fd58a7210a3225b06d9ff192 Mon Sep 17 00:00:00 2001 From: Timothy Kim Date: Mon, 16 Mar 2026 21:07:34 -0400 Subject: [PATCH] fix new-app: retry build trigger, clean up temp nginx conf, add remove-app script --- scripts/deploy.sh | 1 + scripts/new-app.sh | 16 ++++++--- scripts/remove-app.sh | 82 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 5 deletions(-) create mode 100755 scripts/remove-app.sh diff --git a/scripts/deploy.sh b/scripts/deploy.sh index 25496b1..da30526 100755 --- a/scripts/deploy.sh +++ b/scripts/deploy.sh @@ -27,6 +27,7 @@ NGINXCONF certbot certonly --webroot -w /opt/hantim/docker/nginx/certbot/www \ --non-interactive --agree-tos --register-unsafely-without-email \ --cert-name "$APP" -d "$APP" -d "www.$APP" + rm -f "/opt/hantim/docker/nginx/conf.d/$APP.conf" echo "Certificate issued for $APP" exit 0 fi diff --git a/scripts/new-app.sh b/scripts/new-app.sh index aab49dd..ff0d1dc 100755 --- a/scripts/new-app.sh +++ b/scripts/new-app.sh @@ -276,11 +276,17 @@ git push # --- Trigger initial build --- echo "==> Triggering initial build for $APP..." -TRIGGER_CODE=$(curl -s -o /dev/null -w "%{http_code}" \ - -X POST "$GITEA_URL/api/v1/repos/$GITEA_ORG/$APP/contents/.deploy-trigger" \ - -H "Authorization: token $GITEA_TOKEN" \ - -H "Content-Type: application/json" \ - -d "{\"content\": \"$(echo -n "initial deploy" | base64)\", \"message\": \"trigger initial build\"}") +# Wait for template repo to be ready (Gitea generates content asynchronously) +for i in $(seq 1 60); do + TRIGGER_CODE=$(curl -s -o /dev/null -w "%{http_code}" \ + -X POST "$GITEA_URL/api/v1/repos/$GITEA_ORG/$APP/contents/.deploy-trigger" \ + -H "Authorization: token $GITEA_TOKEN" \ + -H "Content-Type: application/json" \ + -d "{\"content\": \"$(echo -n "initial deploy" | base64)\", \"message\": \"trigger initial build\"}") + [ "$TRIGGER_CODE" != "404" ] && break + echo " Waiting for repo to be ready..." + sleep 2 +done if [ "$TRIGGER_CODE" = "201" ]; then echo " Build triggered." diff --git a/scripts/remove-app.sh b/scripts/remove-app.sh new file mode 100755 index 0000000..930efa0 --- /dev/null +++ b/scripts/remove-app.sh @@ -0,0 +1,82 @@ +#!/bin/bash +set -euo pipefail + +GITEA_URL="https://git.timothykim.net" +GITEA_ORG="hantim" + +if [ -z "${1:-}" ]; then + echo "Usage: ./scripts/remove-app.sh " + echo " Example: ./scripts/remove-app.sh hcsuzuki.net" + echo "" + echo "Removes: local files (compose, workflow, nginx conf), Gitea repo." + echo "Does NOT remove: DNS records, SSL certs." + exit 1 +fi + +APP="$1" +REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)" + +if ! [[ "$APP" =~ ^[a-zA-Z0-9][a-zA-Z0-9._-]*$ ]]; then + echo "Error: app name must be alphanumeric (hyphens, dots, underscores allowed)." + exit 1 +fi + +# --- Check dependencies --- + +if ! command -v bw &>/dev/null; then + echo "Error: Bitwarden CLI (bw) is not installed." + exit 1 +fi + +# --- Get Gitea API token from Bitwarden --- + +if bw status 2>/dev/null | grep -q '"status":"unauthenticated"'; then + echo "Log in to Bitwarden:" + bw login +fi + +if bw status 2>/dev/null | grep -q '"status":"locked"'; then + BW_SESSION=$(bw unlock --raw) + export BW_SESSION +fi + +bw sync --session "${BW_SESSION:-}" +GITEA_TOKEN=$(bw get notes hantim-new-app-script --session "${BW_SESSION:-}") + +# --- Delete Gitea repo --- + +echo "==> Deleting Gitea repo $GITEA_ORG/$APP..." +HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" \ + -X DELETE "$GITEA_URL/api/v1/repos/$GITEA_ORG/$APP" \ + -H "Authorization: token $GITEA_TOKEN") + +if [ "$HTTP_CODE" = "204" ]; then + echo " Deleted." +elif [ "$HTTP_CODE" = "404" ]; then + echo " Repo does not exist, skipping." +else + echo " Warning: unexpected response (HTTP $HTTP_CODE)." +fi + +# --- Remove local files --- + +echo "==> Removing local files..." +REMOVED=() + +for f in "docker/$APP" ".gitea/workflows/deploy-$APP.yml" "docker/nginx/conf.d/$APP.conf"; do + if [ -e "$REPO_ROOT/$f" ]; then + git -C "$REPO_ROOT" rm -rf "$f" + REMOVED+=("$f") + fi +done + +if [ ${#REMOVED[@]} -gt 0 ]; then + echo "==> Committing and pushing..." + cd "$REPO_ROOT" + git commit -m "remove $APP" + git push +else + echo " No local files to remove." +fi + +echo "Done. DNS records and SSL certs were left in place."