Fix pipeline: use CI_COMMIT_CHANGED_FILES, --no-interpolate validation, deploy only changed stacks
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
admin
2026-06-20 22:50:42 -07:00
parent a38c652a61
commit 57761aa96f
+31 -21
View File
@@ -6,31 +6,39 @@ steps:
validate: validate:
image: alpine:latest image: alpine:latest
commands: commands:
- apk add --no-cache docker-cli docker-cli-compose git - apk add --no-cache docker-cli docker-cli-compose
- | - |
CHANGED=$(git diff --name-only HEAD~1 HEAD 2>/dev/null | grep '\.yaml$' || ls *.yaml) echo "Changed files: ${CI_COMMIT_CHANGED_FILES}"
for f in $CHANGED; do YAML_FILES=$(echo "${CI_COMMIT_CHANGED_FILES}" | tr ',' '\n' | grep '\.yaml$' || true)
[ -z "$YAML_FILES" ] && echo "No yaml files changed, skipping" && exit 0
for f in $YAML_FILES; do
[ -f "$f" ] || continue [ -f "$f" ] || continue
docker compose -f "$f" config > /dev/null && echo " OK $f" || { echo " FAIL $f"; exit 1; } echo "Validating $f..."
docker compose -f "$f" --no-interpolate config -q \
&& echo " OK $f" \
|| { echo " FAIL $f"; exit 1; }
done done
deploy: deploy:
image: alpine:latest image: alpine:latest
commands: commands:
- apk add --no-cache openssh-client git - apk add --no-cache openssh-client rsync
- 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 192.168.4.32 >> ~/.ssh/known_hosts 2>/dev/null - 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/
- | - |
CHANGED=$(git diff --name-only HEAD~1 HEAD 2>/dev/null | grep '\.yaml$' | sed 's/\.yaml//' || echo "") YAML_FILES=$(echo "${CI_COMMIT_CHANGED_FILES}" | tr ',' '\n' | grep '\.yaml$' || true)
[ -z "$CHANGED" ] && echo "No stacks changed" && exit 0 [ -z "$YAML_FILES" ] && echo "No yaml files changed" && exit 0
for STACK in $CHANGED; do for f in $YAML_FILES; do
[ -f "$f" ] || continue
STACK=$(basename "$f" .yaml)
rsync -av -e "ssh -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa" \
"$f" root@192.168.4.32:/volume1/docker/compose-files/
ssh -o StrictHostKeyChecking=no root@192.168.4.32 \ ssh -o StrictHostKeyChecking=no root@192.168.4.32 \
"cd /volume1/docker/compose-files && \ "cd /volume1/docker/compose-files \
set -a && [ -f ${STACK}.env ] && . ./${STACK}.env || true && set +a && \ && set -a && [ -f ${STACK}.env ] && . ./${STACK}.env; set +a \
docker stack deploy -c ${STACK}.yaml ${STACK}" \ && docker stack deploy -c ${STACK}.yaml ${STACK}" \
&& echo " OK $STACK" || { echo " FAIL $STACK"; exit 1; } && echo " OK $STACK" || { echo " FAIL $STACK"; exit 1; }
done done
secrets: [ ssh_key ] secrets: [ ssh_key ]
@@ -38,29 +46,31 @@ steps:
verify: verify:
image: alpine:latest image: alpine:latest
commands: commands:
- apk add --no-cache openssh-client git - apk add --no-cache openssh-client
- 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 192.168.4.32 >> ~/.ssh/known_hosts 2>/dev/null - ssh-keyscan -H 192.168.4.32 >> ~/.ssh/known_hosts 2>/dev/null
- | - |
CHANGED=$(git diff --name-only HEAD~1 HEAD 2>/dev/null | grep '\.yaml$' | sed 's/\.yaml//' || echo "") YAML_FILES=$(echo "${CI_COMMIT_CHANGED_FILES}" | tr ',' '\n' | grep '\.yaml$' || true)
[ -z "$CHANGED" ] && exit 0 [ -z "$YAML_FILES" ] && exit 0
sleep 5 sleep 5
for STACK in $CHANGED; do for f in $YAML_FILES; do
STACK=$(basename "$f" .yaml)
echo "--- $STACK ---" echo "--- $STACK ---"
ssh -o StrictHostKeyChecking=no root@192.168.4.32 \ ssh -o StrictHostKeyChecking=no root@192.168.4.32 \
"docker stack ps $STACK --filter desired-state=running --format ' {{.Name}} {{.CurrentState}}'" "docker stack ps $STACK --filter desired-state=running \
--format ' {{.Name}} {{.CurrentState}}'"
done done
secrets: [ ssh_key ] secrets: [ ssh_key ]
notify-success: notify-success:
image: alpine:latest image: alpine:latest
commands: commands:
- apk add --no-cache curl git - apk add --no-cache curl
- | - |
CHANGED=$(git diff --name-only HEAD~1 HEAD 2>/dev/null | grep '\.yaml$' | tr '\n' ' ' || echo "unknown") CHANGED=$(echo "${CI_COMMIT_CHANGED_FILES}" | tr ',' ' ')
PAYLOAD=$(printf '{"title":"Swarm Deploy OK","text":"%s","themeColor":"00aa00"}' "$CHANGED") PAYLOAD=$(printf '{"title":"Swarm Deploy OK","text":"Stacks: %s Commit: %s","themeColor":"00aa00"}' "$CHANGED" "$CI_COMMIT_MESSAGE")
curl -sf -X POST -H 'Content-Type: application/json' -d "$PAYLOAD" "$TEAMS_WEBHOOK" || true curl -sf -X POST -H 'Content-Type: application/json' -d "$PAYLOAD" "$TEAMS_WEBHOOK" || true
secrets: [ teams_webhook ] secrets: [ teams_webhook ]
when: when:
@@ -71,7 +81,7 @@ steps:
commands: commands:
- apk add --no-cache curl - apk add --no-cache curl
- | - |
PAYLOAD=$(printf '{"title":"Swarm Deploy FAILED","text":"%s","themeColor":"ff0000"}' "$CI_COMMIT_MESSAGE") PAYLOAD=$(printf '{"title":"Swarm Deploy FAILED","text":"Commit: %s See: https://woodpecker.bryanmail.net","themeColor":"ff0000"}' "$CI_COMMIT_MESSAGE")
curl -sf -X POST -H 'Content-Type: application/json' -d "$PAYLOAD" "$TEAMS_WEBHOOK" || true curl -sf -X POST -H 'Content-Type: application/json' -d "$PAYLOAD" "$TEAMS_WEBHOOK" || true
secrets: [ teams_webhook ] secrets: [ teams_webhook ]
when: when: