Add Woodpecker CI/CD pipeline for Docker Swarm auto-deploy

This commit is contained in:
admin
2026-06-20 22:20:57 -07:00
parent 3144964222
commit 5413331fc2
+45 -69
View File
@@ -1,98 +1,74 @@
steps:
# Validate all compose files have valid syntax
validate:
image: docker:latest
commands:
- docker-compose config -f git.yaml > /dev/null
- echo "✓ git.yaml syntax valid"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
when:
- event: push
branch: main
# Deploy to Docker Swarm
steps:
validate:
image: alpine:latest
commands:
- apk add --no-cache docker-cli docker-cli-compose git
- |
CHANGED=$(git diff --name-only HEAD~1 HEAD 2>/dev/null | grep '\.yaml$' || ls *.yaml)
for f in $CHANGED; do
[ -f "$f" ] || continue
docker compose -f "$f" config > /dev/null && echo " OK $f" || { echo " FAIL $f"; exit 1; }
done
deploy:
image: alpine:latest
commands:
- apk add --no-cache openssh-client
- apk add --no-cache openssh-client git
- mkdir -p ~/.ssh
- echo "$SSH_KEY" | base64 -d > ~/.ssh/id_rsa
- chmod 600 ~/.ssh/id_rsa
- ssh-keyscan -H docker-2.local >> ~/.ssh/known_hosts 2>/dev/null || true
# Copy compose files to swarm manager
- scp -o StrictHostKeyChecking=no -r *.yaml root@docker-2.local:/tmp/compose/
- scp -o StrictHostKeyChecking=no *.env root@docker-2.local:/tmp/compose/ || true
# Deploy stacks via SSH
- ssh-keyscan -H 192.168.4.32 >> ~/.ssh/known_hosts 2>/dev/null
- rsync -av -e "ssh -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa" *.yaml root@192.168.4.32:/volume1/docker/compose-files/
- |
ssh -o StrictHostKeyChecking=no root@docker-2.local << 'DEPLOY'
cd /tmp/compose
docker stack deploy -c git.yaml git
echo "✓ Git stack deployed"
DEPLOY
secrets:
- ssh_key
when:
branch: main
event: push
CHANGED=$(git diff --name-only HEAD~1 HEAD 2>/dev/null | grep '\.yaml$' | sed 's/\.yaml//' || echo "")
[ -z "$CHANGED" ] && echo "No stacks changed" && exit 0
for STACK in $CHANGED; do
ssh -o StrictHostKeyChecking=no root@192.168.4.32 \
"cd /volume1/docker/compose-files && \
set -a && [ -f ${STACK}.env ] && . ./${STACK}.env || true && set +a && \
docker stack deploy -c ${STACK}.yaml ${STACK}" \
&& echo " OK $STACK" || { echo " FAIL $STACK"; exit 1; }
done
secrets: [ ssh_key ]
# Verify deployment success
verify:
image: alpine:latest
commands:
- apk add --no-cache openssh-client
- apk add --no-cache openssh-client git
- mkdir -p ~/.ssh
- echo "$SSH_KEY" | base64 -d > ~/.ssh/id_rsa
- chmod 600 ~/.ssh/id_rsa
- ssh-keyscan -H docker-2.local >> ~/.ssh/known_hosts 2>/dev/null || true
# Check stack status
- ssh-keyscan -H 192.168.4.32 >> ~/.ssh/known_hosts 2>/dev/null
- |
ssh -o StrictHostKeyChecking=no root@docker-2.local << 'VERIFY'
echo "Git Stack Status:"
docker stack ps git
VERIFY
secrets:
- ssh_key
when:
branch: main
status: success
CHANGED=$(git diff --name-only HEAD~1 HEAD 2>/dev/null | grep '\.yaml$' | sed 's/\.yaml//' || echo "")
[ -z "$CHANGED" ] && exit 0
sleep 5
for STACK in $CHANGED; do
echo "--- $STACK ---"
ssh -o StrictHostKeyChecking=no root@192.168.4.32 \
"docker stack ps $STACK --filter desired-state=running --format ' {{.Name}} {{.CurrentState}}'"
done
secrets: [ ssh_key ]
# Notify Teams on success
notify-success:
image: alpine:latest
commands:
- apk add --no-cache curl
- |
curl -X POST -H 'Content-Type: application/json' \
-d '{
"title": "✅ Compose Deploy Success",
"text": "homelab/compose-files deployed to Docker Swarm",
"themeColor": "00aa00"
}' \
"$TEAMS_WEBHOOK"
secrets:
- teams_webhook
- apk add --no-cache curl git
- CHANGED=$(git diff --name-only HEAD~1 HEAD 2>/dev/null | grep '\.yaml$' | tr '\n' ' ' || echo "unknown")
- curl -sf -X POST -H 'Content-Type: application/json' -d "{\"title\":\"Swarm Deploy OK\",\"text\":\"${CHANGED}\",\"themeColor\":\"00aa00\"}" "$TEAMS_WEBHOOK" || true
secrets: [ teams_webhook ]
when:
branch: main
status: success
- status: success
# Notify Teams on failure
notify-failure:
image: alpine:latest
commands:
- apk add --no-cache curl
- |
curl -X POST -H 'Content-Type: application/json' \
-d '{
"title": "❌ Compose Deploy Failed",
"text": "homelab/compose-files deployment failed",
"themeColor": "ff0000"
}' \
"$TEAMS_WEBHOOK"
secrets:
- teams_webhook
- curl -sf -X POST -H 'Content-Type: application/json' -d "{\"title\":\"Swarm Deploy FAILED\",\"text\":\"${CI_COMMIT_MESSAGE}\",\"themeColor\":\"ff0000\"}" "$TEAMS_WEBHOOK" || true
secrets: [ teams_webhook ]
when:
branch: main
status: failure
- status: failure