Replies: 4 comments 7 replies
-
Thanks for sharing this. Do you know of any way to only deploy functions when their code has actually been altered? Currently, every push will redeploy all functions, and using something like `on “/functions/“ will limit to pushes that include changes to the functions, but not a specific function. |
Beta Was this translation helpful? Give feedback.
-
Thank you for the insight into your workflow. |
Beta Was this translation helpful? Give feedback.
-
Hey @rbkayz , thanks for the write up! Very useful. Am I right to assume that you don't use Supabase Local (on development machines) as part of this setup? If not, was there a reason you chose not to include it? Thanks. |
Beta Was this translation helpful? Give feedback.
-
Copying and updating the Deploy To Prod workflow yield the follow error
|
Beta Was this translation helpful? Give feedback.
-
Hello community,
As we began moving our entire project to a staging and prod environment, we struggled with establishing the right repo structure, processes etc. to enable a smooth CI / CD workflow.
Hoping some of our learnings help. Would love to get your thoughts
Supabase - A suggestive guide for devops incl. repo & environment management
Devs,
As we are building hashmail, supabase powers a large part of our backend. Consequently, as we began moving our entire project to a staging and prod environment, we struggled with establishing the right repo structure, processes etc. to enable a smooth CI / CD workflow.
This guide is a recommendation based on what worked for us. It imbibes a lot of the guides written by the supabase team, as well as the github & discord community. As you implement this (or other similar systems in your teams), we hope to learn from you on how can we make this better
This guide will cover a few topics:
And is structured in a sequence of steps to improve your devops with supabase
Step 1 - Setup your repo structure
If you're using the postgres db, postgrest APIs, storage and edge functions, you'll have a lot of code in various languages (some function queries, triggers, RLS policies in psql, functions in deno, etc.)
To maintain it in the same repo, do the following:
Setup a new project repo in VSCode (the editor of choice for me) and initialize git.
Install the supabase CLI
Link your supabase project to the repo (this will create a folder called supabase, write your config.toml file, and setup the functions folder) - https://supabase.com/docs/reference/cli/usage
This folder will contain your edge functions (under ./supabase/functions) and migrations under (./supabase/migrations) - we cover migrations later
In the main project repo, create two new folders - .github (for your CI/CD workflows) and .vscode (for your settings.json file).
Finally, create a folder for your db queries (we will maintain all your function queries, triggers, RLS policies, etc. in this folder)
Step 2 - (OPTIONAL) Install some extensions / software
This step is purely to enhance your experience. Here are some of the extensions we prefer:
PostgreSQL explorer - https://marketplace.visualstudio.com/items?itemName=ckolkman.vscode-postgres
This is so that you can run your SQL queries from vscode by connecting your supabase postgres db. (Connection details are in settings > database, on the studio)
Deno - https://marketplace.visualstudio.com/items?itemName=denoland.vscode-deno
Installs the language server client for Deno, and has an inbuilt linter (removes the pesky lint errors if you use a standard Typescript linter)
pgFormatter or Prettier SQL VSCode - Optional linters for SQL
pgAdmin - I strongly recommend this. Is also my primary choice for diffing your environments (there's a CLI version that supabase launched, but I prefer this, since the diffing is not perfect in either cases, and a GUI helps).
Step 3 - Setting up your migrations
At this point, presumably, you have written some code, or are primed to do so. A good process we've found that works for us is this. In your dev branch, setup your code as follows:
Once you are ready to move to a production environment, create a new supabase project (or two if you need staging). Do read this from the supabase team as well - https://supabase.com/docs/guides/cli/cicd-workflow
supabase migration new <migration-name>
in your terminal. This should create a new empty sql file in your migration calledtimestamp_migration_name.sql
supabase db push
, and supabase will apply the new empty migration in your dev project. To confirm, you can check in the table that the latest timestamp has been added.Step 4 - Enabling CI / CD workflows
Our setup is such that when we push a code to our 'staging' branch, it automatically triggers the latest migrations, and builds all the edge functions again.
To enable this,
Make sure you have added your function names to the production runner file. If you have another staging project, create a separate file for that
Now everytime you create a commit to the main branch, it will run this code, which will link your prod project, apply any unapplied migrations, and redeploy all functions.
To confirm that everything is okay, I recommend doing the following actions
Run your postman collection with all your APIs on your prod environment to check that all APIs are working
Run a diff again between your dbs, to make sure there is no missed changes
Other random remarks
If you have any recommendations or improvements, please do DM
The CLI setup by supabase for diffing is also great, especially if you use migra (gives very concise results)
When diffing in pgAdmin, you can choose ignore owner and whitespace, else you'll get a lot of random diffs that are not needed
When supabase brings out environments within a project, all of this will become redundant, so hoping for that day soon
Beta Was this translation helpful? Give feedback.
All reactions