fixed deploy.yml

This commit is contained in:
traveler 2026-01-11 18:26:55 -06:00
parent 229b68ef09
commit 3364e08969

View file

@ -1,4 +1,3 @@
name: Deploy on push name: Deploy on push
on: on:
@ -10,14 +9,14 @@ jobs:
runs-on: docker2 runs-on: docker2
outputs: outputs:
swarm_files: ${{ steps.changes.outputs.swarm_files }} swarm_files: ${{ steps.changes.outputs.swarm_files }}
compose_matrix: ${{ steps.changes.outputs.compose_matrix }} compose_files_matrix: ${{ steps.changes.outputs.compose_files_matrix }}
steps: steps:
- name: Checkout - name: Checkout
uses: https://data.forgejo.org/actions/checkout@v4 uses: https://data.forgejo.org/actions/checkout@v4
with: with:
fetch-depth: 0 fetch-depth: 0
- name: Detect changed YAML deploy targets (YAML only) - name: Detect changed YAML deploy targets (your naming)
id: changes id: changes
shell: bash shell: bash
run: | run: |
@ -32,7 +31,7 @@ jobs:
if [ -z "${BASE}" ]; then if [ -z "${BASE}" ]; then
echo "No base commit found; deploying nothing." echo "No base commit found; deploying nothing."
echo "swarm_files=" >> "$GITHUB_OUTPUT" echo "swarm_files=" >> "$GITHUB_OUTPUT"
echo 'compose_matrix={"include":[]}' >> "$GITHUB_OUTPUT" echo 'compose_files_matrix={"include":[]}' >> "$GITHUB_OUTPUT"
exit 0 exit 0
fi fi
@ -44,36 +43,30 @@ jobs:
SWARM_FILES_ONE_LINE="$(echo "$SWARM_FILES" | tr '\n' ' ' | xargs || true)" SWARM_FILES_ONE_LINE="$(echo "$SWARM_FILES" | tr '\n' ' ' | xargs || true)"
echo "swarm_files=$SWARM_FILES_ONE_LINE" >> "$GITHUB_OUTPUT" echo "swarm_files=$SWARM_FILES_ONE_LINE" >> "$GITHUB_OUTPUT"
# Compose: ONLY if compose YAML file changed inside a service dir # Compose: any changed YAML under services/compose/<host>/<service>/
COMPOSE_FILES="$( COMPOSE_FILES="$(
echo "$CHANGED" \ echo "$CHANGED" \
| grep -E '^services/compose/[^/]+/[^/]+/(compose|docker-compose)\.ya?ml$' \ | grep -E '^services/compose/[^/]+/[^/]+/.*\.ya?ml$' \
| sort -u \
|| true
)"
COMPOSE_DIRS="$(
echo "$COMPOSE_FILES" \
| awk -F/ '{print $1"/"$2"/"$3"/"$4}' \
| sort -u \ | sort -u \
|| true || true
)" )"
# Build matrix items: {"host":"nas","file":"services/compose/nas/foo/foo.yaml"}
JSON='{"include":[' JSON='{"include":['
FIRST=1 FIRST=1
while read -r DIR; do while read -r F; do
[ -z "$DIR" ] && continue [ -z "$F" ] && continue
HOST="$(echo "$DIR" | awk -F/ '{print $3}')" HOST="$(echo "$F" | awk -F/ '{print $3}')"
if [ $FIRST -eq 1 ]; then if [ $FIRST -eq 1 ]; then
FIRST=0 FIRST=0
else else
JSON+="," JSON+=","
fi fi
JSON+="{\"host\":\"$HOST\",\"dir\":\"$DIR\"}" JSON+="{\"host\":\"$HOST\",\"file\":\"$F\"}"
done <<< "$COMPOSE_DIRS" done <<< "$COMPOSE_FILES"
JSON+=']}' JSON+=']}'
echo "compose_matrix=$JSON" >> "$GITHUB_OUTPUT" echo "compose_files_matrix=$JSON" >> "$GITHUB_OUTPUT"
deploy_swarm: deploy_swarm:
needs: detect needs: detect
@ -106,54 +99,41 @@ jobs:
deploy_compose: deploy_compose:
needs: detect needs: detect
if: ${{ needs.detect.outputs.compose_matrix != '' }} if: ${{ needs.detect.outputs.compose_files_matrix != '' }}
strategy: strategy:
fail-fast: false fail-fast: false
matrix: ${{ fromJSON(needs.detect.outputs.compose_matrix) }} matrix: ${{ fromJSON(needs.detect.outputs.compose_files_matrix) }}
runs-on: ${{ matrix.host }} runs-on: docker
steps: steps:
- name: Checkout - name: Checkout
uses: https://data.forgejo.org/actions/checkout@v4 uses: https://data.forgejo.org/actions/checkout@v4
- name: Pre-flight validate compose service - name: Ensure we are on the right host
shell: bash shell: bash
run: | run: |
set -euo pipefail set -euo pipefail
WANT="${{ matrix.host }}"
DIR="${{ matrix.dir }}" HAVE="$(hostname)"
if [ -f "$DIR/compose.yml" ]; then echo "Want host: $WANT"
F="$DIR/compose.yml" echo "Have host: $HAVE"
elif [ -f "$DIR/compose.yaml" ]; then if [ "$WANT" != "$HAVE" ]; then
F="$DIR/compose.yaml" echo "This runner is on $HAVE but job wants $WANT. Failing to avoid deploying on wrong host."
elif [ -f "$DIR/docker-compose.yml" ]; then
F="$DIR/docker-compose.yml"
elif [ -f "$DIR/docker-compose.yaml" ]; then
F="$DIR/docker-compose.yaml"
else
echo "No compose file found in $DIR"
exit 1 exit 1
fi 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 docker compose -f "$F" config -q
- name: Deploy compose service - name: Deploy compose file
shell: bash shell: bash
run: | run: |
set -euo pipefail set -euo pipefail
F="${{ matrix.file }}"
DIR="${{ matrix.dir }}" echo "Deploying compose file: $F"
if [ -f "$DIR/compose.yml" ]; then
F="$DIR/compose.yml"
elif [ -f "$DIR/compose.yaml" ]; then
F="$DIR/compose.yaml"
elif [ -f "$DIR/docker-compose.yml" ]; then
F="$DIR/docker-compose.yml"
elif [ -f "$DIR/docker-compose.yaml" ]; then
F="$DIR/docker-compose.yaml"
else
echo "No compose file found in $DIR"
exit 1
fi
docker compose -f "$F" pull docker compose -f "$F" pull
docker compose -f "$F" up -d --remove-orphans docker compose -f "$F" up -d --remove-orphans