4.6 KiB · text History f960521
name: checks
# A pull request's build and tests, as the sibling apps run them
# (cartosnap, gitgud):
#
#   rust  — rustfmt and clippy (-D warnings) over each NIF crate in native/
#   test  — `mix precommit` (compile --warnings-as-errors, deps.unlock
#           --unused, format, test) against a throwaway Postgres 17, the NIFs
#           built from source by Rustler on the way, then a check that
#           precommit changed nothing (an unformatted file or a stale lock
#           entry fails the run rather than being fixed silently)
#
# The SDK conformance tests (test/vigil_web/sdk_*_test.exs) skip themselves
# here: they need reference SDK checkouts and binaries the runner doesn't
# have. Run them locally where those exist.
on:
  push:
    branches: [main]
  pull_request:
  workflow_dispatch:

permissions:
  contents: read

# A newer push supersedes this run: on a branch or PR the run in
# progress is cancelled; on main it finishes, and only a run still
# waiting is replaced.
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}

jobs:
  rust:
    runs-on: self-hosted
    steps:
      - uses: actions/checkout@v4

      - name: Build dependencies
        run: |
          sudo apt-get update -q
          sudo apt-get install -y -q --no-install-recommends build-essential

      # The same toolchain as the Dockerfile (RUST_VERSION); move them together.
      - name: Rust
        run: |
          curl -sSf https://sh.rustup.rs | sh -s -- -y --profile minimal --default-toolchain 1.92.0 -c clippy -c rustfmt
          echo "$HOME/.cargo/bin" >> "$GITHUB_PATH"

      - name: rustfmt
        run: |
          set -eu
          for crate in native/*/; do
            echo "::group::$crate"
            (cd "$crate" && cargo fmt --check)
            echo "::endgroup::"
          done

      - name: Clippy
        run: |
          set -eu
          for crate in native/*/; do
            echo "::group::$crate"
            (cd "$crate" && cargo clippy --all-targets --locked -- -D warnings)
            echo "::endgroup::"
          done

  test:
    runs-on: self-hosted
    services:
      # Plain Postgres, as compose.yml runs: Vigil needs no extensions.
      postgres:
        image: postgres:17
        env:
          POSTGRES_USER: vigil
          POSTGRES_PASSWORD: vigil
        options: >-
          --health-cmd "pg_isready -U vigil"
          --health-interval 5s
          --health-timeout 5s
          --health-retries 20
    env:
      MIX_ENV: test
      VIGIL_TEST_DB_HOST: postgres
      VIGIL_TEST_DB_PORT: "5432"
      # Postgres takes 100 connections; the runner has cores for more.
      VIGIL_TEST_DB_POOL_SIZE: "40"
    steps:
      - uses: actions/checkout@v4

      # As the Dockerfile's builder: a C compiler for bcrypt's NIF (and the
      # linker for the Rust ones), git for the heroicons and daisyui
      # dependencies.
      - name: Build dependencies
        run: |
          sudo apt-get update -q
          sudo apt-get install -y -q --no-install-recommends build-essential git

      # The same toolchain the Dockerfile builds with.
      # OTP and Elixir from builds.hex.pm, the builds setup-beam would
      # fetch — taken directly, since setup-beam chooses one by ImageOS,
      # which this runner sets to "selfhosted" over anything a workflow
      # says.
      - name: OTP and Elixir
        env:
          OTP_VERSION: "28.5.0.8"
          ELIXIR_VERSION: "1.20.4"
        run: |
          ubuntu=$(. /etc/os-release && echo "$VERSION_ID")
          curl -sSfL "https://builds.hex.pm/builds/otp/ubuntu-${ubuntu}/OTP-${OTP_VERSION}.tar.gz" | tar xz -C /tmp
          # Installed where it's unpacked: Install's root is its own directory.
          (cd "/tmp/OTP-${OTP_VERSION}" && ./Install -minimal "/tmp/OTP-${OTP_VERSION}" > /dev/null)
          curl -sSfL -o /tmp/elixir.zip "https://builds.hex.pm/builds/elixir/v${ELIXIR_VERSION}-otp-${OTP_VERSION%%.*}.zip"
          unzip -q -d /tmp/elixir /tmp/elixir.zip
          echo "/tmp/OTP-${OTP_VERSION}/bin" >> "$GITHUB_PATH"
          echo /tmp/elixir/bin >> "$GITHUB_PATH"

      # Rustler builds the NIFs from source during `mix compile`.
      - name: Rust
        run: |
          curl -sSf https://sh.rustup.rs | sh -s -- -y --profile minimal --default-toolchain 1.92.0
          echo "$HOME/.cargo/bin" >> "$GITHUB_PATH"

      - name: Dependencies
        run: |
          mix local.hex --force
          mix local.rebar --force
          mix deps.get

      # The `test` alias inside it creates and migrates the database first.
      - name: Precommit
        run: mix precommit

      - name: Precommit left the tree as it was
        run: git diff --exit-code