Part 3: Building CI/CD Pipelines for TypeScript Microservices

Introduction

After learning YAML syntax and workflow basics, it's time to build real CI/CD pipelines. In my microservices architecture, I run separate CI workflows for each service (auth, payment, user, notification, etc.), and I've learned that a good pipeline catches bugs before they reach production.

In this part, I'll show you how to build complete CI/CD pipelines specifically for TypeScript microservices with PostgreSQL databases.

Project Structure

Here's the monorepo structure I'm using:

my-project/
β”œβ”€β”€ services/
β”‚   β”œβ”€β”€ auth/
β”‚   β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ tests/
β”‚   β”‚   β”œβ”€β”€ prisma/
β”‚   β”‚   β”œβ”€β”€ package.json
β”‚   β”‚   └── tsconfig.json
β”‚   β”œβ”€β”€ payment/
β”‚   β”œβ”€β”€ user/
β”‚   └── notification/
β”œβ”€β”€ packages/
β”‚   β”œβ”€β”€ shared-types/
β”‚   β”œβ”€β”€ shared-utils/
β”‚   └── eslint-config/
β”œβ”€β”€ .github/
β”‚   └── workflows/
β”‚       β”œβ”€β”€ auth-ci.yml
β”‚       β”œβ”€β”€ payment-ci.yml
β”‚       └── user-ci.yml
└── package.json

Complete CI Pipeline for TypeScript Service

Here's my complete CI workflow for the auth service:

Caching Dependencies

Caching significantly speeds up workflows. Here's how I implement it:

NPM Cache

Custom Cache for Multiple Paths

Prisma Client Cache

Matrix Builds

Test across multiple Node versions and database versions:

Testing with Different Databases

PostgreSQL with Extensions

Testing with MySQL

Environment-Specific Testing

Security Scanning

Dependency Scanning

Code Scanning with CodeQL

Docker Build Optimization

Multi-Stage Dockerfile

Workflow for Docker Build

Monorepo Workflow Optimization

Detect Changed Services

Parallel Jobs

Notification on Failure

Complete Package.json Scripts

Here's how I set up my package.json for CI:

Key Takeaways

  1. Separate linting, testing, and building into different jobs for faster feedback

  2. Use service containers for integration tests with real databases

  3. Cache dependencies to speed up workflows significantly

  4. Matrix builds help test across different versions

  5. Only run workflows when relevant code changes using path filters

  6. Fail fast when appropriate, but allow matrix builds to continue

  7. Upload artifacts from builds for debugging and deployment

  8. Security scanning should be part of your CI pipeline

  9. Parallel jobs run faster than sequential ones

  10. Monitor and notify on failures

In the next part, we'll explore advanced workflow patterns like reusable workflows, composite actions, and performance optimization.

Last updated