From 9ee9081b1170ba02e44d53033648562707dd7b68 Mon Sep 17 00:00:00 2001 From: Sina Alikhani Date: Sat, 17 Oct 2026 16:42:01 +0330 Subject: [PATCH] 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. --- .woodpecker.yml | 179 ++++++++++++++++++++++++++++++++++++++++++++++++ ci-cd/TASK.md | 53 ++++++++++++++ 2 files changed, 232 insertions(+) create mode 100644 .woodpecker.yml create mode 100644 ci-cd/TASK.md diff --git a/.woodpecker.yml b/.woodpecker.yml new file mode 100644 index 0000000..4488d77 --- /dev/null +++ b/.woodpecker.yml @@ -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 diff --git a/ci-cd/TASK.md b/ci-cd/TASK.md new file mode 100644 index 0000000..4817a12 --- /dev/null +++ b/ci-cd/TASK.md @@ -0,0 +1,53 @@ +# ESG development CI + +Workflow: `.woodpecker.yml` in `Shared/esg`. +Pushes to main and manual runs on main fetch and deploy latest remote main. +The selected revision is saved in the per-run workspace; notifications use +seven-character commit links and may differ from the triggering commit. + +## Enable once + +1. Update `/data/infra-deploy/shared-esg/app` to include this workflow. + The checkout must be on main and clean. Resolve local changes explicitly; + CI does not discard or stash them and refuses locally advanced/divergent main. +2. Enable `Shared/esg` in Woodpecker with `.woodpecker.yml` as the workflow path. + An administrator must enable **Trusted volumes** for host mounts/socket. +3. Use the Deploy-Develop agent with `WOODPECKER_MAX_WORKFLOWS=1` and repository + concurrency 1. With multiple agents, configure labels to select this host. + Let active deployments finish instead of automatically canceling on new pushes. +4. Configure `rocketchat_webhook` with the complete incoming webhook URL for the + existing deployment channel. No quotes, Markdown, or whitespace. +5. Ensure `/home/talieh/.ssh` can fetch Shared/esg and known_hosts trusts + git.ittalie.com. CI mounts SSH read-only with strict host checking. +6. Run a manual workflow on main and inspect pull, deploy and notification logs. + +## Runtime + +Glue: `/data/infra-deploy/shared-esg`, Compose project: `shared-esg`. +Required files: `compose.dev.yaml`, populated `.env`, and `app/Dockerfile`. +Secrets stay in the parent glue `.env`, outside the app checkout. +The Dockerfile's `production` target is used by the development Compose stack; +it is a build-stage name, not a customer production deployment. + +Only `app` is built and deployed. TypeORM runs PostgreSQL migrations at startup. +Compose waits up to 300 seconds for `/health/ready`, which checks database +connectivity, heap usage and disk space. This is not a provider smoke test. +No separate Laravel scheduler or schedule:list step exists for this Nest app. +No database service, UI, production deployment or BNP publishing is included. + +The full selected revision and timing live in the per-run CI workspace, not in +the host checkout. Notification delivery errors fail visibly and do not roll +back a successful deployment. Code rollback does not undo database migrations. +Opsctl stays independent; no shared lock prevents manual operations racing CI. +Glue updates remain a separate Infra/deploy operation. + +## Troubleshooting + +```sh +cd /data/infra-deploy/shared-esg +docker compose -f compose.dev.yaml ps app +docker compose -f compose.dev.yaml logs --tail=100 app +``` + +Check that DATABASE_URL is configured without exposing its value in logs. +Do not share full webhook URLs or credentials in issue reports.