159 lines
4.9 KiB
YAML
159 lines
4.9 KiB
YAML
name: Deploy on push
|
|
|
|
on:
|
|
push:
|
|
branches: ["master"]
|
|
|
|
jobs:
|
|
detect:
|
|
runs-on: docker2
|
|
outputs:
|
|
swarm_files: ${{ steps.changes.outputs.swarm_files }}
|
|
compose_matrix: ${{ steps.changes.outputs.compose_matrix }}
|
|
steps:
|
|
- name: Checkout repository (git clone, no node)
|
|
shell: bash
|
|
env:
|
|
CLONE_URL: ${{ github.server_url }}/${{ github.repository }}.git
|
|
BRANCH: ${{ github.ref_name }}
|
|
run: |
|
|
set -euo pipefail
|
|
rm -rf repo
|
|
git clone --branch "$BRANCH" --depth 50 "$CLONE_URL" repo
|
|
cd repo
|
|
git fetch --depth 50 origin "${{ github.sha }}"
|
|
git checkout -q "${{ github.sha }}"
|
|
|
|
- name: Detect changed YAML files
|
|
id: changes
|
|
shell: bash
|
|
run: |
|
|
set -euo pipefail
|
|
cd repo
|
|
|
|
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_matrix={"include":[]}' >> "$GITHUB_OUTPUT"
|
|
exit 0
|
|
fi
|
|
|
|
CHANGED="$(git diff --name-only "$BASE" "$HEAD" || true)"
|
|
echo "Changed files:"
|
|
echo "$CHANGED"
|
|
|
|
# Swarm stacks live in: swarm/*.yml|yaml
|
|
SWARM_FILES="$(echo "$CHANGED" | grep -E '^swarm/.*\.ya?ml$' || true)"
|
|
echo "swarm_files=$(echo "$SWARM_FILES" | xargs)" >> "$GITHUB_OUTPUT"
|
|
|
|
# Compose YAMLs live in: services/compose/<host>/<service>/<file>.yml|yaml
|
|
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"
|
|
echo "compose_matrix=$JSON"
|
|
|
|
deploy_swarm:
|
|
needs: detect
|
|
if: ${{ needs.detect.outputs.swarm_files != '' }}
|
|
runs-on: docker2
|
|
container:
|
|
image: docker:27-cli
|
|
steps:
|
|
- name: Checkout repository (git clone, no node)
|
|
shell: sh
|
|
env:
|
|
CLONE_URL: ${{ github.server_url }}/${{ github.repository }}.git
|
|
BRANCH: ${{ github.ref_name }}
|
|
run: |
|
|
set -e
|
|
rm -rf repo
|
|
git clone --branch "$BRANCH" --depth 50 "$CLONE_URL" repo
|
|
cd repo
|
|
git fetch --depth 50 origin "${{ github.sha }}"
|
|
git checkout -q "${{ github.sha }}"
|
|
|
|
- name: Validate swarm stacks
|
|
shell: sh
|
|
run: |
|
|
set -e
|
|
cd repo
|
|
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 swarm stacks
|
|
shell: sh
|
|
run: |
|
|
set -e
|
|
cd repo
|
|
for f in ${{ needs.detect.outputs.swarm_files }}; do
|
|
STACK="$(basename "$f" | sed 's/\.ya\?ml$//')"
|
|
echo "Deploying swarm stack: $STACK from $f"
|
|
docker stack deploy -c "$f" "$STACK"
|
|
done
|
|
|
|
deploy_compose:
|
|
needs: detect
|
|
if: ${{ contains(needs.detect.outputs.compose_matrix, '"include":[{') }}
|
|
|
|
strategy:
|
|
fail-fast: false
|
|
matrix: ${{ fromJSON(needs.detect.outputs.compose_matrix) }}
|
|
runs-on: ${{ matrix.host }}
|
|
container:
|
|
image: docker:27-cli
|
|
steps:
|
|
- name: Install docker compose plugin
|
|
shell: sh
|
|
run: |
|
|
set -e
|
|
apk add --no-cache docker-cli-compose
|
|
|
|
- name: Checkout repository (git clone, no node)
|
|
shell: sh
|
|
env:
|
|
CLONE_URL: ${{ github.server_url }}/${{ github.repository }}.git
|
|
BRANCH: ${{ github.ref_name }}
|
|
run: |
|
|
set -e
|
|
rm -rf repo
|
|
git clone --branch "$BRANCH" --depth 50 "$CLONE_URL" repo
|
|
cd repo
|
|
git fetch --depth 50 origin "${{ github.sha }}"
|
|
git checkout -q "${{ github.sha }}"
|
|
|
|
- name: Validate compose file
|
|
shell: sh
|
|
run: |
|
|
set -e
|
|
cd repo
|
|
echo "Validating compose file: ${{ matrix.file }}"
|
|
docker compose -f "${{ matrix.file }}" config -q
|
|
|
|
- name: Deploy compose file
|
|
shell: sh
|
|
run: |
|
|
set -e
|
|
cd repo
|
|
echo "Deploying compose file: ${{ matrix.file }}"
|
|
docker compose -f "${{ matrix.file }}" pull
|
|
docker compose -f "${{ matrix.file }}" up -d --remove-orphans
|