name: Deploy on push on: push: branches: ["master"] jobs: deploy_swarm: runs-on: docker2 container: image: docker:27-cli volumes: - /var/run/docker.sock:/var/run/docker.sock steps: - name: Checkout repository shell: sh env: CLONE_URL: ${{ github.server_url }}/${{ github.repository }}.git BRANCH: ${{ github.ref_name }} run: | set -e git clone --branch "$BRANCH" --depth 1 "$CLONE_URL" repo cd repo git checkout -q "${{ github.sha }}" - name: Detect and deploy swarm stacks shell: sh run: | set -e cd repo BASE="${{ github.event.before }}" HEAD="${{ github.sha }}" # Handle first commit or missing base if [ -z "$BASE" ] || [ "$BASE" = "0000000000000000000000000000000000000000" ]; then git fetch --depth 2 origin "$HEAD" BASE="$(git rev-parse "${HEAD}~1" 2>/dev/null || echo "")" else git fetch --depth 1 origin "$BASE" fi if [ -z "$BASE" ]; then echo "No base commit found; skipping swarm deployment." exit 0 fi # Find changed swarm files SWARM_FILES="$(git diff --name-only "$BASE" "$HEAD" | grep -E '^swarm/.*\.ya?ml$' || true)" if [ -z "$SWARM_FILES" ]; then echo "No swarm stack changes detected." exit 0 fi echo "Changed swarm files: $SWARM_FILES" # Validate and deploy each stack for f in $SWARM_FILES; do if [ ! -f "$f" ]; then echo "Warning: $f not found, skipping" continue fi STACK="$(basename "$f" | sed 's/\.ya\?ml$//')" echo "Validating stack: $STACK" docker stack config -c "$f" >/dev/null echo "Deploying stack: $STACK" docker stack deploy -c "$f" "$STACK" done deploy_compose: runs-on: docker2 container: image: docker:27-cli volumes: - /var/run/docker.sock:/var/run/docker.sock steps: - name: Install docker compose shell: sh run: apk add --no-cache docker-cli-compose - name: Checkout repository shell: sh env: CLONE_URL: ${{ github.server_url }}/${{ github.repository }}.git BRANCH: ${{ github.ref_name }} run: | set -e git clone --branch "$BRANCH" --depth 1 "$CLONE_URL" repo cd repo git checkout -q "${{ github.sha }}" - name: Detect and deploy compose files shell: sh run: | set -e cd repo BASE="${{ github.event.before }}" HEAD="${{ github.sha }}" # Handle first commit or missing base if [ -z "$BASE" ] || [ "$BASE" = "0000000000000000000000000000000000000000" ]; then git fetch --depth 2 origin "$HEAD" BASE="$(git rev-parse "${HEAD}~1" 2>/dev/null || echo "")" else git fetch --depth 1 origin "$BASE" fi if [ -z "$BASE" ]; then echo "No base commit found; skipping compose deployment." exit 0 fi # Find changed compose files COMPOSE_FILES="$(git diff --name-only "$BASE" "$HEAD" | grep -E '^services/compose/[^/]+/[^/]+/.*\.ya?ml$' || true)" if [ -z "$COMPOSE_FILES" ]; then echo "No compose file changes detected." exit 0 fi echo "Changed compose files: $COMPOSE_FILES" # Group files by host and deploy echo "$COMPOSE_FILES" | while read -r FILE; do [ -z "$FILE" ] && continue if [ ! -f "$FILE" ]; then echo "Warning: $FILE not found, skipping" continue fi HOST="$(echo "$FILE" | awk -F/ '{print $3}')" # Only deploy if we're on the correct host if [ "$HOST" = "docker2" ] || [ "$HOST" = "$(hostname)" ]; then echo "Validating: $FILE" docker compose -f "$FILE" config -q echo "Deploying: $FILE" docker compose -f "$FILE" pull docker compose -f "$FILE" up -d --remove-orphans else echo "Skipping $FILE (requires host: $HOST)" fi done