Skip to content

Instantly share code, notes, and snippets.

@jrr
Last active May 8, 2025 03:24
Show Gist options
  • Save jrr/5122300ea2ccd7543a90c7fd10741f86 to your computer and use it in GitHub Desktop.
Save jrr/5122300ea2ccd7543a90c7fd10741f86 to your computer and use it in GitHub Desktop.
An example GitHub CI pipeline for deploying a fly/neon review app
# based on https://github.com/neondatabase/preview-branches-with-fly
name: Clean up Review App
on:
pull_request:
types: [closed]
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }} # You can generate a Fly API token in your account settings
jobs:
delete-preview:
runs-on: ubuntu-latest
steps:
- name: Delete Fly app
uses: superfly/[email protected]
- name: Delete Neon Branch
uses: neondatabase/[email protected]
with:
project_id: ${{ vars.NEON_PROJECT_ID }}
branch: preview/pr-${{ github.event.number }}
api_key: ${{ secrets.NEON_API_KEY }}
# based on https://github.com/neondatabase/preview-branches-with-fly
name: Deploy Review App
on:
pull_request:
types: [opened, reopened, synchronize]
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
FLY_REGION: iad
FLY_ORG: my-org
NEON_PROJECT_ID: ${{ vars.NEON_PROJECT_ID }} # You can find this in your Neon project settings
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Required for commenting on pull requests for private repos
jobs:
fork-neon:
runs-on: ubuntu-latest
outputs:
# The url is base64-encoded so that it can be passed between jobs.
# You won't have to do this if you put all your steps in one job.
db_url_b64: ${{ steps.encode-db-url.outputs.encoded_url }}
branch_id: ${{ steps.create-neon-branch.outputs.branch_id }}
# Only run one deployment at a time per PR.
concurrency:
group: pr-setup-${{ github.event.pull_request.number }}
steps:
- uses: actions/checkout@v4
- name: Get branch name
id: branch-name
uses: tj-actions/branch-names@v8
# if the branch already exists, you'll see error messages in the CI log
# ("ERROR: branch already exists"), but the action will still succeed.
- id: create-neon-branch
name: Branch DB
uses: neondatabase/create-branch-action@v6
with:
project_id: ${{ env.NEON_PROJECT_ID }}
role: "my-db-owner"
parent_branch: develop
database: herd
branch_name: preview/pr-${{ github.event.number }}
api_key: ${{ secrets.NEON_API_KEY }} # Generate an API key in your Neon account settings
- name: Encode DB URL
id: encode-db-url
run: |
ENCODED_URL=$(echo -n "${{ steps.create-neon-branch.outputs.db_url }}" | base64 -w 0)
echo "encoded_url=$ENCODED_URL" >> $GITHUB_OUTPUT
migrate-db:
runs-on: ubuntu-latest
needs: fork-neon
# Only run one database migration at a time per PR.
concurrency:
group: pr-db-${{ github.event.pull_request.number }}
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
with:
version: 10.7.1
- uses: actions/setup-node@v4
with:
node-version: 22
cache: "pnpm"
- run: pnpm install
- name: Decode and use DB URL
id: decode-db-url
run: |
DB_URL=$(echo -n "${{ needs.fork-neon.outputs.db_url_b64 }}" | base64 -d)
echo "DATABASE_URL=$DB_URL" >> $GITHUB_ENV
- name: Migrate database
working-directory: packages/database
run: pnpm apply-migrations
fly-deploy:
runs-on: ubuntu-latest
needs: [fork-neon, migrate-db]
outputs:
url: ${{ steps.fly-deploy.outputs.url }}
# Only run one deployment at a time per PR.
concurrency:
group: pr-app-${{ github.event.pull_request.number }}
# This allows GitHub to link to the app from the PR
environment:
name: review
url: ${{ steps.fly-deploy.outputs.url }}
steps:
- uses: actions/checkout@v4
- name: Decode DB URL
id: decode-db-url
run: |
DB_URL=$(echo -n "${{ needs.fork-neon.outputs.db_url_b64 }}" | base64 -d)
echo "db_url=$DB_URL" >> $GITHUB_OUTPUT
- id: fly-deploy
name: Deploy Fly Preview App
uses: superfly/[email protected]
with:
secrets: |
DATABASE_URL=${{ steps.decode-db-url.outputs.db_url }}
pr-comment:
runs-on: ubuntu-latest
needs: [fork-neon, fly-deploy]
# Run this job whenever the fly-deploy job succeeds, regardless of PR action type
steps:
- name: Comment on Pull Request
uses: thollander/actions-comment-pull-request@v3
with:
github-token: ${{ env.GH_TOKEN }} # Required for commenting on pull requests for private repos
comment-tag: preview_app_urls
message: |
Fly Preview URL :balloon: : ${{ needs.fly-deploy.outputs.url }}
Neon branch :elephant: : https://console.neon.tech/app/projects/${{ env.NEON_PROJECT_ID }}/branches/${{ needs.fork-neon.outputs.branch_id }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment