gitlab_ci_cd_documentation:cotrav-platform
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| gitlab_ci_cd_documentation:cotrav-platform [2026/03/30 10:42] – [Auth Service Documentation] raviraj | gitlab_ci_cd_documentation:cotrav-platform [2026/03/31 05:54] (current) – [Stage 1 — Test] raviraj | ||
|---|---|---|---|
| Line 1326: | Line 1326: | ||
| < | < | ||
| + | |||
| </ | </ | ||
| - | auth-service | + | < |
| + | |||
| + | auth-service │ ├── src │ ├── controllers │ ├── routes │ ├── services │ ├── middlewares │ └── index.ts │ ├── Dockerfile ├── docker-compose.yml ├── nginx.conf ├── .gitlab-ci.yml ├── package.json ├── pnpm-lock.yaml └── tsconfig.json | ||
| + | |||
| + | </ | ||
| ---- | ---- | ||
| + | |||
| ====== Source Code (src) ====== | ====== Source Code (src) ====== | ||
| + | < | ||
| The '' | The '' | ||
| Typical structure: | Typical structure: | ||
| - | < | + | src │ ├── controllers # API request handlers ├── routes # Express route definitions ├── services # Business logic layer ├── middlewares # Authentication & validation └── index.ts # Application entry point |
| - | </code> | + | </file> |
| - | src │ ├── controllers # API request handlers | ||
| ==== Responsibilities ==== | ==== Responsibilities ==== | ||
| Line 1357: | Line 1363: | ||
| ====== Dockerfile ====== | ====== Dockerfile ====== | ||
| - | The '' | + | The '' |
| Responsibilities: | Responsibilities: | ||
| Line 1366: | Line 1372: | ||
| Example workflow: | Example workflow: | ||
| + | |||
| < | < | ||
| + | Build Image ↓ Install Dependencies ↓ Compile TypeScript ↓ Run Service | ||
| </ | </ | ||
| - | |||
| - | Build Image ↓ Install Dependencies | ||
| ---- | ---- | ||
| + | |||
| ====== docker-compose.yml ====== | ====== docker-compose.yml ====== | ||
| + | < | ||
| '' | '' | ||
| Line 1382: | Line 1389: | ||
| Example usage: | Example usage: | ||
| - | |||
| - | < | ||
| - | |||
| - | </ | ||
| docker-compose up -d | docker-compose up -d | ||
| + | |||
| + | </ | ||
| Benefits: | Benefits: | ||
| - | * Easy local setup | + | Container orchestration for development |
| - | * Container orchestration for development | + | |
| - | | + | Easy local setup |
| + | |||
| + | Environment configuration | ||
| ---- | ---- | ||
| + | |||
| ====== nginx.conf ====== | ====== nginx.conf ====== | ||
| - | The '' | + | The '' |
| Responsibilities: | Responsibilities: | ||
| Line 1411: | Line 1419: | ||
| < | < | ||
| + | Client Request │ ▼ Nginx Reverse Proxy │ ▼ Auth Service Container | ||
| </ | </ | ||
| - | |||
| - | Client Request | ||
| ---- | ---- | ||
| + | |||
| ====== .gitlab-ci.yml ====== | ====== .gitlab-ci.yml ====== | ||
| - | The '' | + | The '' |
| Pipeline stages may include: | Pipeline stages may include: | ||
| Line 1433: | Line 1441: | ||
| < | < | ||
| + | Code Push │ ▼ GitLab Pipeline │ ├── Install Dependencies ├── Run Tests ├── Build Docker Image ├── Push Image ▼ Deployment | ||
| </ | </ | ||
| + | ==== Project: Cotrav Services – Auth Service ==== | ||
| - | Code Push │ ▼ GitLab Pipeline | + | ===== 1. Overview ===== |
| + | |||
| + | This project implements a **CI/CD pipeline using GitLab CI** to automate the process of: | ||
| + | |||
| + | * Testing | ||
| + | * Building | ||
| + | * Validating | ||
| + | * Deploying | ||
| + | * Cleaning | ||
| + | |||
| + | The pipeline is designed for a **Monorepo Microservices Architecture** | ||
| ---- | ---- | ||
| - | ====== | + | ====== |
| - | The service runs inside | + | The CI/CD pipeline contains the following stages: |
| + | |||
| + | < | ||
| + | │ | ||
| + | ▼ | ||
| + | GitLab Repository | ||
| + | │ | ||
| + | ▼ | ||
| + | GitLab Runner (docker-runner01) | ||
| + | │ | ||
| + | ▼ | ||
| + | TEST → BUILD → VALIDATE → DEPLOY → CLEANUP | ||
| + | │ | ||
| + | ▼ | ||
| + | Docker | ||
| + | │ | ||
| + | ▼ | ||
| + | Push to GitLab Container Registry | ||
| + | │ | ||
| + | ▼ | ||
| + | SSH Deployment to Server | ||
| + | |||
| + | </ | ||
| + | |||
| + | ---- | ||
| + | |||
| + | ====== 3. Pipeline Stages ====== | ||
| + | |||
| + | ===== Stage 1 — Test ===== | ||
| < | < | ||
| + | test-auth | ||
| </ | </ | ||
| - | Internet | + | 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: | ||
| + | |||
| + | < | ||
| + | extends: .base_test_job | ||
| + | |||
| + | </ | ||
| + | |||
| + | The runner used: | ||
| + | |||
| + | < | ||
| + | tags: | ||
| + | - docker-runner01 | ||
| + | |||
| + | </ | ||
| ---- | ---- | ||
| + | |||
| + | |||
| + | ====== 4. Stage 2 — Build ====== | ||
| + | |||
| + | < | ||
| + | |||
| + | </ | ||
| + | |||
| + | Purpose: | ||
| + | |||
| + | * Build the Auth service | ||
| + | * Create Docker image | ||
| + | * Push image to GitLab Container Registry | ||
| + | |||
| + | Dependency: | ||
| + | |||
| + | < | ||
| + | |||
| + | </ | ||
| + | |||
| + | Build only runs after **tests pass**. | ||
| + | |||
| + | Docker image naming format: | ||
| + | |||
| + | < | ||
| + | |||
| + | </ | ||
| + | |||
| + | Example: | ||
| + | |||
| + | < | ||
| + | |||
| + | </ | ||
| + | |||
| + | ---- | ||
| + | |||
| + | ====== 5. Stage 3 — Validate ====== | ||
| + | |||
| + | < | ||
| + | |||
| + | </ | ||
| + | |||
| + | Purpose: | ||
| + | |||
| + | * Validate Docker image | ||
| + | * Ensure artifacts are correct | ||
| + | * Check build integrity | ||
| + | |||
| + | Dependency: | ||
| + | |||
| + | < | ||
| + | - build-auth | ||
| + | |||
| + | </ | ||
| + | |||
| + | 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: | ||
| + | < | ||
| + | deploy-dev | ||
| + | |||
| + | </ | ||
| + | |||
| + | Runs automatically when: | ||
| + | |||
| + | < | ||
| + | |||
| + | </ | ||
| + | |||
| + | Configuration: | ||
| + | |||
| + | < | ||
| + | INTERNAL_PORT=3000 | ||
| + | |||
| + | </ | ||
| + | |||
| + | This deploys the container to the development environment. | ||
| + | |||
| + | ---- | ||
| + | |||
| + | ====== 8. Test Deployment ====== | ||
| + | |||
| + | Job: | ||
| + | |||
| + | < | ||
| + | |||
| + | </ | ||
| + | |||
| + | Runs when: | ||
| + | |||
| + | < | ||
| + | |||
| + | </ | ||
| + | |||
| + | Deployment is **manual approval based**: | ||
| + | |||
| + | < | ||
| + | |||
| + | </ | ||
| + | |||
| + | Configuration: | ||
| + | |||
| + | < | ||
| + | |||
| + | </ | ||
| + | |||
| + | This allows QA or testing before production release. | ||
| + | |||
| + | ---- | ||
| + | |||
| + | ====== 9. Production Deployment ====== | ||
| + | |||
| + | Job: | ||
| + | |||
| + | < | ||
| + | |||
| + | </ | ||
| + | |||
| + | Production deployment uses **Blue-Green Deployment Strategy**. | ||
| + | |||
| + | Configuration: | ||
| + | |||
| + | < | ||
| + | GREEN_PORT=4004 | ||
| + | |||
| + | </ | ||
| + | |||
| + | Benefits: | ||
| + | |||
| + | * Zero downtime deployment | ||
| + | * Safe rollback | ||
| + | * High availability | ||
| + | |||
| + | Deployment is also **manual approval based**. | ||
| + | |||
| + | ---- | ||
| + | |||
| + | ====== 10. Pipeline Rules ====== | ||
| + | |||
| + | Pipeline runs only if **relevant files change**. | ||
| + | |||
| + | < | ||
| + | - Cotrav_Services/ | ||
| + | - Cotrav_Services/ | ||
| + | - Cotrav_Services/ | ||
| + | - Cotrav_Services/ | ||
| + | |||
| + | </ | ||
| + | |||
| + | Benefits: | ||
| + | |||
| + | * Faster pipeline | ||
| + | * Avoid unnecessary builds | ||
| + | |||
| + | ---- | ||
| + | |||
| + | ====== 11. Caching Strategy ====== | ||
| + | |||
| + | Pipeline uses **PNPM caching** | ||
| + | |||
| + | < | ||
| + | key: pnpm-cache-$CI_COMMIT_REF_SLUG | ||
| + | |||
| + | </ | ||
| + | |||
| + | Cached directories: | ||
| + | |||
| + | < | ||
| + | Cotrav_Services/ | ||
| + | node_modules | ||
| + | |||
| + | </ | ||
| + | |||
| + | Benefits: | ||
| + | |||
| + | * Faster dependency installation | ||
| + | * Reduced CI runtime | ||
| + | |||
| + | ---- | ||
| + | |||
| + | ====== 12. Docker Build Optimization ====== | ||
| + | |||
| + | < | ||
| + | |||
| + | </ | ||
| + | |||
| + | 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: | ||
| + | < | ||
| + | AUTH_SERVICE_ENV_BASE64 | ||
| + | |||
| + | </ | ||
| + | |||
| + | ---- | ||
| + | |||
| + | ====== 14. Cleanup Stage ====== | ||
| + | |||
| + | < | ||
| + | |||
| + | </ | ||
| + | |||
| + | Purpose: | ||
| + | |||
| + | Remove old Docker images from the registry. | ||
| + | |||
| + | Command used: | ||
| + | |||
| + | < | ||
| + | |||
| + | </ | ||
| + | |||
| + | Configuration: | ||
| + | |||
| + | < | ||
| + | |||
| + | </ | ||
| + | |||
| + | Meaning: | ||
| + | |||
| + | Only **latest 5 images are kept**. | ||
| + | |||
| + | Benefits: | ||
| + | |||
| + | * Saves registry storage | ||
| + | * Maintains clean image history | ||
| + | |||
| + | ---- | ||
| + | |||
| + | ====== 15. GitLab Templates ====== | ||
| + | |||
| + | Pipeline uses reusable templates: | ||
| + | |||
| + | < | ||
| + | |||
| + | gitlab/ | ||
| + | gitlab/ | ||
| + | gitlab/ | ||
| + | gitlab/ | ||
| + | |||
| + | </ | ||
| + | |||
| + | Advantages: | ||
| + | |||
| + | * Reusable CI logic | ||
| + | * Cleaner '' | ||
| + | * Easy to scale for multiple services | ||
| + | |||
| + | ---- | ||
| + | |||
| + | ====== 16. Monorepo Structure ====== | ||
| + | |||
| + | Example project structure: | ||
| + | |||
| + | < | ||
| + | Cotrav_Services | ||
| + | │ | ||
| + | ├── packages | ||
| + | │ ├── logger | ||
| + | │ ├── errors | ||
| + | │ └── middlewares | ||
| + | │ | ||
| + | ├── services | ||
| + | │ └── auth-service | ||
| + | │ | ||
| + | ├── scripts | ||
| + | │ | ||
| + | ├── pnpm-workspace.yaml | ||
| + | ├── turbo.json | ||
| + | └── package.json | ||
| + | |||
| + | Tools used: | ||
| + | PNPM TurboRepo TypeScript | ||
| + | |||
| + | </ | ||
| + | |||
| + | < | ||
| + | include: | ||
| + | - local: gitlab/ | ||
| + | - local: gitlab/ | ||
| + | - local: gitlab/ | ||
| + | - local: gitlab/ | ||
| + | |||
| + | variables: | ||
| + | RUNNER_TAG: " | ||
| + | DOCKER_BUILDKIT: | ||
| + | MONOREPO_ROOT: | ||
| + | SERVICE_PATH: | ||
| + | BASE_SERVICE_NAME: | ||
| + | VERSION_NUMBER: | ||
| + | DOCKER_IMAGE: | ||
| + | DOCKER_TAG: " | ||
| + | REMOTE_BASE_PATH: | ||
| + | ENV_CONTENT: | ||
| + | |||
| + | stages: | ||
| + | - test | ||
| + | - build | ||
| + | - validate | ||
| + | - deploy | ||
| + | - cleanup | ||
| + | |||
| + | cache: | ||
| + | key: " | ||
| + | paths: | ||
| + | - " | ||
| + | - " | ||
| + | - " | ||
| + | policy: pull-push | ||
| + | |||
| + | .auth-service_rules: | ||
| + | rules: | ||
| + | - if: ' | ||
| + | changes: | ||
| + | - " | ||
| + | - " | ||
| + | - " | ||
| + | - " | ||
| + | # ... | ||
| + | |||
| + | # ─── TEST ──────────────────────────────────────────────── | ||
| + | test-auth: | ||
| + | extends: .base_test_job | ||
| + | tags: | ||
| + | - " | ||
| + | <<: *auth_rules | ||
| + | |||
| + | # ─── BUILD ─────────────────────────────────────────────── | ||
| + | build-auth: | ||
| + | extends: .base_build_job | ||
| + | needs: [" | ||
| + | tags: | ||
| + | - " | ||
| + | <<: *auth_rules | ||
| + | |||
| + | # ─── VALIDATE ──────────────────────────────────────────── | ||
| + | validate-auth: | ||
| + | extends: .base_validate_job | ||
| + | stage: validate | ||
| + | needs: | ||
| + | - job: build-auth | ||
| + | artifacts: true | ||
| + | tags: | ||
| + | - " | ||
| + | <<: *auth_rules | ||
| + | |||
| + | # ─── DEPLOY DEV ────────────────────────────────────────── | ||
| + | deploy-dev: | ||
| + | extends: .base_deploy_job | ||
| + | needs: | ||
| + | - job: build-auth | ||
| + | artifacts: true | ||
| + | - job: validate-auth | ||
| + | artifacts: true | ||
| + | variables: | ||
| + | SERVICE_NAME: | ||
| + | TARGET_ENV: " | ||
| + | DEPLOY_SERVER_IP: | ||
| + | SSH_USER: " | ||
| + | PORT: " | ||
| + | INTERNAL_PORT: | ||
| + | environment: | ||
| + | name: dev/ | ||
| + | rules: | ||
| + | - if: ' | ||
| + | |||
| + | # ─── DEPLOY TEST ───────────────────────────────────────── | ||
| + | deploy-test: | ||
| + | extends: .base_deploy_job | ||
| + | needs: | ||
| + | - job: build-auth | ||
| + | artifacts: true | ||
| + | - job: validate-auth | ||
| + | artifacts: true | ||
| + | variables: | ||
| + | SERVICE_NAME: | ||
| + | TARGET_ENV: " | ||
| + | DEPLOY_SERVER_IP: | ||
| + | SSH_USER: " | ||
| + | PORT: " | ||
| + | INTERNAL_PORT: | ||
| + | environment: | ||
| + | name: test/ | ||
| + | rules: | ||
| + | - if: ' | ||
| + | when: manual | ||
| + | |||
| + | # ─── DEPLOY PROD ───────────────────────────────────────── | ||
| + | deploy-prod: | ||
| + | extends: .base_deploy_job | ||
| + | needs: | ||
| + | - job: build-auth | ||
| + | artifacts: true | ||
| + | - job: validate-auth | ||
| + | artifacts: true | ||
| + | variables: | ||
| + | SERVICE_NAME: | ||
| + | TARGET_ENV: " | ||
| + | DEPLOY_SERVER_IP: | ||
| + | SSH_USER: " | ||
| + | BLUE_PORT: " | ||
| + | GREEN_PORT: " | ||
| + | PORT: " | ||
| + | INTERNAL_PORT: | ||
| + | environment: | ||
| + | name: prod/ | ||
| + | rules: | ||
| + | - if: ' | ||
| + | when: manual | ||
| + | |||
| + | # ─── CLEANUP ───────────────────────────────────── | ||
| + | cleanup-registry: | ||
| + | stage: cleanup | ||
| + | image: registry.gitlab.com/ | ||
| + | script: | ||
| + | - echo " | ||
| + | - glab registry delete " | ||
| + | rules: | ||
| + | - if: ' | ||
| + | when: on_success | ||
| + | allow_failure: | ||
| + | |||
| + | </ | ||
| + | |||
| + | ---- | ||
| + | |||
| + | |||
| + | ====== 17. Deployment Server ====== | ||
| + | |||
| + | Deployment server path: | ||
| + | |||
| + | < | ||
| + | |||
| + | </ | ||
| + | |||
| + | Deployment method: | ||
| + | |||
| + | < | ||
| + | |||
| + | </ | ||
| + | |||
| + | ---- | ||
| + | |||
| + | ====== 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/ | ||
| + | |Docker|Containerization| | ||
| + | |PNPM|Dependency management| | ||
| + | |TurboRepo|Monorepo build system| | ||
| + | |TypeScript|Backend development| | ||
| + | |Nginx|Reverse proxy| | ||
| + | |SSH|Remote deployment| | ||
| + | |||
| + | ---- | ||
| + | |||
| + | |||
| + | ====== Deployment Architecture ====== | ||
| + | |||
| + | The service runs inside Docker containers and is exposed through **NGINX**. | ||
| + | < | ||
| + | Internet │ ▼ Nginx Gateway │ ▼ Auth Service Container | ||
| + | |||
| + | </ | ||
| + | |||
| + | ---- | ||
| + | |||
| ====== Configuration Files ====== | ====== Configuration Files ====== | ||
| Line 1465: | Line 2049: | ||
| ====== Integration with Monorepo ====== | ====== Integration with Monorepo ====== | ||
| - | '' | + | '' |
| Shared packages from the monorepo can be used inside the service: | Shared packages from the monorepo can be used inside the service: | ||
| Example: | Example: | ||
| + | |||
| < | < | ||
| + | import logger from " | ||
| </ | </ | ||
| - | |||
| - | import logger from " | ||
| ---- | ---- | ||
| + | |||
| ====== Advantages of This Setup ====== | ====== Advantages of This Setup ====== | ||
gitlab_ci_cd_documentation/cotrav-platform.1774867362.txt.gz · Last modified: by raviraj
