Files
appfactory-tools/scripts/new-static-site-app.sh
T

127 lines
2.8 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
source /opt/appfactory/config/appfactory.env
APP_ID="${1:-}"
APP_NAME="${2:-$APP_ID}"
if [ -z "$APP_ID" ]; then
echo "Usage: new-static-site-app.sh <app-id> [app-name]"
exit 1
fi
APP_DIR="$WORKSPACE_DIR/$APP_ID"
TARGET_DIR="$APP_DIR"
TOOLS_DIR="$WORKSPACE_DIR/appfactory-tools"
mkdir -p "$APP_DIR"
cd "$APP_DIR"
cat > index.html <<EOF_HTML
<!doctype html>
<html lang="cs">
<head>
<meta charset="utf-8">
<title>$APP_NAME</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<main>
<h1>$APP_NAME</h1>
<p>Služba <strong>$APP_ID</strong> běží.</p>
</main>
</body>
</html>
EOF_HTML
cat > health.json <<'EOF_JSON'
{
"status": "ok"
}
EOF_JSON
cat > nginx.conf <<'EOF_CONF'
server {
listen 80;
server_name _;
root /usr/share/nginx/html;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
location /health {
default_type application/json;
alias /usr/share/nginx/html/health.json;
}
}
EOF_CONF
cat > Dockerfile <<'EOF_DOCKER'
FROM nginx:1.27-alpine
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY index.html /usr/share/nginx/html/index.html
COPY health.json /usr/share/nginx/html/health.json
EXPOSE 80
EOF_DOCKER
cat > .dockerignore <<'EOF_IGNORE'
.git/
EOF_IGNORE
cat > README.md <<EOF_MD
# $APP_NAME
Static Site služba vytvořená přes CSBot Services Portal.
EOF_MD
cp "$TOOLS_DIR/AGENTS.md" "$TARGET_DIR/AGENTS.md"
git init
git config user.name "AppFactory Bot"
git config user.email "appfactory@local"
git branch -M main
git add .
git commit -m "Initial Static Site service"
curl -sS -X POST "$GITEA_URL/api/v1/orgs/$GITEA_ORG/repos" \
-H "Authorization: token $GITEA_TOKEN" \
-H "Content-Type: application/json" \
-d "{\"name\":\"$APP_ID\",\"private\":false,\"auto_init\":false}" >/dev/null || true
echo "Creating Gitea webhook..."
curl -sS -X POST \
"$GITEA_URL/api/v1/repos/$GITEA_ORG/$APP_ID/hooks" \
-H "Authorization: token $GITEA_TOKEN" \
-H "Content-Type: application/json" \
-d "{
\"type\": \"gitea\",
\"active\": true,
\"events\": [\"push\"],
\"config\": {
\"url\": \"$WEBHOOK_INTERNAL_URL\",
\"content_type\": \"json\",
\"secret\": \"$WEBHOOK_SECRET\"
}
}" > /tmp/appfactory-create-hook-response.json
if grep -q '"message"' /tmp/appfactory-create-hook-response.json; then
cat /tmp/appfactory-create-hook-response.json
fi
if [[ "$GITEA_URL" == http://* ]]; then
GITEA_PUSH_URL="${GITEA_URL/http:\/\//http:\/\/${GITEA_TOKEN}@}"
elif [[ "$GITEA_URL" == https://* ]]; then
GITEA_PUSH_URL="${GITEA_URL/https:\/\//https:\/\/${GITEA_TOKEN}@}"
else
echo "Unsupported GITEA_URL: $GITEA_URL"
exit 1
fi
git remote remove origin 2>/dev/null || true
git remote add origin "$GITEA_PUSH_URL/$GITEA_ORG/$APP_ID.git"
git push -u origin main