CI/CD with Docker

A Typical Pipeline

  1. Push to Git triggers the pipeline.
  2. Build the image (with cache) and tag it with the commit SHA.
  3. Run tests inside the image.
  4. Scan the image for vulnerabilities.
  5. Push it to the registry.
  6. Deploy that exact tag to staging, then production.

GitHub Actions Example

name: build-and-push
on:
  push:
    branches: [main]
jobs:
  docker:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: docker/setup-buildx-action@v3
      - uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USER }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - uses: docker/build-push-action@v6
        with:
          push: true
          tags: yourname/myapi:${{ github.sha }}
          cache-from: type=gha
          cache-to: type=gha,mode=max