implement gitlab blue-green ci cd

This commit is contained in:
Meghdad committed 2026-07-23 08:17:56 +03:30
1 parent a353b80a15
commit 0f2dee08cb
13 files changed
+686 -230

No files matched your search

+79 -93
View File
@@ -1,119 +1,105 @@
# Production deployment: `warehouse-allocation.mvphub.ir`
## Current layout
## Runtime layout
| Item | Value |
|---|---|
| SSH host | `ict` |
| Public URL | `https://warehouse-allocation.mvphub.ir` |
| Release root | `/srv/apps/warehouse-allocation` |
| Current release | `/srv/apps/warehouse-allocation/current` |
| Shared environment | `/srv/apps/warehouse-allocation/shared/.env` |
| Compose project | `warehouse-allocation` |
| API loopback address | `127.0.0.1:18081` |
| Nginx site | `/etc/nginx/sites-available/warehouse-allocation.mvphub.ir` |
| Certificate | `/etc/letsencrypt/live/warehouse-allocation.mvphub.ir` |
| ACME webroot | `/var/www/letsencrypt` |
| Blue slot | `127.0.0.1:18081` |
| Green slot | `127.0.0.1:18082` |
| Shared Redis | `warehouse-allocation-redis-1`, no host port |
| Nginx upstream | `/etc/nginx/conf.d/10-warehouse-allocation-upstream.conf` |
| Restricted account | `warehouse-deploy` |
| Deployment program | `/usr/local/sbin/warehouse-allocation-deploy` |
The API and Redis belong to a dedicated Compose project. Redis has no host port, and
the API port is bound only to loopback. Public traffic can reach it only through
the hostname-specific Nginx reverse proxy.
Only one API slot receives traffic after a deployment. During cutover both slots
run briefly, allowing Nginx to switch without an API interruption. Redis and its
named volume remain shared between releases.
## GitLab pipeline
The authoritative pipeline is [`.gitlab-ci.yml`](../.gitlab-ci.yml). It runs
quality checks, tests with a real Redis service, coverage, optional SonarQube,
container build validation, release packaging, and automatic protected-`main`
deployment.
Required protected GitLab variables:
| Variable | Type/value |
|---|---|
| `DEPLOY_HOST` | `188.137.233.91` |
| `DEPLOY_PORT` | `22` |
| `DEPLOY_USER` | `warehouse-deploy` |
| `DEPLOY_SSH_PRIVATE_KEY` | File variable containing the dedicated private key |
| `DEPLOY_KNOWN_HOSTS` | File variable containing the pinned server host key |
Optional SonarQube variables are `SONAR_HOST_URL` and masked `SONAR_TOKEN`. If both
exist, the quality gate becomes mandatory for deployment.
The CI account has no Docker-group membership and no general sudo access. It may
invoke only the root-owned deployment program through `/etc/sudoers.d`.
## Deployment transaction
The deploy job uploads:
```text
/home/warehouse-deploy/incoming/release-<40-character-commit-sha>.tar.gz
```
It then invokes:
```bash
sudo /usr/local/sbin/warehouse-allocation-deploy <commit-sha> <sha256>
```
The program:
1. serializes deployments with `flock`;
2. validates the commit ID, checksum, and archive paths;
3. extracts an immutable release;
4. builds an exact commit-tagged image while the old slot serves traffic;
5. starts and checks the inactive slot;
6. switches only the warehouse-allocation Nginx upstream;
7. runs public health, OpenAPI, and exact Swagger-example tests twice;
8. commits the active-slot metadata and removes the old API container;
9. retains five recent releases and application images.
If any step before commit fails, the upstream is restored, Nginx is reloaded,
the failed candidate is removed, and the previous release remains current.
## Status and logs
```bash
ssh ict
docker compose \
--env-file /srv/apps/warehouse-allocation/shared/.env \
-f /srv/apps/warehouse-allocation/current/deploy/compose.server.yaml \
ps
source /srv/apps/warehouse-allocation/shared/.env
echo "$ACTIVE_SLOT"
readlink -f /srv/apps/warehouse-allocation/current
docker logs --tail 100 warehouse-allocation-api-1
docker ps --filter name=warehouse-allocation
docker logs --tail 100 warehouse-allocation-api-blue-1
docker logs --tail 100 warehouse-allocation-api-green-1
docker logs --tail 100 warehouse-allocation-redis-1
curl -fsS http://127.0.0.1:18081/health/ready
curl -fsS https://warehouse-allocation.mvphub.ir/health/ready
nginx -t
```
Do not print or commit `/srv/apps/warehouse-allocation/shared/.env`. It contains the
inventory administration token.
Do not print or commit the shared `.env`; it contains the inventory administration
token. Do not use the root development `compose.yaml` for production.
## Starting or reconciling the current release
## Manual rollback
```bash
docker compose \
--env-file /srv/apps/warehouse-allocation/shared/.env \
-f /srv/apps/warehouse-allocation/current/deploy/compose.server.yaml \
up -d --build
```
The production Compose file is
[`compose.server.yaml`](compose.server.yaml). It must be used instead of the local
root `compose.yaml`, because it provides the isolated loopback port, restart
policy, resource limits, persistent Redis volume, and production environment.
## Safe release process
1. Run tests and quality checks locally.
2. Package tracked and required untracked source files without `.env`, `.git`, or
virtual environments.
3. Upload the archive to the server and verify its SHA-256 checksum.
4. Extract it into a new immutable directory under
`/srv/apps/warehouse-allocation/releases/<release-id>`.
5. Validate the new Compose file using the shared environment.
6. Point `/srv/apps/warehouse-allocation/current` at the new release.
7. Run Compose with `up -d --build`.
8. Wait for both containers to become healthy.
9. Verify loopback health, public HTTPS health, OpenAPI, and one allocation smoke
test.
Never modify another application's Compose project, loopback port, Nginx site, or
volume as part of this release.
## Nginx and TLS
The repository contains:
- [`nginx/warehouse-allocation.bootstrap.conf`](nginx/warehouse-allocation.bootstrap.conf)
for initial HTTP/ACME validation;
- [`nginx/warehouse-allocation.mvphub.ir.conf`](nginx/warehouse-allocation.mvphub.ir.conf)
for the final HTTP-to-HTTPS redirect and reverse proxy.
Always run `nginx -t` before `systemctl reload nginx`. A reload is graceful and
must only happen after the full server configuration passes validation.
The certificate uses Certbot's webroot authenticator. The existing
`certbot.timer` renews it automatically.
## Rollback
Identify the prior release first:
```bash
find /srv/apps/warehouse-allocation/releases -mindepth 1 -maxdepth 1 -type d -print
readlink -f /srv/apps/warehouse-allocation/current
```
Point `current` at the explicit prior release, validate Compose, and start it:
```bash
ln -sfn /srv/apps/warehouse-allocation/releases/<prior-release-id> \
/srv/apps/warehouse-allocation/current
docker compose \
--env-file /srv/apps/warehouse-allocation/shared/.env \
-f /srv/apps/warehouse-allocation/current/deploy/compose.server.yaml \
config --quiet
docker compose \
--env-file /srv/apps/warehouse-allocation/shared/.env \
-f /srv/apps/warehouse-allocation/current/deploy/compose.server.yaml \
up -d --build
```
Then verify `/health/ready` on loopback and HTTPS. The Redis volume is deliberately
shared between releases, so rolling application code back does not discard
inventory data.
Normal failures roll back automatically before the old slot is stopped. For a
post-deployment application regression, redeploy the desired older commit
artifact through the same deployment program. This performs the same health,
traffic-switch, and rollback protections instead of manually editing Nginx.
The deployment program is scoped to warehouse-allocation release directories,
images, two API containers, and its dedicated upstream file. It never prunes or
restarts bank-flow containers, unrelated images, Nginx sites, certificates, or
volumes.