Prompt
I need a CI/CD workflow for a monorepo with backend, frontend, and shared libraries; suggest how to structure build, test, and deployment stages.
Latest observation
Here’s a practical CI/CD structure for a monorepo containing backend, frontend, and shared libraries.
1) Recommended repo layout
A clear structure makes selective builds/testing much easier:
repo/
apps/
backend/
frontend/
packages/
shared-lib-a/
shared-lib-b/
infra/
.github/workflows/
If you use a monorepo tool like Nx, Turborepo, or Bazel, you can make pipeline execution dependency-aware so only impacted projects run.
2) CI/CD pipeline stages
A. Trigger strategy
Use different triggers for different purposes:
- Pull requests: validate only
- Main branch: build, test, package, deploy to staging
- Release tags / manual approval: deploy to production
B. Suggested pipeline stages
1. Lint and format check
Run first and fail fast.
What to run
- ESLint / language linter
- Prettier / formatter check
- Type checking if applicable
Why
- Catches obvious issues quickly
- Keeps code style consistent
2. Dependency install and cache setup
Cache dependencies to speed up CI.
Cache
node_modulesor package manager cache- build cache
- test cache
Tools
- GitHub Actions cache
- pnpm/yarn/npm cache
- Nx/Turbo remote cache if available
3. Build shared libraries first
Shared libraries should be built and validated before apps depending on them.
Flow
- Build shared packages
- Run unit tests for shared packages
- Publish versioned artifacts if needed, or keep internal build artifacts
Benefit
- Prevents downstream app failures due to shared code changes
4. Unit tests
Run unit tests for:
- backend
- frontend
- shared libraries
Prefer affected-only testing on PRs.
Example
- If only frontend code changed, don’t run backend integration tests unless shared code was touched.
5. Integration tests
Validate interactions between backend and shared libraries, or frontend and API contracts.
Examples
- API contract tests
- DB integration tests
- service-to-service tests
- component tests for frontend
Run:
- on main branch
- on PRs when backend/shared code changes
- in a dedicated staging environment if needed
6. End-to-end tests
Run after deployment to a temporary preview or staging environment.
Use for
- user flows
- login
- critical business paths
- regression checks
Recommendation
- Run a small smoke suite on every merge
- Run the full E2E suite nightly or before production release
3) Build strategy
Backend
Build backend as a container image or deployable artifact.
Typical steps
- compile/transpile
- run unit tests
- package into Docker image
- scan image for vulnerabilities
- push to registry
Frontend
Build frontend into static assets or a container image.
Typical steps
- install deps
- typecheck/test
- build production bundle
- upload assets to CDN/object storage or containerize for deployment
Shared libraries
Options:
- Internal workspace packages
- built as part of the monorepo
- consumed directly by backend/frontend
- Versioned packages
- published to a private registry
- useful if you want strict versioning and reuse across repos
For most monorepos, workspace packages + affected builds are simplest.
4) Deployment strategy
A. Environments
Use a promotion model:
- Dev/Preview
- per-PR or per-branch preview
- Staging
- deploy automatically from main
- Production
- deploy from release tag or manual approval
B. Backend deployment
Common patterns:
- Kubernetes deployment
- ECS/Cloud Run/App Service
- Serverless functions
Deployment steps:
- Build image
- Run integration tests
- Deploy to staging
- Run smoke tests
- Approve/promote to production
C. Frontend deployment
Common patterns:
- static hosting (S3 + CDN, Vercel, Netlify, etc.)
- containerized web server
Deployment steps:
- Build assets
- Upload to staging bucket/environment
- Smoke test
- Promote to production
D. Shared library deployment
If libraries are internal-only:
- no separate deploy; they are built and consumed within the same pipeline
If published:
- version and publish them before app deployment
- use semantic versioning and changelogs
- ensure app pipelines pin compatible versions
5) Change detection / affected builds
In a monorepo, avoid rebuilding everything on every commit.
Use dependency-aware execution:
- detect changed files
- identify impacted apps/libs
- run only relevant tests/builds
Example
- change in
packages/shared-lib-a→ build/testshared-lib-a, backend, frontend if they depend on it - change in
apps/frontendonly → build/test frontend only
This is one of the biggest CI cost savers.
6) Example pipeline flow
Pull request
- Checkout
- Install deps with cache
- Lint/format/typecheck
- Build affected packages
- Run unit tests for affected projects
- Run integration tests if relevant
- Generate preview deployment if needed
Merge to main
- Checkout
- Install deps with cache
- Lint/format/typecheck
- Build affected projects
- Run full unit/integration tests for affected areas
- Build Docker images / artifacts
- Deploy to staging
- Run smoke/E2E tests
Release
- Promote staging artifacts
- Manual approval
- Deploy to production
- Post-deploy smoke tests
- Rollback if needed
7) Best practices
- Keep jobs small and parallelizable
- Fail fast on lint/typecheck
- Use matrix builds for backend/frontend/tests
- Cache dependencies and build artifacts
- Prefer immutable artifacts between stages
- Use environment-specific secrets
- Add rollback strategy for production
- Track build provenance and artifact versions
8) Example stage breakdown
A clean logical pipeline could look like this:
CI
├─ Validate
│ ├─ Lint
│ ├─ Format check
│ └─ Type check
├─ Test
│ ├─ Unit tests (affected)
│ ├─ Integration tests (affected)
│ └─ E2E smoke tests
├─ Build
│ ├─ Shared libs
│ ├─ Backend artifact/image
│ └─ Frontend bundle/artifact
└─ Deploy
├─ Staging
├─ Smoke tests
└─ Production approval + deploy
If you want, I can also give you:
- a GitHub Actions YAML example for this monorepo, or
- a GitLab CI / Jenkins pipeline design.