forked from Shared/esg
ci(deployment): automate esg development deployment
Deploy latest remote main from a clean app checkout, build the development Compose app, wait for readiness after TypeORM migrations, and notify Rocket.Chat using short commit links.
This commit is contained in:
+179
@@ -0,0 +1,179 @@
|
||||
# Shared/esg — development deployment (Deploy-Develop)
|
||||
# Manual tasks: see ci-cd/TASK.md
|
||||
# Requires: repo marked Trusted in Woodpecker (host volume mounts)
|
||||
|
||||
when:
|
||||
- event: push
|
||||
branch: main
|
||||
- event: manual
|
||||
branch: main
|
||||
|
||||
skip_clone: true
|
||||
|
||||
# Per-run metadata stays outside the host checkout.
|
||||
workspace:
|
||||
base: /woodpecker
|
||||
path: esg-deploy
|
||||
|
||||
variables:
|
||||
- &host_workspace /data/infra-deploy/shared-esg/app
|
||||
- &workspace_volume /data/infra-deploy/shared-esg/app:/workspace
|
||||
- &glue_volume /data/infra-deploy/shared-esg:/data/infra-deploy/shared-esg
|
||||
- &host_ssh /home/talieh/.ssh:/root/.ssh:ro
|
||||
- &pipeline_env
|
||||
WORKSPACE: *host_workspace
|
||||
PROJECT_NAME: ESG
|
||||
ENVIRONMENT: Development
|
||||
APPLICATION_URL: https://esg.ittalie.ir
|
||||
GIT_COMMIT_URL: https://git.ittalie.com/Shared/esg/commit/
|
||||
GIT_BRANCH: main
|
||||
COMPOSE_FILE: compose.dev.yaml
|
||||
COMPOSE_PROJECT_NAME: shared-esg
|
||||
SUCCESS_COLOR: "#36a64f"
|
||||
FAILURE_COLOR: "#dc3545"
|
||||
|
||||
steps:
|
||||
pull:
|
||||
image: docker.arvancloud.ir/alpine/git:latest
|
||||
environment:
|
||||
<<: *pipeline_env
|
||||
GIT_SSH_COMMAND: ssh -o BatchMode=yes -o StrictHostKeyChecking=yes
|
||||
volumes:
|
||||
- *workspace_volume
|
||||
- *host_ssh
|
||||
commands:
|
||||
- git config --global --add safe.directory /workspace
|
||||
- cd /workspace
|
||||
- |
|
||||
set -eu
|
||||
date +%s > /woodpecker/esg-deploy/start
|
||||
if [ "$(git branch --show-current)" != main ]; then
|
||||
echo "App checkout must be on main" >&2
|
||||
exit 1
|
||||
fi
|
||||
if [ -n "$(git status --porcelain --untracked-files=all)" ]; then
|
||||
echo "App checkout has uncommitted changes; commit or move them before retrying" >&2
|
||||
git status --short
|
||||
exit 1
|
||||
fi
|
||||
git fetch origin main
|
||||
if ! git merge-base --is-ancestor HEAD FETCH_HEAD; then
|
||||
echo "Local main has commits outside remote main; reconcile manually" >&2
|
||||
exit 1
|
||||
fi
|
||||
git merge --ff-only FETCH_HEAD
|
||||
git rev-parse HEAD > /woodpecker/esg-deploy/commit
|
||||
git log -3 --pretty=format:'• %s' > /woodpecker/esg-deploy/changes
|
||||
echo "Building latest main at $(cat /woodpecker/esg-deploy/commit)"
|
||||
|
||||
deploy:
|
||||
image: docker.arvancloud.ir/docker:24-cli
|
||||
environment:
|
||||
<<: *pipeline_env
|
||||
volumes:
|
||||
- /var/run/docker.sock:/var/run/docker.sock
|
||||
- *glue_volume
|
||||
commands:
|
||||
- cd /data/infra-deploy/shared-esg
|
||||
- test -d app/.git && test -f app/Dockerfile && test -f .env
|
||||
- docker compose -f compose.dev.yaml config --quiet
|
||||
- docker compose -f compose.dev.yaml build app
|
||||
- docker compose -f compose.dev.yaml up -d --no-deps --wait --wait-timeout 300 app
|
||||
- touch /woodpecker/esg-deploy/deployed
|
||||
|
||||
notify-success:
|
||||
image: docker.arvancloud.ir/alpine:3.20
|
||||
environment:
|
||||
<<: *pipeline_env
|
||||
ROCKETCHAT_WEBHOOK:
|
||||
from_secret: rocketchat_webhook
|
||||
volumes:
|
||||
- *workspace_volume
|
||||
when:
|
||||
- status: success
|
||||
commands:
|
||||
- apk add --no-cache curl jq git > /dev/null
|
||||
- |
|
||||
set -euo pipefail
|
||||
# Reject a token/partial URL without exposing the secret in logs.
|
||||
if ! printf '%s' "$ROCKETCHAT_WEBHOOK" | grep -Eq '^https?://[^/[:space:]]+/hooks/[^/[:space:]]+/[^/[:space:]]+$'; then
|
||||
echo "rocketchat_webhook must contain the complete Rocket.Chat incoming webhook URL (https://host/hooks/id/token)" >&2
|
||||
exit 1
|
||||
fi
|
||||
git config --global --add safe.directory /workspace
|
||||
cd /workspace
|
||||
|
||||
COMMIT_HASH="$(cut -c1-7 /woodpecker/esg-deploy/commit)"
|
||||
DEPLOY_START="$(cat /woodpecker/esg-deploy/start)"
|
||||
DEPLOY_END="$(date +%s)"
|
||||
DEPLOY_DURATION="$((DEPLOY_END - DEPLOY_START))"
|
||||
|
||||
CHANGES="$(cat /woodpecker/esg-deploy/changes)"
|
||||
|
||||
TITLE="✅ $PROJECT_NAME - $ENVIRONMENT ✅"
|
||||
TEXT="🌐 **URL**: $APPLICATION_URL
|
||||
|
||||
🔖 **Commit Hash**: [$COMMIT_HASH]($GIT_COMMIT_URL$COMMIT_HASH)
|
||||
|
||||
📝 **Recent Changes**:
|
||||
|
||||
$CHANGES
|
||||
|
||||
⏱️ **Deployment Duration**: $${DEPLOY_DURATION}s"
|
||||
|
||||
payload="$(jq -n \
|
||||
--arg title "$TITLE" \
|
||||
--arg text "$TEXT" \
|
||||
--arg color "$SUCCESS_COLOR" \
|
||||
'{text: $title, attachments: [{text: $text, color: $color}]}')"
|
||||
|
||||
curl -fsS --connect-timeout 10 --max-time 30 -H "Content-Type: application/json" -d "$payload" "$ROCKETCHAT_WEBHOOK" >/dev/null
|
||||
|
||||
notify-failure:
|
||||
image: docker.arvancloud.ir/alpine:3.20
|
||||
environment:
|
||||
<<: *pipeline_env
|
||||
ROCKETCHAT_WEBHOOK:
|
||||
from_secret: rocketchat_webhook
|
||||
volumes:
|
||||
- *workspace_volume
|
||||
when:
|
||||
- status: failure
|
||||
commands:
|
||||
- apk add --no-cache curl jq git > /dev/null
|
||||
- |
|
||||
set -euo pipefail
|
||||
if [ -f /woodpecker/esg-deploy/deployed ]; then
|
||||
echo "Deployment succeeded; see notify-success for notification delivery failure" >&2
|
||||
exit 0
|
||||
fi
|
||||
# Reject a token/partial URL without exposing the secret in logs.
|
||||
if ! printf '%s' "$ROCKETCHAT_WEBHOOK" | grep -Eq '^https?://[^/[:space:]]+/hooks/[^/[:space:]]+/[^/[:space:]]+$'; then
|
||||
echo "rocketchat_webhook must contain the complete Rocket.Chat incoming webhook URL (https://host/hooks/id/token)" >&2
|
||||
exit 1
|
||||
fi
|
||||
git config --global --add safe.directory /workspace
|
||||
cd /workspace
|
||||
|
||||
COMMIT_HASH="$(cat /woodpecker/esg-deploy/commit 2>/dev/null || echo not-selected)"
|
||||
TITLE="💥 $PROJECT_NAME - $ENVIRONMENT 💥"
|
||||
TEXT="━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||||
🌐 **Application URL**
|
||||
|
||||
$APPLICATION_URL
|
||||
|
||||
🔖 **Commit**
|
||||
|
||||
\`$COMMIT_HASH\`
|
||||
|
||||
⚠️ **Pipeline failed** — check Woodpecker for the failing step.
|
||||
Commit above is the selected build revision, not a claim of successful deployment.
|
||||
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
|
||||
|
||||
payload="$(jq -n \
|
||||
--arg title "$TITLE" \
|
||||
--arg text "$TEXT" \
|
||||
--arg color "$FAILURE_COLOR" \
|
||||
'{text: $title, attachments: [{text: $text, color: $color}]}')"
|
||||
|
||||
curl -fsS --connect-timeout 10 --max-time 30 -H "Content-Type: application/json" -d "$payload" "$ROCKETCHAT_WEBHOOK" >/dev/null
|
||||
Reference in New Issue
Block a user