homelab/.gitea/workflows/auto-deploy.yml
Nathan 2466972389
Some checks failed
Auto-Deploy Changed Stacks / deploy (push) Failing after 13s
feat: add git installation step to auto-deploy workflow
2026-06-02 12:35:06 -04:00

111 lines
3.7 KiB
YAML

name: Auto-Deploy Changed Stacks
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Install git
run: apt-get update && apt-get install -y git curl
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 2
- name: Detect changed stacks and deploy via Komodo API
env:
KOMODO_URL: ${{ secrets.KOMODO_URL }}
KOMODO_API_KEY: ${{ secrets.KOMODO_API_KEY }}
KOMODO_API_SECRET: ${{ secrets.KOMODO_API_SECRET }}
run: |
set -euo pipefail
# ---------------------------------------------------------------
# Detect changed paths vs the previous commit
# ---------------------------------------------------------------
CHANGED_FILES=$(git diff --name-only HEAD~1 HEAD)
echo "=== Changed files ==="
if [[ -z "$CHANGED_FILES" ]]; then
echo " (none)"
else
echo "$CHANGED_FILES"
fi
echo ""
# ---------------------------------------------------------------
# Parse changed paths → extract {node} and {stack}
# Expected pattern: nodes/{node}/{stack}/...
# ---------------------------------------------------------------
declare -A SEEN_STACKS
declare -a STACKS_TO_DEPLOY
while IFS= read -r filepath; do
[[ -z "$filepath" ]] && continue
IFS='/' read -ra PARTS <<< "$filepath"
# Must start with "nodes" and have at least 4 components
# (nodes / {node} / {stack} / file)
[[ "${PARTS[0]}" != "nodes" ]] && continue
[[ "${#PARTS[@]}" -lt 4 ]] && continue
node="${PARTS[1]}"
stack="${PARTS[2]}"
[[ -z "$stack" ]] && continue
# Deduplicate: skip if this stack was already queued
[[ -n "${SEEN_STACKS[$stack]+_}" ]] && continue
SEEN_STACKS["$stack"]=1
STACKS_TO_DEPLOY+=("$stack")
echo " Queued for deploy: $stack (node: $node)"
done <<< "$CHANGED_FILES"
echo ""
if [[ "${#STACKS_TO_DEPLOY[@]}" -eq 0 ]]; then
echo "No stack changes detected — nothing to deploy."
exit 0
fi
# ---------------------------------------------------------------
# Call Komodo DeployStack API for each changed stack
# ---------------------------------------------------------------
echo "=== Triggering Komodo deployments ==="
FAILED=0
for stack in "${STACKS_TO_DEPLOY[@]}"; do
echo -n " → $stack ... "
HTTP_STATUS=$(curl --silent --show-error --output /dev/null \
--write-out "%{http_code}" \
--max-time 30 \
--request POST "${KOMODO_URL}/execute/DeployStack" \
--header "Content-Type: application/json" \
--header "X-Api-Key: ${KOMODO_API_KEY}" \
--header "X-Api-Secret: ${KOMODO_API_SECRET}" \
--data "{\"stack\": \"${stack}\"}")
if [[ "$HTTP_STATUS" =~ ^2 ]]; then
echo "HTTP $HTTP_STATUS OK"
elif [[ "$HTTP_STATUS" == "404" ]]; then
echo "HTTP 404 WARNING — stack not found in Komodo (not yet registered?)"
else
echo "HTTP $HTTP_STATUS FAILED"
FAILED=1
fi
done
echo ""
if [[ "$FAILED" -ne 0 ]]; then
echo "ERROR: One or more Komodo API calls returned a non-2xx status."
exit 1
fi
echo "All deployments triggered successfully."