User Tools

Site Tools


gitlab_ci_cd_documentation:cotrav-platform

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
gitlab_ci_cd_documentation:cotrav-platform [2026/03/30 10:46] – [Integration with Monorepo] ravirajgitlab_ci_cd_documentation:cotrav-platform [2026/03/31 05:54] (current) – [Stage 1 — Test] raviraj
Line 1444: Line 1444:
  
 </code> </code>
 +==== Project: Cotrav Services – Auth Service ====
 +
 +===== 1. Overview =====
 +
 +This project implements a **CI/CD pipeline using GitLab CI**  to automate the process of:
 +
 +  * Testing
 +  * Building
 +  * Validating
 +  * Deploying
 +  * Cleaning Docker images
 +
 +The pipeline is designed for a **Monorepo Microservices Architecture**  where multiple services exist inside a single repository. The pipeline specifically handles deployment for the **Auth Service**.
 +
 +----
 +
 +====== 2. Pipeline Architecture ======
 +
 +The CI/CD pipeline contains the following stages:
 +
 +<code>Developer Push Code
 +
 +
 +GitLab Repository
 +
 +
 +GitLab Runner (docker-runner01)
 +
 +
 +TEST → BUILD → VALIDATE → DEPLOY → CLEANUP
 +
 +
 +Docker Image Build
 +
 +
 +Push to GitLab Container Registry
 +
 +
 +SSH Deployment to Server
 +
 +</code>
 +
 +----
 +
 +====== 3. Pipeline Stages ======
 +
 +===== Stage 1 — Test =====
 +
 +<code>
 +test-auth
 +
 +</code>
 +
 +This stage runs automated tests before building the service.
 +
 +Purpose:
 +
 +  * Verify code functionality
 +  * Prevent broken builds
 +  * Ensure stability before deployment
 +
 +The job extends a reusable template:
 +
 +<code>
 +extends: .base_test_job
 +
 +</code>
 +
 +The runner used:
 +
 +<code>
 +tags:
 +- docker-runner01
 +
 +</code>
 +
 +----
 +
 +
 +====== 4. Stage 2 — Build ======
 +
 +<code>build-auth
 +
 +</code>
 +
 +Purpose:
 +
 +  * Build the Auth service
 +  * Create Docker image
 +  * Push image to GitLab Container Registry
 +
 +Dependency:
 +
 +<code>needs: ["test-auth"]
 +
 +</code>
 +
 +Build only runs after **tests pass**.
 +
 +Docker image naming format:
 +
 +<code>auth-service-${branch}-V02-${commit}
 +
 +</code>
 +
 +Example:
 +
 +<code>auth-service-dev-V02-a23f9c
 +
 +</code>
 +
 +----
 +
 +====== 5. Stage 3 — Validate ======
 +
 +<code>validate-auth
 +
 +</code>
 +
 +Purpose:
 +
 +  * Validate Docker image
 +  * Ensure artifacts are correct
 +  * Check build integrity
 +
 +Dependency:
 +
 +<code>needs:
 +- build-auth
 +
 +</code>
 +
 +Artifacts from build are used for validation.
 +
 +----
 +
 +====== 6. Deployment Strategy ======
 +
 +Deployment is automated using **SSH + Docker**.
 +
 +Pipeline supports **3 environments**:
 +
 +^Environment^Branch^Port|
 +|Dev|dev|4001|
 +|Test|test|4002|
 +|Production|production|4003|
 +
 +----
 +
 +====== 7. Dev Deployment ======
 +
 +Job:
 +<code>
 +deploy-dev
 +
 +</code>
 +
 +Runs automatically when:
 +
 +<code>branch == dev
 +
 +</code>
 +
 +Configuration:
 +
 +<code>PORT=4001
 +INTERNAL_PORT=3000
 +
 +</code>
 +
 +This deploys the container to the development environment.
 +
 +----
 +
 +====== 8. Test Deployment ======
 +
 +Job:
 +
 +<code>deploy-test
 +
 +</code>
 +
 +Runs when:
 +
 +<code>branch == test
 +
 +</code>
 +
 +Deployment is **manual approval based**:
 +
 +<code>when: manual
 +
 +</code>
 +
 +Configuration:
 +
 +<code>PORT=4002
 +
 +</code>
 +
 +This allows QA or testing before production release.
 +
 +----
 +
 +====== 9. Production Deployment ======
 +
 +Job:
 +
 +<code>deploy-prod
 +
 +</code>
 +
 +Production deployment uses **Blue-Green Deployment Strategy**.
 +
 +Configuration:
 +
 +<code>ex - BLUE_PORT=4003
 +GREEN_PORT=4004
 +
 +</code>
 +
 +Benefits:
 +
 +  * Zero downtime deployment
 +  * Safe rollback
 +  * High availability
 +
 +Deployment is also **manual approval based**.
 +
 +----
 +
 +====== 10. Pipeline Rules ======
 +
 +Pipeline runs only if **relevant files change**.
 +
 +<code>changes:
 +- Cotrav_Services/packages/**/*
 +- Cotrav_Services/services/auth-service/**/*
 +- Cotrav_Services/pnpm-lock.yaml
 +- Cotrav_Services/package.json
 +
 +</code>
 +
 +Benefits:
 +
 +  * Faster pipeline
 +  * Avoid unnecessary builds
 +
 +----
 +
 +====== 11. Caching Strategy ======
 +
 +Pipeline uses **PNPM caching**  to speed up builds.
 +
 +<code>cache:
 +key: pnpm-cache-$CI_COMMIT_REF_SLUG
 +
 +</code>
 +
 +Cached directories:
 +
 +<code>Cotrav_Services/.pnpm-store
 +Cotrav_Services/node_modules
 +node_modules
 +
 +</code>
 +
 +Benefits:
 +
 +  * Faster dependency installation
 +  * Reduced CI runtime
 +
 +----
 +
 +====== 12. Docker Build Optimization ======
 +
 +<code>DOCKER_BUILDKIT: "1"
 +
 +</code>
 +
 +BuildKit improves:
 +
 +  * Docker build performance
 +  * Layer caching
 +  * Parallel builds
 +
 +----
 +
 +====== 13. Environment Variables ======
 +
 +Important pipeline variables:
 +
 +^Variable^Purpose|
 +|RUNNER_TAG|Runner to execute jobs|
 +|SERVICE_PATH|Path to service|
 +|DOCKER_IMAGE|Docker registry image|
 +|DOCKER_TAG|Unique build tag|
 +|REMOTE_BASE_PATH|Deployment directory|
 +|ENV_CONTENT|Encoded environment file|
 +
 +Environment file is stored securely in GitLab CI variables:
 +<code>
 +AUTH_SERVICE_ENV_BASE64  /   this add cicd varible
 +
 +</code>
 +
 +----
 +
 +====== 14. Cleanup Stage ======
 +
 +<code>cleanup-registry
 +
 +</code>
 +
 +Purpose:
 +
 +Remove old Docker images from the registry.
 +
 +Command used:
 +
 +<code>glab registry delete
 +
 +</code>
 +
 +Configuration:
 +
 +<code>--keep-n 5
 +
 +</code>
 +
 +Meaning:
 +
 +Only **latest 5 images are kept**.
 +
 +Benefits:
 +
 +  * Saves registry storage
 +  * Maintains clean image history
 +
 +----
 +
 +====== 15. GitLab Templates ======
 +
 +Pipeline uses reusable templates:
 +
 +<file>
 +
 +gitlab/templates/test-service.yml
 +gitlab/templates/build-service.yml
 +gitlab/templates/validate-service.yml
 +gitlab/templates/deploy-service.yml
 +
 +</file>
 +
 +Advantages:
 +
 +  * Reusable CI logic
 +  * Cleaner ''.gitlab-ci.yml''
 +  * Easy to scale for multiple services
 +
 +----
 +
 +====== 16. Monorepo Structure ======
 +
 +Example project structure:
 +
 +<file>
 +Cotrav_Services
 +
 +├── packages
 +│ ├── logger
 +│ ├── errors
 +│ └── middlewares
 +
 +├── services
 +│ └── auth-service
 +
 +├── scripts
 +
 +├── pnpm-workspace.yaml
 +├── turbo.json
 +└── package.json
 +
 +Tools used:
 +PNPM TurboRepo TypeScript
 +
 +</file>
 +
 +<file>
 +include:
 +  - local: gitlab/templates/test-service.yml
 +  - local: gitlab/templates/build-service.yml
 +  - local: gitlab/templates/validate-service.yml
 +  - local: gitlab/templates/deploy-service.yml
 +
 +variables:
 +  RUNNER_TAG: "docker-runner01"
 +  DOCKER_BUILDKIT: "1"
 +  MONOREPO_ROOT: "Cotrav_Services"
 +  SERVICE_PATH: "Cotrav_Services/services/auth-service"
 +  BASE_SERVICE_NAME: "auth-service"
 +  VERSION_NUMBER: "V02"
 +  DOCKER_IMAGE: "$CI_REGISTRY_IMAGE/auth-service"
 +  DOCKER_TAG: "auth-service-${CI_COMMIT_REF_SLUG}-V02-${CI_COMMIT_SHORT_SHA}"
 +  REMOTE_BASE_PATH: "/opt/cotrav/CICD_Pipeline"
 +  ENV_CONTENT: "$AUTH_SERVICE_ENV_BASE64"
 +
 +stages:
 +  - test
 +  - build
 +  - validate
 +  - deploy
 +  - cleanup
 +
 +cache:
 +  key: "pnpm-cache-$CI_COMMIT_REF_SLUG"
 +  paths:
 +    - "Cotrav_Services/.pnpm-store"
 +    - "Cotrav_Services/node_modules"
 +    - "node_modules"
 +  policy: pull-push
 +
 +.auth-service_rules: &auth_rules
 +  rules:
 +    - if: '$CI_COMMIT_BRANCH == "dev" || $CI_COMMIT_BRANCH == "test" || $CI_COMMIT_BRANCH == "production"'
 +      changes:
 +        - "Cotrav_Services/packages/**/*"
 +        - "Cotrav_Services/services/auth-service/**/*"
 +        - "Cotrav_Services/pnpm-lock.yaml"
 +        - "Cotrav_Services/package.json"
 +# ...
 +
 +# ─── TEST ────────────────────────────────────────────────
 +test-auth:
 +  extends: .base_test_job
 +  tags:
 +    - "$RUNNER_TAG"
 +  <<: *auth_rules
 +
 +# ─── BUILD ───────────────────────────────────────────────
 +build-auth:
 +  extends: .base_build_job
 +  needs: ["test-auth"]
 +  tags:
 +    - "$RUNNER_TAG"
 +  <<: *auth_rules
 +
 +# ─── VALIDATE ────────────────────────────────────────────
 +validate-auth:
 +  extends: .base_validate_job
 +  stage: validate
 +  needs:
 +    - job: build-auth
 +      artifacts: true
 +  tags:
 +    - "$RUNNER_TAG"
 +  <<: *auth_rules
 +
 +# ─── DEPLOY DEV ──────────────────────────────────────────
 +deploy-dev:
 +  extends: .base_deploy_job
 +  needs:
 +    - job: build-auth
 +      artifacts: true
 +    - job: validate-auth
 +      artifacts: true
 +  variables:
 +    SERVICE_NAME: "auth-service"
 +    TARGET_ENV: "dev"
 +    DEPLOY_SERVER_IP: "$DEV_IP"
 +    SSH_USER: "root"
 +    PORT: "4001"
 +    INTERNAL_PORT: "3000"
 +  environment:
 +    name: dev/auth-service
 +  rules:
 +    - if: '$CI_COMMIT_BRANCH == "dev"'
 +
 +# ─── DEPLOY TEST ─────────────────────────────────────────
 +deploy-test:
 +  extends: .base_deploy_job
 +  needs:
 +    - job: build-auth
 +      artifacts: true
 +    - job: validate-auth
 +      artifacts: true
 +  variables:
 +    SERVICE_NAME: "auth-service"
 +    TARGET_ENV: "test"
 +    DEPLOY_SERVER_IP: "$DEV_IP"
 +    SSH_USER: "root"
 +    PORT: "4002"
 +    INTERNAL_PORT: "3000"
 +  environment:
 +    name: test/auth-service
 +  rules:
 +    - if: '$CI_COMMIT_BRANCH == "test"'
 +      when: manual
 +
 +# ─── DEPLOY PROD ─────────────────────────────────────────
 +deploy-prod:
 +  extends: .base_deploy_job
 +  needs:
 +    - job: build-auth
 +      artifacts: true
 +    - job: validate-auth
 +      artifacts: true
 +  variables:
 +    SERVICE_NAME: "auth-service"
 +    TARGET_ENV: "prod"
 +    DEPLOY_SERVER_IP: "$DEV_IP"
 +    SSH_USER: "root"
 +    BLUE_PORT: "4003"
 +    GREEN_PORT: "4004"
 +    PORT: "4003"
 +    INTERNAL_PORT: "3000"
 +  environment:
 +    name: prod/auth-service
 +  rules:
 +    - if: '$CI_COMMIT_BRANCH == "production"'
 +      when: manual
 +
 +# ─── CLEANUP ─────────────────────────────────────
 +cleanup-registry:
 +  stage: cleanup
 +  image: registry.gitlab.com/gitlab-org/cli:latest
 +  script:
 +    - echo "Cleaning up old images for $BASE_SERVICE_NAME"
 +    - glab registry delete "$CI_PROJECT_PATH" --name "$BASE_SERVICE_NAME" --keep-n 5 --yes || true
 +  rules:
 +    - if: '$CI_COMMIT_BRANCH == "production"'
 +      when: on_success
 +  allow_failure: true
 +
 +</file>
 +
 +----
 +
 +
 +====== 17. Deployment Server ======
 +
 +Deployment server path:
 +
 +<code>/opt/cotrav/CICD_Pipeline
 +
 +</code>
 +
 +Deployment method:
 +
 +<code>GitLab Runner → SSH → Docker Container
 +
 +</code>
 +
 +----
 +
 +====== 18. Advantages of This Pipeline ======
 +
 +  * Automated testing
 +  * Automated Docker build
 +  * Monorepo support
 +  * Environment based deployment
 +  * Blue-Green production deployment
 +  * Docker registry cleanup
 +  * Faster builds using caching
 +
 +----
 +
 +====== 19. Technologies Used ======
 +
 +^Technology^Purpose|
 +|GitLab CI/CD|Pipeline automation|
 +|Docker|Containerization|
 +|PNPM|Dependency management|
 +|TurboRepo|Monorepo build system|
 +|TypeScript|Backend development|
 +|Nginx|Reverse proxy|
 +|SSH|Remote deployment|
  
 ---- ----
gitlab_ci_cd_documentation/cotrav-platform.1774867604.txt.gz · Last modified: by raviraj