Created
November 27, 2025 08:57
-
-
Save Trogious/448dacd2c0cef67d1f6ad39f64e2c36a to your computer and use it in GitHub Desktop.
GitHub Actions steps for efficient Supabase migrations and edge functions deployment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 # fetching all history is required for all the commit_range magic below | |
| - name: Install Supabase CLI | |
| uses: supabase/setup-cli@v1 | |
| with: | |
| version: latest | |
| - name: Determine commit range | |
| id: commit_range | |
| run: | | |
| if ${{ github.event_name == 'push' }}; then | |
| FROM_SHA="${{ github.event.before }}" | |
| TO_SHA="${{ github.sha }}" | |
| elif ${{ github.event_name == 'pull_request' }}; then | |
| FROM_SHA="${{ github.event.pull_request.base.sha }}" | |
| TO_SHA="${{ github.event.pull_request.head.sha }}" | |
| fi | |
| echo "from_sha=$FROM_SHA" >> $GITHUB_OUTPUT | |
| echo "to_sha=$TO_SHA" >> $GITHUB_OUTPUT | |
| - name: Get changed edge functions | |
| id: changed_functions | |
| run: | | |
| FUNCTION_NAMES=$(git diff --name-only "${{ steps.commit_range.outputs.from_sha }}..${{ steps.commit_range.outputs.to_sha }}" -- supabase/functions | sed 's!.*supabase/functions/!!' | sed 's!/.*!!' | uniq) | |
| FUNCTION_NAMES_JSON=$(printf '%s\n' "$FUNCTION_NAMES" | jq -R . | jq -sc .) | |
| echo "FUNCTION_NAMES_JSON=$FUNCTION_NAMES_JSON" | |
| echo "function_names=$FUNCTION_NAMES_JSON" >> "$GITHUB_OUTPUT" | |
| - name: Get migrations count | |
| id: migrations_count_in_commit | |
| run: | | |
| MIGRATIONS_COUNT=$(git diff --name-only "${{ steps.commit_range.outputs.from_sha }}..${{ steps.commit_range.outputs.to_sha }}" -- supabase/migrations |grep '\.sql$' | wc -l | sed 's/[^0-9]*//g' | tr "\n" "\0") | |
| echo "MIGRATIONS_COUNT=$MIGRATIONS_COUNT" | |
| echo "migrations_count=$MIGRATIONS_COUNT" >> "$GITHUB_OUTPUT" | |
| - name: Supabase link project | |
| if: (steps.changed_functions.outputs.function_names != '[""]' && steps.changed_functions.outputs.function_names != '') || (steps.migrations_count_in_commit.outputs.migrations_count != '' && steps.migrations_count_in_commit.outputs.migrations_count > 0) | |
| run: | | |
| supabase link --project-ref ${{ secrets.SUPABASE_PROJECT_ID }} | |
| - name: Apply Supabase migrations | |
| if: steps.migrations_count_in_commit.outputs.migrations_count != '' && steps.migrations_count_in_commit.outputs.migrations_count > 0 | |
| run: | | |
| supabase db push --yes | |
| - name: Deploy changed Supabase edge functions | |
| if: steps.changed_functions.outputs.function_names != '[""]' && steps.changed_functions.outputs.function_names != '' | |
| run: | | |
| for function_name in $(echo '${{ steps.changed_functions.outputs.function_names }}' | jq -r '.[]'); do | |
| echo "Deploying function: $function_name" | |
| supabase functions deploy $function_name | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment