Add Woodpecker CI/CD pipeline for Docker Swarm auto-deploy
This commit is contained in:
+46
-70
@@ -1,98 +1,74 @@
|
|||||||
steps:
|
when:
|
||||||
# Validate all compose files have valid syntax
|
- event: push
|
||||||
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:
|
|
||||||
branch: main
|
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:
|
deploy:
|
||||||
image: alpine:latest
|
image: alpine:latest
|
||||||
commands:
|
commands:
|
||||||
- apk add --no-cache openssh-client
|
- apk add --no-cache openssh-client git
|
||||||
- mkdir -p ~/.ssh
|
- mkdir -p ~/.ssh
|
||||||
- echo "$SSH_KEY" | base64 -d > ~/.ssh/id_rsa
|
- echo "$SSH_KEY" | base64 -d > ~/.ssh/id_rsa
|
||||||
- chmod 600 ~/.ssh/id_rsa
|
- chmod 600 ~/.ssh/id_rsa
|
||||||
- ssh-keyscan -H docker-2.local >> ~/.ssh/known_hosts 2>/dev/null || true
|
- 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/
|
||||||
# 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 -o StrictHostKeyChecking=no root@docker-2.local << 'DEPLOY'
|
CHANGED=$(git diff --name-only HEAD~1 HEAD 2>/dev/null | grep '\.yaml$' | sed 's/\.yaml//' || echo "")
|
||||||
cd /tmp/compose
|
[ -z "$CHANGED" ] && echo "No stacks changed" && exit 0
|
||||||
docker stack deploy -c git.yaml git
|
for STACK in $CHANGED; do
|
||||||
echo "✓ Git stack deployed"
|
ssh -o StrictHostKeyChecking=no root@192.168.4.32 \
|
||||||
DEPLOY
|
"cd /volume1/docker/compose-files && \
|
||||||
secrets:
|
set -a && [ -f ${STACK}.env ] && . ./${STACK}.env || true && set +a && \
|
||||||
- ssh_key
|
docker stack deploy -c ${STACK}.yaml ${STACK}" \
|
||||||
when:
|
&& echo " OK $STACK" || { echo " FAIL $STACK"; exit 1; }
|
||||||
branch: main
|
done
|
||||||
event: push
|
secrets: [ ssh_key ]
|
||||||
|
|
||||||
# Verify deployment success
|
|
||||||
verify:
|
verify:
|
||||||
image: alpine:latest
|
image: alpine:latest
|
||||||
commands:
|
commands:
|
||||||
- apk add --no-cache openssh-client
|
- apk add --no-cache openssh-client git
|
||||||
- mkdir -p ~/.ssh
|
- mkdir -p ~/.ssh
|
||||||
- echo "$SSH_KEY" | base64 -d > ~/.ssh/id_rsa
|
- echo "$SSH_KEY" | base64 -d > ~/.ssh/id_rsa
|
||||||
- chmod 600 ~/.ssh/id_rsa
|
- chmod 600 ~/.ssh/id_rsa
|
||||||
- ssh-keyscan -H docker-2.local >> ~/.ssh/known_hosts 2>/dev/null || true
|
- ssh-keyscan -H 192.168.4.32 >> ~/.ssh/known_hosts 2>/dev/null
|
||||||
|
|
||||||
# Check stack status
|
|
||||||
- |
|
- |
|
||||||
ssh -o StrictHostKeyChecking=no root@docker-2.local << 'VERIFY'
|
CHANGED=$(git diff --name-only HEAD~1 HEAD 2>/dev/null | grep '\.yaml$' | sed 's/\.yaml//' || echo "")
|
||||||
echo "Git Stack Status:"
|
[ -z "$CHANGED" ] && exit 0
|
||||||
docker stack ps git
|
sleep 5
|
||||||
VERIFY
|
for STACK in $CHANGED; do
|
||||||
secrets:
|
echo "--- $STACK ---"
|
||||||
- ssh_key
|
ssh -o StrictHostKeyChecking=no root@192.168.4.32 \
|
||||||
when:
|
"docker stack ps $STACK --filter desired-state=running --format ' {{.Name}} {{.CurrentState}}'"
|
||||||
branch: main
|
done
|
||||||
status: success
|
secrets: [ ssh_key ]
|
||||||
|
|
||||||
# Notify Teams on success
|
|
||||||
notify-success:
|
notify-success:
|
||||||
image: alpine:latest
|
image: alpine:latest
|
||||||
commands:
|
commands:
|
||||||
- apk add --no-cache curl
|
- apk add --no-cache curl git
|
||||||
- |
|
- CHANGED=$(git diff --name-only HEAD~1 HEAD 2>/dev/null | grep '\.yaml$' | tr '\n' ' ' || echo "unknown")
|
||||||
curl -X POST -H 'Content-Type: application/json' \
|
- curl -sf -X POST -H 'Content-Type: application/json' -d "{\"title\":\"Swarm Deploy OK\",\"text\":\"${CHANGED}\",\"themeColor\":\"00aa00\"}" "$TEAMS_WEBHOOK" || true
|
||||||
-d '{
|
secrets: [ teams_webhook ]
|
||||||
"title": "✅ Compose Deploy Success",
|
|
||||||
"text": "homelab/compose-files deployed to Docker Swarm",
|
|
||||||
"themeColor": "00aa00"
|
|
||||||
}' \
|
|
||||||
"$TEAMS_WEBHOOK"
|
|
||||||
secrets:
|
|
||||||
- teams_webhook
|
|
||||||
when:
|
when:
|
||||||
branch: main
|
- status: success
|
||||||
status: success
|
|
||||||
|
|
||||||
# Notify Teams on failure
|
|
||||||
notify-failure:
|
notify-failure:
|
||||||
image: alpine:latest
|
image: alpine:latest
|
||||||
commands:
|
commands:
|
||||||
- apk add --no-cache curl
|
- apk add --no-cache curl
|
||||||
- |
|
- curl -sf -X POST -H 'Content-Type: application/json' -d "{\"title\":\"Swarm Deploy FAILED\",\"text\":\"${CI_COMMIT_MESSAGE}\",\"themeColor\":\"ff0000\"}" "$TEAMS_WEBHOOK" || true
|
||||||
curl -X POST -H 'Content-Type: application/json' \
|
secrets: [ teams_webhook ]
|
||||||
-d '{
|
|
||||||
"title": "❌ Compose Deploy Failed",
|
|
||||||
"text": "homelab/compose-files deployment failed",
|
|
||||||
"themeColor": "ff0000"
|
|
||||||
}' \
|
|
||||||
"$TEAMS_WEBHOOK"
|
|
||||||
secrets:
|
|
||||||
- teams_webhook
|
|
||||||
when:
|
when:
|
||||||
branch: main
|
- status: failure
|
||||||
status: failure
|
|
||||||
|
|||||||
Reference in New Issue
Block a user