Deploy Supabase Edge Functions in CI with GitHub Actions

The Context

Working on a React Native application using EAS + Expo Go - a very frontend heavy project which needs a managed authentication solution and a lightweight API to interact with a Postgres database. We chose to use Supabase.
We needed the ability for users to delete their account to comply with App Store regulations. The JavaScript Supabase SDK has a server-side API for deleting an account. This needs to be run on the server for security reasons (we don’t want to bundle an API key into the app which malicious users could use to authenticate and programatically delete other users).
We decided to use a Supabase Edge Function to write the code to delete an account.

The Problem

As far as I can see, the only way to deploy a Supabase Edge Function is via the command: supabase functions deploy your-function-name as per the docs.
I didn’t want to have to manually name every Edge Function in my code (i.e. if I need to update the CI every time I want to deploy a function, I might as well run the command locally). I wanted it to check which functions had been changed and deploy all that had changes automatically.

The Solution

I wrote a script which checks which files have been changed via git diff in the specific pull request and grabs the names from the directory structure. This runs inside a GitHub Actions workflow in our CI whenever a pull request is closed.
supabase functions deploy already assumes you are in the root of the project and that the folder structure goes ./functions/<your-function-name>/index.ts - so some restrictions already exist on naming of the functions.

The Code

The bash command I ended up with looks like this: git diff --name-only HEAD main | grep 'supabase/functions/*' | cut -d'/' -f 3 | xargs --no-run-if-empty -n1 supabase functions deploy --project-ref $STAGING_PROJECT_ID.
  • do git diff between the HEAD and target branch to get the pathnames of changed files
  • filter those pathnames where they start with supabase/functions/
  • split the filtered pathnames by / and select the directory that matches the function name deployment
  • deploy the named functions
# .github/workflows/deploy-supabase-edge-functions.yml on: pull_request: types: - closed jobs: deploy: runs-on: ubuntu-22.04 env: # Your env variables here steps: - uses: actions/checkout@v3 - uses: supabase/setup-cli@v1 with: version: 1.0.0 - run: | supabase link --project-ref $PROJECT_ID git diff --name-only HEAD main | grep 'supabase/functions/*' | cut -d'/' -f 3 | xargs --no-run-if-empty -n1 supabase functions deploy --project-ref $PROJECT_ID