Continuous integration

Create a .github/workflows/docker.yml file.

Note

This assumes there is already a “CI” workflow that runs tests on the main branch.

If you need to build multiple images, then for each image:

  1. Include a docker/build-push-action step.

  2. Set either:

    • The path to the Dockerfile with the file key

    • The path to the directory (context) with the context key

  3. Add a suffix to the repository name under the tags key.

name: Deploy
on:
  workflow_run:
    workflows: ["CI"]
    branches: [main]
    types:
      - completed
jobs:
  docker:
    if: ${{ github.event.workflow_run.conclusion == 'success' }}
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      # https://github.com/docker/login-action#github-container-registry
      - uses: docker/login-action@v1
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      # https://github.com/docker/setup-buildx-action#usage
      - uses: docker/setup-buildx-action@v1
      # https://github.com/docker/build-push-action#usage
      - uses: docker/build-push-action@v2
        with:
          push: true
          tags: |
            ghcr.io/${{ github.repository }}:latest
          cache-from: type=gha
          cache-to: type=gha,mode=max

Note

The docker/build-push-action step uses BuildKit by default.

Reference: