fly-pr-review-apps/README.md

138 lines
5.6 KiB
Markdown
Raw Permalink Normal View History

2022-03-08 18:40:04 +00:00
# PR Review Apps on Fly.io
2019-09-19 20:50:53 +00:00
This GitHub action wraps the Fly.io CLI to automatically deploy pull requests to [fly.io](http://fly.io) for review. These are useful for testing changes on a branch without having to setup explicit staging environments.
2019-09-19 20:50:53 +00:00
2022-01-29 19:53:26 +00:00
This action will create, deploy, and destroy Fly apps. Just set an Action Secret for `FLY_API_TOKEN`.
If you have an existing `fly.toml` in your repo, this action will copy it with a new name when deploying. By default, Fly apps will be named with the scheme `pr-{number}-{repo_org}-{repo_name}`.
## Inputs
| name | description |
| ---------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `name` | The name of the Fly app. Alternatively, set the env `FLY_APP`. For safety, must include the PR number. Example: `myapp-pr-${{ github.event.number }}`. Defaults to `pr-{number}-{repo_org}-{repo_name}`. |
| `region` | Which Fly region to run the app in. Alternatively, set the env `FLY_REGION`. Defaults to `iad`. |
| `org` | Which Fly organization to launch the app under. Alternatively, set the env `FLY_ORG`. Defaults to `personal`. |
| `path` | Path to run the `flyctl` commands from. Useful if you have an existing `fly.toml` in a subdirectory. |
| `postgres` | Optional name of an existing Postgres cluster to `flyctl postgres attach` to. |
| `update` | Whether or not to update this Fly app when the PR is updated. Default `true`. |
2022-09-02 18:05:14 +00:00
| `config` | Optional path to a custom Fly toml config. Config path should be relative to `path` parameter, if specified.
2022-01-29 19:53:26 +00:00
## Required Secrets
2022-03-11 12:10:30 +00:00
`FLY_API_TOKEN` - **Required**. The token to use for authentication. You can find a token by running `flyctl auth token` or going to your [user settings on fly.io](https://fly.io/user/personal_access_tokens).
2022-01-29 19:53:26 +00:00
## Basic Example
2019-09-19 20:50:53 +00:00
```yaml
2022-01-29 19:53:26 +00:00
name: Staging App
on:
pull_request:
types: [opened, reopened, synchronize, closed]
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
FLY_REGION: iad
FLY_ORG: personal
2019-09-19 20:50:53 +00:00
jobs:
2022-01-29 19:53:26 +00:00
staging_app:
2019-09-19 20:50:53 +00:00
runs-on: ubuntu-latest
2022-01-29 19:53:26 +00:00
# Only run one deployment at a time per PR.
concurrency:
group: pr-${{ github.event.number }}
# Create a GitHub deployment environment per staging app so it shows up
# in the pull request UI.
environment:
name: pr-${{ github.event.number }}
url: ${{ steps.deploy.outputs.url }}
2019-09-19 20:50:53 +00:00
steps:
2020-01-23 16:41:48 +00:00
- uses: actions/checkout@v2
2022-01-29 19:53:26 +00:00
- name: Deploy
id: deploy
2022-01-29 19:55:10 +00:00
uses: brentd/fly-staging-app@v1
2022-01-29 19:53:26 +00:00
```
## Cleaning up GitHub environments
This action will destroy the Fly app, but it will not destroy the GitHub environment, so those will hang around in the GitHub UI. If this is bothersome, use an action like `strumwolf/delete-deployment-environment` to delete the environment when the PR is closed.
```yaml
on:
pull_request:
types: [opened, reopened, synchronize, closed]
# ...
jobs:
staging_app:
# ...
# Create a GitHub deployment environment per review app.
environment:
name: pr-${{ github.event.number }}
url: ${{ steps.deploy.outputs.url }}
steps:
- uses: actions/checkout@v2
- name: Deploy app
id: deploy
2022-01-29 19:55:10 +00:00
uses: brentd/fly-staging-app@v1
2022-01-29 19:53:26 +00:00
- name: Clean up GitHub environment
uses: strumwolf/delete-deployment-environment@v2
if: ${{ github.event.action == 'closed' }}
2019-09-19 20:50:53 +00:00
with:
2022-01-29 19:53:26 +00:00
# ⚠️ The provided token needs permission for admin write:org
token: ${{ secrets.GITHUB_TOKEN }}
environment: pr-${{ github.event.number }}
2019-09-19 20:50:53 +00:00
```
2022-01-29 19:53:26 +00:00
## Example with Postgres cluster
2019-09-19 20:50:53 +00:00
2022-01-29 19:53:26 +00:00
If you have an existing [Fly Postgres cluster](https://fly.io/docs/reference/postgres/) you can attach it using the `postgres` action input. `flyctl postgres attach` will be used, which automatically creates a new database in the cluster named after the Fly app and sets `DATABASE_URL`.
2019-09-19 20:50:53 +00:00
2022-01-29 19:53:26 +00:00
For production apps, it's a good idea to create a new Postgres cluster specifically for staging apps.
```yaml
# ...
steps:
- uses: actions/checkout@v2
- name: Deploy app
id: deploy
2022-01-29 19:55:10 +00:00
uses: brentd/fly-staging-app@v1
2022-01-29 19:53:26 +00:00
with:
postgres: myapp-postgres-staging-apps
```
## Example with multiple Fly apps
If you need to run multiple Fly apps per staging app, for example Redis, memcached, etc, just give each app a unique name. Your application code will need to be able to discover the app hostnames.
Redis example:
```yaml
steps:
- uses: actions/checkout@v2
2022-01-29 19:55:10 +00:00
- name: Deploy redis
uses: brentd/fly-staging-app@v1
2022-01-29 19:53:26 +00:00
with:
2022-01-29 20:53:29 +00:00
update: false # Don't need to re-deploy redis when the PR is updated
path: redis # Keep fly.toml in a subdirectory to avoid confusing flyctl
image: flyio/redis:6.2.6
2022-01-29 19:53:26 +00:00
name: pr-${{ github.event.number }}-myapp-redis
- name: Deploy app
id: deploy
2022-01-29 19:55:10 +00:00
uses: brentd/fly-staging-app@v1
2022-01-29 19:53:26 +00:00
with:
name: pr-${{ github.event.number }}-myapp-app
```