0Pricing
Supabase Backend as a Service · 课时

环境管理与持续集成/持续交付

学习管理不同环境(开发、预发布和生产),并将 Supabase 集成到持续集成/持续交付流水线中

环境管理与持续集成/持续交付 是 CoddyKit 上的免费 Supabase Backend as a Service 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Supabase Backend as a Service 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Supabase Backend as a Service 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Why Multiple Environments?

When building applications, it's crucial to manage different stages of development. These stages are called environments.

Typically, you'll work with three main environments:

  • Development (Dev): Where developers write and test new features.
  • Staging: A pre-production environment for testing by QA or stakeholders.
  • Production (Prod): The live application used by your end-users.

Each environment helps isolate changes and prevent issues in the live app.

Supabase & Your Environments

For effective environment management, it's best practice to have separate Supabase projects for each major environment.

This means you'd have:

  • One Supabase project for your development database, auth, storage.
  • Another Supabase project for staging.
  • A final, distinct Supabase project for production.

This isolation prevents accidental data loss or breaking changes from affecting your live application.

Managing Configuration

Each environment often requires different configurations, such as database connection strings, API keys, or external service URLs.

These sensitive details should never be hardcoded directly into your application's source code.

Instead, we use environment variables. These variables are loaded at runtime and provide the necessary configuration specific to the environment your application is running in.

Local Dev Setup with .env

During local development, you'll often use a .env file to manage your environment variables. This file stores key-value pairs specific to your local setup.

Tools like the Supabase client libraries can automatically load these variables, making it easy to switch between local and deployed environments.

Remember to add .env to your .gitignore to prevent accidentally committing sensitive information!

SUPABASE_URL="http://localhost:54321"
SUPABASE_ANON_KEY="YOUR_LOCAL_ANON_KEY"
DATABASE_URL="postgresql://postgres:password@localhost:54322/db"

Introduction to CI/CD

CI/CD stands for Continuous Integration and Continuous Deployment (or Delivery). It's a set of practices that automate the building, testing, and deployment of software.

For Supabase, CI/CD helps you:

  • Automatically apply database schema changes.
  • Deploy Edge Functions.
  • Ensure consistent deployments across environments.

This automation reduces manual errors and speeds up your development cycle.

Database Migrations

As your application evolves, your database schema will change. Database migrations are version-controlled scripts that track and apply these changes.

The Supabase CLI provides powerful tools for managing migrations:

  • supabase migration new <name>: Creates a new migration file.
  • supabase db diff: Generates a migration script by comparing your local schema to your Supabase project.
  • supabase db push: Applies local migrations to your Supabase project.

These commands are essential for safely evolving your database schema.

# Create a new migration file
supabase migration new create_users_table

# Generate migration from local changes
supabase db diff -f new_feature_migration

# Apply all pending migrations
supabase db push

CI/CD Workflow Overview

A typical CI/CD pipeline for a Supabase project might involve these steps:

  • Build: Compile your application code (e.g., client-side app, Edge Functions).
  • Test: Run unit and integration tests against a development or staging Supabase project.
  • Deploy Schema: Apply pending database migrations to the target Supabase project.
  • Deploy Functions: Deploy updated Supabase Edge Functions.
  • Deploy Application: Deploy your client-side application (e.g., to Vercel, Netlify).

Each step ensures your changes are validated before reaching production.

Automating Deployments

You can use CI/CD platforms like GitHub Actions, GitLab CI, or CircleCI to automate these steps. For Supabase, a common task is to automate database migrations.

Here's a conceptual snippet of a GitHub Actions step that could apply migrations to a Supabase project. It uses the Supabase CLI and environment variables for authentication.

# This is a conceptual snippet for a CI/CD pipeline.
# It assumes SUPABASE_ACCESS_TOKEN and SUPABASE_PROJECT_REF
# are set as secrets in your CI/CD environment.

- name: Apply Supabase Migrations
  run: |
    # Install Supabase CLI
    npm i -g supabase

    # Link to your Supabase project (e.g., staging)
    supabase link --project-ref ${{ secrets.SUPABASE_PROJECT_REF_STAGING }}

    # Apply migrations
    supabase db push
  env:
    SUPABASE_ACCESS_TOKEN: ${{ secrets.SUPABASE_ACCESS_TOKEN }}

Quick Check

Which of the following are good practices when managing Supabase projects across different environments?

Recap: Secure & Streamlined

In this lesson, we explored how to effectively manage different environments (development, staging, production) for your Supabase projects. We also introduced the power of CI/CD.

  • Separate Projects: Essential for isolating environments.
  • Environment Variables: Securely manage configuration.
  • Supabase CLI Migrations: Automate database schema changes.
  • CI/CD: Streamline deployments and reduce errors.

By adopting these practices, you can build more robust, secure, and scalable applications with Supabase.

常见问题解答

「环境管理与持续集成/持续交付」课时是免费的吗?

是的 — 「环境管理与持续集成/持续交付」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Supabase Backend as a Service 课程的其余内容,请升级到 CoddyKit PRO。 Supabase Backend as a Service 课程共包含 4 节课。

「环境管理与持续集成/持续交付」这节课中我会学到什么?

学习管理不同环境(开发、预发布和生产),并将 Supabase 集成到持续集成/持续交付流水线中 你通过在浏览器中直接运行的动手代码来练习 Supabase Backend as a Service,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Supabase Backend as a Service 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Supabase Backend as a Service 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「环境管理与持续集成/持续交付」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Supabase Backend as a Service 课中编写并运行代码吗?

能。每节 Supabase Backend as a Service 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 环境管理与持续集成/持续交付
  2. 备份、恢复与灾难恢复
  3. 安全审计与最佳实践
  4. 监控、日志记录与可观测性
← 返回 Supabase Backend as a Service