96 lines
2.8 KiB
YAML
96 lines
2.8 KiB
YAML
name: Deploy on push
|
|
|
|
on:
|
|
push:
|
|
branches: ["main"]
|
|
|
|
jobs:
|
|
detect:
|
|
runs-on: docker2
|
|
outputs:
|
|
swarm_files: ${{ steps.changes.outputs.swarm_files }}
|
|
compose_matrix: ${{ steps.changes.outputs.compose_matrix }}
|
|
steps:
|
|
- name: Checkout
|
|
uses: https://data.forgejo.org/actions/checkout@v4
|
|
with:
|
|
fetch-depth: 0
|
|
|
|
- name: Detect changed YAML files
|
|
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
|
|
|
|
CHANGED="$(git diff --name-only "$BASE" "$HEAD" || true)"
|
|
echo "$CHANGED"
|
|
|
|
# --------------------
|
|
# Swarm stack YAMLs
|
|
# --------------------
|
|
SWARM_FILES="$(echo "$CHANGED" | grep -E '^services/swarm/stacks/.*\.ya?ml$' || true)"
|
|
echo "swarm_files=$(echo "$SWARM_FILES" | xargs)" >> "$GITHUB_OUTPUT"
|
|
|
|
# --------------------
|
|
# Compose YAMLs
|
|
# services/compose/<host>/<service>/<file>.yml
|
|
# --------------------
|
|
COMPOSE_FILES="$(echo "$CHANGED" | grep -E '^services/compose/[^/]+/[^/]+/.*\.ya?ml$' || true)"
|
|
|
|
JSON='{"include":['
|
|
FIRST=1
|
|
while read -r FILE; do
|
|
[ -z "$FILE" ] && continue
|
|
HOST="$(echo "$FILE" | awk -F/ '{print $3}')"
|
|
if [ $FIRST -eq 0 ]; then JSON+=','; fi
|
|
FIRST=0
|
|
JSON+="{\"host\":\"$HOST\",\"file\":\"$FILE\"}"
|
|
done <<< "$COMPOSE_FILES"
|
|
JSON+=']}'
|
|
|
|
echo "compose_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: Validate swarm stacks
|
|
shell: bash
|
|
run: |
|
|
set -euo pipefail
|
|
for f in ${{ needs.detect.outputs.swarm_files }}; do
|
|
docker stack config -c "$f" >/dev/null
|
|
done
|
|
|
|
- name: Deploy swarm stacks
|
|
shell: bash
|
|
run: |
|
|
set -euo pipefail
|
|
for f in ${{ needs.detect.outputs.swarm_files }}; do
|
|
STACK="$(basename "$f" | sed 's/\.ya\?ml$//')"
|
|
docker stack deploy -c "$f" "$STACK"
|
|
done
|
|
|
|
deploy_compose:
|
|
needs: detect
|
|
if: ${{ needs.detect.outputs.compose_matrix != '' }}
|
|
strategy:
|
|
fail-fast: false
|
|
matrix: ${{ fromJSON(needs.detect.outputs.compose_matrix) }}
|
|
runs-on: ${{ matrix.host }}
|
|
steps:
|
|
- name: Checkout
|
|
uses: https://data.forgejo.org/actions/checkout@v4
|
|
|
|
- name: Validate
|