77 lines
2.3 KiB
Bash
77 lines
2.3 KiB
Bash
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
# Environment variables (can be passed via docker-compose)
|
|
: "${GIT_REPO:?You must set GIT_REPO environment variable}"
|
|
GIT_BRANCH="${GIT_BRANCH:-main}"
|
|
WEBHOOK_SECRET="${WEBHOOK_SECRET:-}"
|
|
WEBHOOK_PORT="${WEBHOOK_PORT:-9000}"
|
|
HUGO_FLAGS="${HUGO_FLAGS:-}"
|
|
HUGO_BASEURL="${HUGO_BASEURL:-http://localhost:1313}"
|
|
REPO_DIR="/app/site"
|
|
HOOKS_FILE="/app/hooks.json"
|
|
WEBHOOK_BIN="/usr/local/bin/webhook"
|
|
|
|
# Clone repo into volume if missing
|
|
if [ ! -d "$REPO_DIR/.git" ]; then
|
|
echo "Cloning $GIT_REPO (branch $GIT_BRANCH) into $REPO_DIR"
|
|
git clone --branch "$GIT_BRANCH" --single-branch --recursive "$GIT_REPO" "$REPO_DIR"
|
|
else
|
|
echo "Repository already exists at $REPO_DIR"
|
|
fi
|
|
|
|
# Ensure working tree matches origin branch
|
|
git -C "$REPO_DIR" fetch origin "$GIT_BRANCH" || true
|
|
git -C "$REPO_DIR" reset --hard "origin/$GIT_BRANCH" || true
|
|
|
|
# Generate hooks.json for adnanh/webhook with secret and trigger rule for branch
|
|
cat > "$HOOKS_FILE" <<EOF
|
|
[
|
|
{
|
|
"id": "update-site",
|
|
"execute-command": "/app/hooks/update_site.sh",
|
|
"command-working-directory": "/app",
|
|
"response-message": "Update started",
|
|
"trigger-rule": {
|
|
"and": [
|
|
{
|
|
"match": {
|
|
"type": "value",
|
|
"value": "refs/heads/${GIT_BRANCH}",
|
|
"parameter": {
|
|
"source": "payload",
|
|
"name": "ref"
|
|
}
|
|
}
|
|
}
|
|
]
|
|
},
|
|
"pass-arguments-to-command": [
|
|
{
|
|
"source": "payload",
|
|
"name": "ref"
|
|
}
|
|
]
|
|
$( [ -n "$WEBHOOK_SECRET" ] && echo ",\"secret\": \"$WEBHOOK_SECRET\"" || echo "" )
|
|
}
|
|
]
|
|
EOF
|
|
|
|
echo "Created webhook hooks file at $HOOKS_FILE (branch filter: $GIT_BRANCH)"
|
|
|
|
# Start webhook in background
|
|
echo "Starting adnanh/webhook on port $WEBHOOK_PORT"
|
|
# run webhook with hooks file and allow hotreload (good for changing hooks.json via entrypoint)
|
|
$WEBHOOK_BIN -hooks "$HOOKS_FILE" -port "$WEBHOOK_PORT" -hotreload &
|
|
WEBHOOK_PID=$!
|
|
|
|
# Start hugo server in foreground (PID 1 for container)
|
|
echo "Starting Hugo server, baseURL=$HUGO_BASEURL"
|
|
cd "$REPO_DIR"
|
|
# bind to 0.0.0.0 so it is reachable outside container
|
|
hugo server --bind 0.0.0.0 --baseURL "${HUGO_BASEURL}" ${HUGO_FLAGS}
|
|
|
|
# If hugo exits, shut down webhook as well
|
|
kill "$WEBHOOK_PID" 2>/dev/null || true
|
|
wait "$WEBHOOK_PID" 2>/dev/null || true
|