139 lines
4.3 KiB
YAML
139 lines
4.3 KiB
YAML
name: Deploy on push
|
|
|
|
on:
|
|
push:
|
|
branches: ["main"]
|
|
|
|
jobs:
|
|
detect:
|
|
runs-on: docker2
|
|
outputs:
|
|
swarm_files: ${{ steps.changes.outputs.swarm_files }}
|
|
compose_files_matrix: ${{ steps.changes.outputs.compose_files_matrix }}
|
|
steps:
|
|
- name: Checkout
|
|
uses: https://data.forgejo.org/actions/checkout@v4
|
|
with:
|
|
fetch-depth: 0
|
|
|
|
- name: Detect changed YAML deploy targets (your naming)
|
|
id: changes
|
|
shell: bash
|
|
run: |
|
|
set -euo pipefail
|
|
|
|
BASE="${{ github.event.before }}"
|
|
HEAD="${{ github.sha }}"
|
|
|
|
if [ -z "${BASE}" ] || [ "${BASE}" = "0000000000000000000000000000000000000000" ]; then
|
|
BASE="$(git rev-parse "${HEAD}~1" || true)"
|
|
fi
|
|
if [ -z "${BASE}" ]; then
|
|
echo "No base commit found; deploying nothing."
|
|
echo "swarm_files=" >> "$GITHUB_OUTPUT"
|
|
echo 'compose_files_matrix={"include":[]}' >> "$GITHUB_OUTPUT"
|
|
exit 0
|
|
fi
|
|
|
|
CHANGED="$(git diff --name-only "$BASE" "$HEAD" || true)"
|
|
echo "$CHANGED"
|
|
|
|
# Swarm: only changed stack YAMLs
|
|
SWARM_FILES="$(echo "$CHANGED" | grep -E '^services/swarm/stacks/.*\.ya?ml$' || true)"
|
|
SWARM_FILES_ONE_LINE="$(echo "$SWARM_FILES" | tr '\n' ' ' | xargs || true)"
|
|
echo "swarm_files=$SWARM_FILES_ONE_LINE" >> "$GITHUB_OUTPUT"
|
|
|
|
# Compose: any changed YAML under services/compose/<host>/<service>/
|
|
COMPOSE_FILES="$(
|
|
echo "$CHANGED" \
|
|
| grep -E '^services/compose/[^/]+/[^/]+/.*\.ya?ml$' \
|
|
| sort -u \
|
|
|| true
|
|
)"
|
|
|
|
# Build matrix items: {"host":"nas","file":"services/compose/nas/foo/foo.yaml"}
|
|
JSON='{"include":['
|
|
FIRST=1
|
|
while read -r F; do
|
|
[ -z "$F" ] && continue
|
|
HOST="$(echo "$F" | awk -F/ '{print $3}')"
|
|
if [ $FIRST -eq 1 ]; then
|
|
FIRST=0
|
|
else
|
|
JSON+=","
|
|
fi
|
|
JSON+="{\"host\":\"$HOST\",\"file\":\"$F\"}"
|
|
done <<< "$COMPOSE_FILES"
|
|
JSON+=']}'
|
|
|
|
echo "compose_files_matrix=$JSON" >> "$GITHUB_OUTPUT"
|
|
|
|
deploy_swarm:
|
|
needs: detect
|
|
if: ${{ needs.detect.outputs.swarm_files != '' }}
|
|
runs-on: docker2
|
|
steps:
|
|
- name: Checkout
|
|
uses: https://data.forgejo.org/actions/checkout@v4
|
|
|
|
- name: Pre-flight validate swarm stacks
|
|
shell: bash
|
|
run: |
|
|
set -euo pipefail
|
|
for f in ${{ needs.detect.outputs.swarm_files }}; do
|
|
echo "Validating swarm stack file: $f"
|
|
docker stack config -c "$f" > /dev/null
|
|
done
|
|
|
|
- name: Deploy changed swarm stacks
|
|
shell: bash
|
|
run: |
|
|
set -euo pipefail
|
|
for f in ${{ needs.detect.outputs.swarm_files }}; do
|
|
echo "Deploying swarm stack file: $f"
|
|
STACK="$(basename "$f")"
|
|
STACK="${STACK%.yml}"
|
|
STACK="${STACK%.yaml}"
|
|
docker stack deploy -c "$f" "$STACK"
|
|
done
|
|
|
|
deploy_compose:
|
|
needs: detect
|
|
if: ${{ needs.detect.outputs.compose_files_matrix != '' }}
|
|
strategy:
|
|
fail-fast: false
|
|
matrix: ${{ fromJSON(needs.detect.outputs.compose_files_matrix) }}
|
|
runs-on: docker
|
|
steps:
|
|
- name: Checkout
|
|
uses: https://data.forgejo.org/actions/checkout@v4
|
|
|
|
- name: Ensure we are on the right host
|
|
shell: bash
|
|
run: |
|
|
set -euo pipefail
|
|
WANT="${{ matrix.host }}"
|
|
HAVE="$(hostname)"
|
|
echo "Want host: $WANT"
|
|
echo "Have host: $HAVE"
|
|
if [ "$WANT" != "$HAVE" ]; then
|
|
echo "This runner is on $HAVE but job wants $WANT. Failing to avoid deploying on wrong host."
|
|
exit 1
|
|
fi
|
|
|
|
- name: Pre-flight validate compose file
|
|
shell: bash
|
|
run: |
|
|
set -euo pipefail
|
|
F="${{ matrix.file }}"
|
|
echo "Validating compose file: $F"
|
|
docker compose -f "$F" config -q
|
|
|
|
- name: Deploy compose file
|
|
shell: bash
|
|
run: |
|
|
set -euo pipefail
|
|
F="${{ matrix.file }}"
|
|
echo "Deploying compose file: $F"
|
|
docker compose -f "$F" pull
|
|
docker compose -f "$F" up -d --remove-orphans
|