Run Safegres Database Audits in GitHub Actions

Dan Lynch

Dan Lynch

lesson header image

Start in report-only mode. Use the same PGPM workspace, Safegres config, and Constructive Postgres image locally and on pull requests.

Run the check by hand

The scaffold's safegres.config.js sets a security floor of B and fails on new performance findings. Review the baseline you created in lesson 1. When you deliberately accept performance findings, update it from the workspace:

bash
pnpm run audit:db:baseline --report-only

Review and commit the baseline, configs, and lockfile. Ignore generated reports. Update the baseline only when you accept new performance findings. B is an example security floor.

bash
pnpm exec safegres print-config --explain
pnpm run audit:db --report-only

--report-only makes findings advisory, not configuration or connection errors. Use --verbose to include internal advisories.

Add a pull request check

The scaffold already has an audit job. To run it as a standalone check, move that job to .github/workflows/database-audit.yml using this configuration. Safegres deploys the migrations itself; the job only starts Postgres and bootstraps the roles they grant to.

yaml
name: Database audit
on:
  pull_request:
    branches: [main]
  push:
    branches: [main]

jobs:
  audit:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      actions: read
    services:
      postgres:
        image: constructiveio/postgres-plus:18
        env:
          POSTGRES_PASSWORD: ci-only
        ports: ['5432:5432']
        options: >-
          --health-cmd "pg_isready -U postgres"
          --health-interval 5s --health-timeout 5s --health-retries 10
    env:
      PGHOST: 127.0.0.1
      PGPORT: 5432
      PGUSER: postgres
      PGPASSWORD: ci-only
      PGDATABASE: postgres
    steps:
      - uses: actions/checkout@v4
        with:
          persist-credentials: false
      - uses: actions/setup-node@v4
        with:
          node-version: '22'
      - run: npm install -g pnpm
      - run: pnpm install --frozen-lockfile
      - run: pnpm exec pgpm admin-users bootstrap --yes
      - uses: constructive-io/constructive/packages/safegres@safegres-action-v1
        with:
          version: local
          report-only: true

version: local uses the workspace install so Safegres can resolve pgsql-test. The action writes a job summary and uploads any generated reports, even if a grade gate fails. Keep write tokens and deployed database credentials out of untrusted PR jobs.

Once scope and results are stable, remove report-only: true and require the check. Keep the three comparison tools distinct:

ToolWhat it does
Security grade floorFails below the configured grade.
Performance or call-graph baselineDetects new findings in its category; each uses a separate file.
Previous JSON reportShows what changed without gating it (--compare previous.json).

On another CI platform, use the same CLI commands and upload reports even when a check fails.

Next, show the audit diff on the pull request.