|
| 1 | +# GitHub Actions Workflows |
| 2 | + |
| 3 | +## Supabase Edge Function Tests |
| 4 | + |
| 5 | +**File:** `supabase-tests.yml` |
| 6 | + |
| 7 | +**Purpose:** Prevent reintroduction of the 401 "Invalid JWT" authentication bug in Supabase Edge Functions. |
| 8 | + |
| 9 | +**Triggers:** |
| 10 | +- Pull requests to `main` branch |
| 11 | +- Pushes to `main` branch |
| 12 | +- Only runs when files in `supabase/` directory are modified |
| 13 | + |
| 14 | +**What It Tests:** |
| 15 | + |
| 16 | +1. **RPC Function Verification:** |
| 17 | + - Checks that `public.get_service_role_key()` function exists in database |
| 18 | + - This function is CRITICAL for function-to-function authentication |
| 19 | + - If missing, edge functions will fail with 401 errors |
| 20 | + |
| 21 | +2. **Code Pattern Verification:** |
| 22 | + - Ensures `sync-all-devices` uses `supabase.rpc('get_service_role_key')` |
| 23 | + - Detects if code incorrectly uses `Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')` |
| 24 | + - The env var is only 41 chars (internal ID), not the 219-char JWT needed |
| 25 | + |
| 26 | +3. **Full Regression Test Suite:** |
| 27 | + - Runs `supabase/tests/function-to-function-auth.test.sql` |
| 28 | + - Tests both direct function calls and nested function-to-function calls |
| 29 | + - Uses permanent test fixtures: `echo-test` and `caller-test` functions |
| 30 | + |
| 31 | +**Requirements:** |
| 32 | +- Repository secret `DATABASE_URL` must be configured |
| 33 | +- Database must have vault secrets configured |
| 34 | +- PostgreSQL client (automatically installed in workflow) |
| 35 | + |
| 36 | +**Workflow will skip if:** |
| 37 | +- No files in `supabase/` directory were modified |
| 38 | +- `DATABASE_URL` secret is not configured (shows as skipped, not failed) |
| 39 | + |
| 40 | +**Setup:** |
| 41 | + |
| 42 | +1. Add repository secret: |
| 43 | + - Go to Settings → Secrets and variables → Actions |
| 44 | + - Add `DATABASE_URL` with value: `postgresql://postgres:[password]@[host]:[port]/postgres` |
| 45 | + |
| 46 | +2. Ensure database has required migrations applied: |
| 47 | + ```bash |
| 48 | + psql $DATABASE_URL -f supabase/migrations/20260203194800_add_get_service_role_key_function.sql |
| 49 | + ``` |
| 50 | + |
| 51 | +**Viewing Results:** |
| 52 | +- Check the "Actions" tab in GitHub repository |
| 53 | +- Look for workflow run named "Supabase Edge Function Tests" |
| 54 | +- All checks must pass before merging pull requests |
| 55 | + |
| 56 | +**Related Documentation:** |
| 57 | +- See `supabase/TROUBLESHOOTING.md` for details on the 401 bug |
| 58 | +- See `supabase/POSTMORTEM_401_AUTH_BUG.md` for incident history |
| 59 | +- See `supabase/tests/function-to-function-auth.test.sql` for test details |
| 60 | + |
| 61 | +**Troubleshooting:** |
| 62 | + |
| 63 | +If workflow fails: |
| 64 | + |
| 65 | +1. **"get_service_role_key() RPC function missing":** |
| 66 | + - Apply migration: `supabase/migrations/20260203194800_add_get_service_role_key_function.sql` |
| 67 | + - Or run: `npx supabase db push` |
| 68 | + |
| 69 | +2. **"sync-all-devices uses env var instead of vault RPC":** |
| 70 | + - Someone modified `sync-all-devices/index.ts` incorrectly |
| 71 | + - Must use: `await supabase.rpc('get_service_role_key')` |
| 72 | + - See `supabase/TROUBLESHOOTING.md` for correct pattern |
| 73 | + |
| 74 | +3. **"Full regression test suite failed":** |
| 75 | + - Check database connectivity |
| 76 | + - Verify vault secrets are configured |
| 77 | + - Review test output for specific failures |
| 78 | + - Run tests manually: `psql $DATABASE_URL -f supabase/tests/function-to-function-auth.test.sql` |
| 79 | + |
| 80 | +## Adding New Workflows |
| 81 | + |
| 82 | +When adding new workflows to this directory: |
| 83 | + |
| 84 | +1. Name files descriptively: `feature-name.yml` |
| 85 | +2. Add documentation section in this README |
| 86 | +3. Use specific triggers (avoid `on: [push]` for all branches) |
| 87 | +4. Add proper job conditions to skip unnecessary runs |
| 88 | +5. Include clear error messages in failure steps |
| 89 | +6. Document required secrets and setup steps |
0 commit comments