Prompt
How do I set up CI/CD so commits trigger builds, tests, and deployments automatically?
Latest observation
To set up CI/CD so each commit automatically triggers builds, tests, and deployments, you generally need three parts:
- Source control trigger
- CI pipeline for build/test
- CD pipeline for deploy
Here’s a practical way to do it.
1) Choose your CI/CD platform
Common options:
- GitHub Actions for GitHub repos
- GitLab CI/CD for GitLab
- Jenkins if you want self-managed flexibility
- CircleCI / Travis / Azure DevOps / Bitbucket Pipelines depending on your ecosystem
If you're starting fresh, GitHub Actions or GitLab CI/CD are usually the easiest.
2) Define your workflow stages
A typical pipeline:
- On push / pull request
- install dependencies
- lint
- run unit tests
- build artifacts / Docker image
- On merge to main
- run integration tests
- publish artifact/image
- deploy to staging
- On tag or approved release
- deploy to production
3) Set up triggers
Configure your pipeline to run automatically when code changes happen.
Common triggers:
- Push to any branch → run tests
- Pull request → validate code before merge
- Push to main → deploy staging
- Tag release → deploy production
Example concept:
feature/*branches: test onlymain: test + deploy to stagingv*tags: test + deploy to prod
4) Create the pipeline file
Example: GitHub Actions
Create .github/workflows/ci-cd.yml
name: CI/CD
on:
push:
branches:
- main
- develop
pull_request:
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up runtime
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Install dependencies
run: npm ci
- name: Run lint
run: npm run lint
- name: Run tests
run: npm test
- name: Build
run: npm run build
deploy-staging:
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
needs: test
runs-on: ubuntu-latest
steps:
- name: Deploy to staging
run: echo "Deploying to staging..."
5) Add secrets and credentials securely
Never hardcode deployment keys or cloud credentials in the repo.
Store them in:
- GitHub Secrets
- GitLab CI/CD variables
- Jenkins credentials store
Examples of secrets:
- cloud access keys
- Docker registry token
- SSH private key
- API tokens for deployment tools
6) Build and package your app
Usually you’ll want a consistent deployable artifact:
- Docker image
- ZIP/JAR/package
- compiled frontend assets
For example, with Docker:
- build image in CI
- tag with commit SHA or version
- push to registry
- deploy that exact image
This gives reproducible deployments.
7) Deploy automatically
You can deploy to:
- VMs via SSH/Ansible
- Kubernetes via Helm/kubectl
- PaaS like Heroku, Render, Cloud Run, Azure App Service
- Container platforms like ECS, EKS, GKE, AKS
A common pattern:
- CI builds and pushes Docker image
- CD pulls that image and updates the environment
Example deployment flow:
- commit to
main - pipeline runs tests
- if successful, build Docker image
- push to registry
- deploy image to staging
- after approval, deploy same image to production
8) Add environment separation
Use separate environments:
- dev
- staging
- production
This helps avoid accidental production deploys.
Best practice:
- auto-deploy to staging
- require manual approval for production
9) Make deployments safe
Use these practices:
- health checks
- rollback strategy
- blue/green or canary deployments
- database migration handling
- versioned artifacts
This reduces risk when a bad commit gets through.
10) Improve pipeline quality
Add:
- code coverage reports
- security scans (SAST/dependency scanning)
- secret scanning
- integration tests
- e2e tests
- artifact caching to speed builds
Example end-to-end flow
A simple automated setup might look like this:
- Developer pushes code
- CI runs:
- lint
- unit tests
- build
- If branch is
main:- package app
- push Docker image
- deploy to staging
- If release tag is created:
- deploy same image to production
Recommended starting setup
If you want a straightforward modern setup:
- GitHub Actions for CI/CD
- Docker for packaging
- GitHub Container Registry or another registry
- Staging + production environments
- Manual approval for production
If you want, I can give you:
- a GitHub Actions YAML for your stack,
- a GitLab CI config, or
- a Jenkins pipeline example.