Skip to content

Commit f03db72

Browse files
authored
✨ Add support for deploying multiple environments (staging, production) to the same server (#1128)
1 parent 4c8837c commit f03db72

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

.github/workflows/deploy-production.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ jobs:
1313
env:
1414
ENVIRONMENT: production
1515
DOMAIN: ${{ secrets.DOMAIN_PRODUCTION }}
16+
STACK_NAME: ${{ secrets.STACK_NAME_PRODUCTION }}
1617
SECRET_KEY: ${{ secrets.SECRET_KEY }}
1718
FIRST_SUPERUSER: ${{ secrets.FIRST_SUPERUSER }}
1819
FIRST_SUPERUSER_PASSWORD: ${{ secrets.FIRST_SUPERUSER_PASSWORD }}
@@ -25,5 +26,5 @@ jobs:
2526
steps:
2627
- name: Checkout
2728
uses: actions/checkout@v4
28-
- run: docker compose -f docker-compose.yml build
29-
- run: docker compose -f docker-compose.yml up -d
29+
- run: docker compose -f docker-compose.yml --project-name ${{ secrets.STACK_NAME_PRODUCTION }} build
30+
- run: docker compose -f docker-compose.yml --project-name ${{ secrets.STACK_NAME_PRODUCTION }} up -d

.github/workflows/deploy-staging.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ jobs:
1313
env:
1414
ENVIRONMENT: staging
1515
DOMAIN: ${{ secrets.DOMAIN_STAGING }}
16+
STACK_NAME: ${{ secrets.STACK_NAME_STAGING }}
1617
SECRET_KEY: ${{ secrets.SECRET_KEY }}
1718
FIRST_SUPERUSER: ${{ secrets.FIRST_SUPERUSER }}
1819
FIRST_SUPERUSER_PASSWORD: ${{ secrets.FIRST_SUPERUSER_PASSWORD }}
@@ -25,5 +26,5 @@ jobs:
2526
steps:
2627
- name: Checkout
2728
uses: actions/checkout@v4
28-
- run: docker compose -f docker-compose.yml build
29-
- run: docker compose -f docker-compose.yml up -d
29+
- run: docker compose -f docker-compose.yml --project-name ${{ secrets.STACK_NAME_STAGING }} build
30+
- run: docker compose -f docker-compose.yml --project-name ${{ secrets.STACK_NAME_STAGING }} up -d

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ Before deploying it, make sure you change at least the values for:
135135
- `FIRST_SUPERUSER_PASSWORD`
136136
- `POSTGRES_PASSWORD`
137137

138+
You can (and should) pass these as environment variables from secrets.
139+
140+
Read the [deployment.md](./deployment.md) docs for more details.
141+
138142
### Generate Secret Keys
139143

140144
Some environment variables in the `.env` file have a default value of `changethis`.
@@ -196,7 +200,7 @@ But don't worry, you can just update any of that in the `.env` files afterwards.
196200
The input variables, with their default values (some auto generated) are:
197201

198202
- `project_name`: (default: `"FastAPI Project"`) The name of the project, shown to API users (in .env).
199-
- `stack_name`: (default: `"fastapi-project"`) The name of the stack used for Docker Compose labels (no spaces) (in .env).
203+
- `stack_name`: (default: `"fastapi-project"`) The name of the stack used for Docker Compose labels and project name (no spaces, no periods) (in .env).
200204
- `secret_key`: (default: `"changethis"`) The secret key for the project, used for security, stored in .env, you can generate one with the method above.
201205
- `first_superuser`: (default: `"admin@example.com"`) The email of the first superuser (in .env).
202206
- `first_superuser_password`: (default: `"changethis"`) The password of the first superuser (in .env).

deployment.md

+6
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ export DOMAIN=fastapi-project.example.com
127127

128128
You can set several variables, like:
129129

130+
* `PROJECT_NAME`: The name of the project, used in the API for the docs and emails.
131+
* `STACK_NAME`: The name of the stack used for Docker Compose labels and project name, this should be different for `staging`, `production`, etc. You could use the same domain replacing dots with dashes, e.g. `fastapi-project-example-com` and `staging-fastapi-project-example-com`.
130132
* `BACKEND_CORS_ORIGINS`: A list of allowed CORS origins separated by commas.
131133
* `SECRET_KEY`: The secret key for the FastAPI project, used to sign tokens.
132134
* `FIRST_SUPERUSER`: The email of the first superuser, this superuser will be the one that can create new users.
@@ -254,11 +256,15 @@ The current Github Actions workflows expect these secrets:
254256

255257
* `DOMAIN_PRODUCTION`
256258
* `DOMAIN_STAGING`
259+
* `STACK_NAME_PRODUCTION`
260+
* `STACK_NAME_STAGING`
257261
* `EMAILS_FROM_EMAIL`
258262
* `FIRST_SUPERUSER`
259263
* `FIRST_SUPERUSER_PASSWORD`
260264
* `POSTGRES_PASSWORD`
261265
* `SECRET_KEY`
266+
* `LATEST_CHANGES`
267+
* `SMOKESHOW_AUTH_KEY`
262268

263269
## GitHub Action Deployment Workflows
264270

0 commit comments

Comments
 (0)