6.5 KiB · text History fd1515b
name: ci

on:
  push:
    branches: [master]
    tags: ["v*"]
  pull_request:

jobs:
  test:
    # Self-hosted ARC runners (same as ../angler). Swap for `ubuntu-latest`
    # if you'd rather use GitHub-hosted runners. The Postgres service container
    # requires the runner to support service containers (Docker/dind).
    runs-on: arc-runner-set
    env:
      MIX_ENV: test
      # Under forgejo-runner/act the job runs in a container, so the postgres
      # service is reached by its service name, not localhost.
      POSTGRES_HOST: postgres
      # service is reached at its container port under forgejo-runner
      POSTGRES_PORT: 5432
      # Cap the test DB pool below the Postgres service's default
      # max_connections (100); big CI runners otherwise scale pool to cores×2
      # and exhaust connections.
      POOL_SIZE: "40"
    services:
      postgres:
        image: docker.io/postgres:16-alpine
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
        # config/test.exs connects on 54329 (matches compose.yml), so publish
        # the service container's 5432 on that host port.
        ports:
          - 54329:5432
        options: >-
          --health-cmd "pg_isready -U postgres"
          --health-interval 5s
          --health-timeout 3s
          --health-retries 5
    steps:
      - uses: actions/checkout@v4

      # ARC's default runner image is bare; install the toolchain needed to
      # compile native deps.
      - run: sudo apt-get update && sudo apt-get install -y --no-install-recommends make build-essential
      # Forge fork of erlef/setup-beam: defaults ImageOS to ubuntu-24.04,
      # since forgejo/act injects an unmappable ImageOS from the runs-on label.
      - uses: https://gg.neiam.co/neiam/action-setup-beam@v1
        with:
          # Match the build image in the Dockerfile (OTP >= 27.3.3; 27.2 breaks
          # `mix local.hex` against hex.pm's current TLS chain).
          elixir-version: "1.17.3"
          otp-version: "27.3.4.11"

      - uses: actions/cache@v4
        with:
          path: |
            deps
            _build
          key: ${{ runner.os }}-mix-${{ hashFiles('**/mix.lock') }}
          restore-keys: ${{ runner.os }}-mix-

      - run: mix deps.get

      # `mix precommit` = compile --warnings-as-errors, deps.unlock --unused,
      # format, test (which creates + migrates the test DB).
      - run: mix precommit

  image:
    needs: test
    runs-on: arc-runner-set
    permissions:
      contents: read
      packages: write
    steps:
      - uses: actions/checkout@v4
      - uses: docker/setup-buildx-action@v3

      # The forge's own registry, when this is running on one. Unlike the
      # three public registries below it needs no stored credentials: the
      # per-job token is already authorized to push to this repository's
      # own image path and nowhere else.
      #
      # `vars.CI_REGISTRY` is what says whether there is one -- on a forge
      # that doesn't set it, or on GitHub, every forge step below drops
      # out and the public registries are unaffected.
      - name: Resolve the forge registry
        id: forge
        env:
          CI_REGISTRY: ${{ vars.CI_REGISTRY }}
        run: |
          set -euo pipefail

          images=(
            "ghcr.io/$GITHUB_REPOSITORY"
            "docker.io/neiam/diogramos"
            "quay.io/neiam/diogramos"
          )

          if [ -n "${CI_REGISTRY:-}" ]; then
            forge_image="$CI_REGISTRY/$GITHUB_REPOSITORY"
            echo "publishing to the forge registry at $forge_image"

            images+=("$forge_image")
            echo "registry=$CI_REGISTRY" >> "$GITHUB_OUTPUT"
            echo "image=$forge_image" >> "$GITHUB_OUTPUT"
          else
            echo "No CI_REGISTRY variable set -- skipping the forge registry."
          fi

          {
            echo "images<<IMAGES_EOF"
            printf '%s\n' "${images[@]}"
            echo "IMAGES_EOF"
          } >> "$GITHUB_OUTPUT"

      # The token endpoint ignores the username but login-action requires
      # it to be non-empty.
      - name: Log in to the forge registry
        if: steps.forge.outputs.registry != '' && github.event_name != 'pull_request'
        uses: docker/login-action@v3
        with:
          registry: ${{ steps.forge.outputs.registry }}
          username: gitgud-actions
          password: ${{ github.token }}

      # GHCR uses the built-in token. Docker Hub and Quay creds come from
      # org-wide secrets (DOCKERHUB_USER/DOCKERHUB_TOKEN, QUAY_USERNAME/QUAY_TOKEN).
      - name: Log in to GHCR
        if: github.event_name != 'pull_request'
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ vars.GHCR_USER || github.actor }}
          password: ${{ secrets.GHCR_TOKEN }}

      - name: Log in to Docker Hub
        if: github.event_name != 'pull_request'
        uses: docker/login-action@v3
        with:
          registry: docker.io
          username: ${{ secrets.DOCKERHUB_USER }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Log in to Quay
        if: github.event_name != 'pull_request'
        uses: docker/login-action@v3
        with:
          registry: quay.io
          username: ${{ secrets.QUAY_USERNAME }}
          password: ${{ secrets.QUAY_TOKEN }}

      - id: meta
        uses: docker/metadata-action@v5
        with:
          images: ${{ steps.forge.outputs.images }}
          tags: |
            type=ref,event=branch
            type=ref,event=pr
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}.{{minor}}
            type=sha
            type=raw,value=latest,enable={{is_default_branch}}

      # The layer cache lives in the forge registry alongside the image.
      # `type=gha` is not an option here: the forge's RunnerJwt carries no
      # `ac` claim, so the Actions cache service rejects it. Writing the
      # cache is gated on a real push -- a pull request's token can read
      # that path but not write it.
      - uses: docker/build-push-action@v6
        with:
          context: .
          platforms: linux/amd64
          push: ${{ github.event_name != 'pull_request' }}
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          cache-from: ${{ steps.forge.outputs.image != '' && format('type=registry,ref={0}:buildcache', steps.forge.outputs.image) || '' }}
          cache-to: ${{ steps.forge.outputs.image != '' && github.event_name != 'pull_request' && format('type=registry,ref={0}:buildcache,mode=max', steps.forge.outputs.image) || '' }}