Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 0 additions & 37 deletions .github/workflows/deploy-production.yml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
name: Deploy to Staging
name: Deploy

on:
push:
branches:
- master
paths-ignore:
- release-notes.md

permissions:
contents: read

jobs:
deploy:
environment: staging
# Do not deploy in the main repository, only in user projects
if: github.repository_owner != 'fastapi'
runs-on:
- self-hosted
- staging
runs-on: self-hosted
env:
DOMAIN: ${{ secrets.DOMAIN }}
SECRET_KEY: ${{ secrets.SECRET_KEY }}
Expand Down
60 changes: 12 additions & 48 deletions deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ You can deploy the project using Docker Compose to a remote server.

The deployment Docker Compose configuration includes Traefik to handle HTTPS and route incoming traffic to the application.

You can use CI/CD (continuous integration and continuous deployment) systems to deploy automatically, there are already configurations to do it with GitHub Actions.
The included GitHub Actions workflow can deploy the application automatically.

But you have to configure a couple things first. 🤓

## Preparation

* Have a remote server ready and available. Use a separate server for each environment, for example one for staging and one for production.
* Have a remote server ready and available.
* Configure DNS records pointing to the server for the application domain and any supporting service subdomains you want to expose, e.g. `fastapi-project.example.com` and `adminer.fastapi-project.example.com`.
* Install and configure [Docker](https://docs.docker.com/engine/install/) on the remote server (Docker Engine, not Docker Desktop).

Expand Down Expand Up @@ -82,12 +82,15 @@ You can set several other environment variables:
* `EMAILS_FROM_EMAIL`: The email account to send emails from.
* `SENTRY_DSN`: The DSN for Sentry, if you are using it.

## GitHub Actions Environment Variables
## GitHub Repository Automation

There are some environment variables only used by GitHub Actions that you can configure:
Install the following GitHub Apps to enable the included repository automation:

* `LATEST_CHANGES`: Used by the GitHub Action [latest-changes](https://github.com/tiangolo/latest-changes) to automatically add release notes based on the PRs merged. It's a personal access token, read the docs for details.
* `SMOKESHOW_AUTH_KEY`: Used to handle and publish the code coverage using [Smokeshow](https://github.com/samuelcolvin/smokeshow), follow their instructions to create a (free) Smokeshow key.
* [Latest Changes](https://github.com/apps/latest-changes) updates `release-notes.md` when a pull request is merged.
* [PR Push](https://github.com/apps/pr-push) lets the pre-commit workflow push automated fixes to pull request branches.
* [PR Submit](https://github.com/apps/pr-submit) lets the **Bump pre-commit hooks** and **Prepare Release** workflows create pull requests.

To publish code coverage with [Smokeshow](https://github.com/samuelcolvin/smokeshow), add `SMOKESHOW_AUTH_KEY` as a repository secret.

### Deploy with Docker Compose

Expand All @@ -106,7 +109,7 @@ The `compose.deploy.yml` file adds the deployment settings to the shared configu

You can use GitHub Actions to deploy your project automatically. 😎

There are already two environment deployments configured, `staging` and `production`. Each environment should be deployed to a separate server. 🚀
The included `deploy.yml` workflow deploys the application whenever changes are pushed to `master`, including when a pull request is merged. 🚀

### Install GitHub Actions Runner

Expand Down Expand Up @@ -136,8 +139,6 @@ cd

* [Install a GitHub Action self-hosted runner following the official guide](https://docs.github.com/en/actions/hosting-your-own-runners/managing-self-hosted-runners/adding-self-hosted-runners#adding-a-self-hosted-runner-to-a-repository).

* When asked about labels, add a label for the environment, e.g. `production`. You can also add labels later.

After installing, the guide would tell you to run a command to start the runner. Nevertheless, it would stop once you terminate that process or if your local connection to your server is lost.

To make sure it runs on startup and continues running, you can install it as a service. To do that, exit the `github` user and go back to the `root` user:
Expand Down Expand Up @@ -180,17 +181,9 @@ cd /home/github/actions-runner

You can read more about it in the official guide: [Configuring the self-hosted runner application as a service](https://docs.github.com/en/actions/hosting-your-own-runners/managing-self-hosted-runners/configuring-the-self-hosted-runner-application-as-a-service).

### Configure GitHub Environments

The deployment workflows use [GitHub Environments](https://docs.github.com/en/actions/how-tos/deploy/configure-and-manage-deployments/manage-environments) for `staging` and `production`. This enables environment-specific secrets, deployment protection rules (e.g. required reviewers, wait timers), and deployment status tracking.

To configure them, go to your repository's **Settings** > **Environments** and create the `staging` and `production` environments.

### Set Secrets
### Set Repository Secrets

For each GitHub Environment (`staging` and `production`), configure the required secrets as [environment secrets](https://docs.github.com/en/actions/how-tos/write-workflows/choose-what-workflows-do/use-secrets#creating-secrets-for-an-environment). Environment secrets are preferred over [repository secrets](https://docs.github.com/en/actions/how-tos/write-workflows/choose-what-workflows-do/use-secrets#creating-secrets-for-a-repository) because they are scoped to the specific environment, reducing exposure and aligning with any protection rules you configure.

The deployment workflows require these secrets:
In your repository, go to **Settings** > **Secrets and variables** > **Actions** and add the following [repository secrets](https://docs.github.com/en/actions/how-tos/write-workflows/choose-what-workflows-do/use-secrets#creating-secrets-for-a-repository):

* `DOMAIN`
* `FIRST_SUPERUSER`
Expand All @@ -207,41 +200,12 @@ To enable emails, configure these additional secrets with the values from your e

To enable Sentry, configure the `SENTRY_DSN` secret.

## GitHub Action Deployment Workflows

There are GitHub Action workflows in the `.github/workflows` directory already configured for deploying to the environments (GitHub Actions runners with the labels):

* `staging`: after pushing (or merging) to the branch `master`.
* `production`: after publishing a release.

Both workflows are associated with their respective GitHub Environments, so deployments will be visible in the repository's **Environments** section and will respect any protection rules you configure.

### Prepare a Release

Install the [PR Submit GitHub App](https://github.com/apps/pr-submit) in your repository to enable the **Prepare Release** workflow.

Run the workflow manually from the **Actions** tab and select the version bump. It creates a pull request that updates the release notes. When you merge that pull request, the **Create Draft Release** workflow creates a draft GitHub release with the corresponding release notes.

Review and publish the draft release to trigger the production deployment.

If you need to add extra environments you could use those as a starting point.

## URLs

Replace `fastapi-project.example.com` with your domain.

### Production

Application (frontend and API): `https://fastapi-project.example.com`

Interactive API docs: `https://fastapi-project.example.com/docs`

Adminer: `https://adminer.fastapi-project.example.com`

### Staging

Application (frontend and API): `https://staging.fastapi-project.example.com`

Interactive API docs: `https://staging.fastapi-project.example.com/docs`

Adminer: `https://adminer.staging.fastapi-project.example.com`
2 changes: 1 addition & 1 deletion development.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ biome check..............................................................Passed

## URLs

The production or staging URLs would use these same paths, but with your own domain.
The deployed URLs use these same paths, but with your own domain.

### Development URLs

Expand Down