Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the workflow #13

Merged
merged 4 commits into from
May 8, 2024
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
2 changes: 1 addition & 1 deletion .github/workflows/cleanup-preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ jobs:
uses: neondatabase/[email protected]
with:
project_id: ${{ var.NEON_PROJECT_ID }}
branch: preview/pr-${{ github.event.number }}-${{ github.event.pull_request.head.ref }}
branch: preview/${{ github.event.pull_request.head.ref }}
api_key: ${{ secrets.NEON_API_KEY }}
13 changes: 7 additions & 6 deletions .github/workflows/deploy-preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Preview Deployment
on: [pull_request]

env:
NEON_PROJECT_ID: ${{ var.NEON_PROJECT_ID }} # You can find this in your Neon project settings
NEON_PROJECT_ID: ${{ vars.NEON_PROJECT_ID }} # You can find this in your Neon project settings
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }} # You can generate a Fly API token in your account settings
GH_TOKEN: ${{ secrets.GH_TOKEN }} # Required for commenting on pull requests for private repos

Expand Down Expand Up @@ -31,27 +31,28 @@ jobs:
uses: tj-actions/branch-names@v8

- id: create-branch
uses: neondatabase/create-branch-action@v4
uses: neondatabase/create-branch-action@v5
with:
project_id: ${{ env.NEON_PROJECT_ID }}
username: "neondb_owner" # Change this to the role you use to connect to your database
# parent: dev # optional (defaults to your primary branch)
branch_name: preview/pr-${{ github.event.number }}-${{ steps.branch-name.outputs.current_branch }}
branch_name: preview/${{ steps.branch-name.outputs.current_branch }}
api_key: ${{ secrets.NEON_API_KEY }} # Generate a an API key in your Neon account settings

- run: |
echo "DATABASE_URL=${{ steps.create-branch.outputs.db_url_with_pooler }}?sslmode=require" >> "$GITHUB_ENV"
echo "DATABASE_URL=${{ steps.create-branch.outputs.db_url_with_pooler }}" >> "$GITHUB_ENV"

- run: pnpm run db:migrate

- id: deploy
uses: superfly/[email protected].0
uses: superfly/[email protected].1
with:
secrets: DATABASE_URL=$DATABASE_URL

- name: Comment on Pull Request
uses: thollander/actions-comment-pull-request@v2
with:
# GITHUB_TOKEN: ${{ env.GH_TOKEN }} # Required for commenting on pull requests for private repos
GITHUB_TOKEN: ${{ env.GH_TOKEN }} # Required for commenting on pull requests for private repos
message: |
Fly Preview URL :balloon: : ${{ steps.deploy.outputs.url }}
Neon branch :elephant: : https://console.neon.tech/app/projects/${{ env.NEON_PROJECT_ID }}/branches/${{ steps.create-branch.outputs.branch_id }}
41 changes: 20 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ You can copy the files located at [`.github/workflows/`](./.github/workflows/) a
You will then need to set the following secrets in your repository:

- `FLY_API_TOKEN`: Your Fly.io API token, you can find it in your Fly.io account settings.
- `NEON_PROJECT_ID`: The ID of your Neon project, you can find it in your Neon project settings.
- `NEON_API_KEY`: Your Neon API key, you can find it in your Neon account settings.
- `NEON_DATABASE_USERNAME`: The username for your Neon database. This is the same as the username for your production database.
- `DATABASE_URL`: The connection string for your production database. You can find it in your Neon project's connection details.
- `GH_TOKEN`: A GitHub token with access to your repository, you can create one in your GitHub account settings. You will need to give it access to the `repo` scope so that the `deploy-preview` workflow can comment on the pull request. You can uncomment the step which uses this token in the `.github/workflows/deploy-preview.yml` workflow file.

You will then need to set the following variables:

- `NEON_PROJECT_ID`: The ID of your Neon project, you can find it in your Neon project settings.

## How it works

### Preview deployment
Expand All @@ -32,7 +34,7 @@ Activated on a `pull_request` event
on: [pull_request]
```

Requires the `NEON_API_KEY`, `NEON_PROJECT_ID`, `GH_TOKEN`, `FLY_API_TOKEN` and `NEON_DATABASE_USERNAME` secrets that are set in the repository.
Requires the `NEON_API_KEY`, `NEON_PROJECT_ID`, `GH_TOKEN`, and `FLY_API_TOKEN` secrets that are set in the repository.

The workflow has a single job called `deploy-preview`
```yaml
Expand All @@ -48,66 +50,63 @@ In that job it consists of the following steps:
group: pr-${{ github.event.number }}
```

2. Check out the codebase using [`actions/checkout@v4`](https://github.com/marketplace/actions/checkout).
1. Check out the codebase using [`actions/checkout@v4`](https://github.com/marketplace/actions/checkout).
```yaml
- uses: actions/checkout@v4
```
3. Sets up PNPM with [`pnpm/action-setup@v2`](https://github.com/marketplace/actions/setup-pnpm). (You can use another package manager depending on your setup.)
1. Sets up PNPM with [`pnpm/action-setup@v2`](https://github.com/marketplace/actions/setup-pnpm). (You can use another package manager depending on your setup.)
```yaml
- uses: pnpm/action-setup@v2
with:
version: 8
```
4. Configures Node.js version with caching for PNPM via `actions/setup-node@v4`.
1. Configures Node.js version with caching for PNPM via `actions/setup-node@v4`.
```yaml
- name: Use Node.js 18
uses: actions/setup-node@v4
with:
node-version: 18
cache: "pnpm"
```
5. Installs dependencies using `pnpm install`.
1. Installs dependencies using `pnpm install`.
```yaml
- name: Install dependencies
run: pnpm install
```
6. Retrieves and normalizes the branch name using [`tj-actions/branch-names@v8`](https://github.com/marketplace/actions/branch-names).
1. Retrieves and normalizes the branch name using [`tj-actions/branch-names@v8`](https://github.com/marketplace/actions/branch-names).
```yaml
- name: Get branch name
id: branch-name
uses: tj-actions/branch-names@v8
```
7. Creates a Neon database branch for the pull request with [`neondatabase/create-branch-action@v4`](https://github.com/marketplace/actions/neon-database-create-branch-action). By default, the branch name will be `preview/<git-branch-name>-<commit_SHA>`
1. Creates a Neon database branch for the pull request with [`neondatabase/create-branch-action@v5`](https://github.com/marketplace/actions/neon-database-create-branch-action). By default, the branch name will be `preview/<git-branch-name>`
```yaml
- name: Create Neon Branch
id: create-branch
uses: neondatabase/create-branch-action@v4
with:
project_id: ${{ env.NEON_PROJECT_ID }}
# parent: dev # optional (defaults to your primary branch)
branch_name: preview/pr-${{ github.event.number }}-${{ steps.branch-name.outputs.current_branch }}
username: ${{ env.NEON_DATABASE_USERNAME }}
branch_name: preview/${{ steps.branch-name.outputs.current_branch }}
username: "neondb_owner" # change this to your Neon database username if you're not using the default
api_key: ${{ env.NEON_API_KEY }}
```
8. Executes database migrations by setting up the `.env` file and running migration scripts.
1. Executes database migrations by setting up the `.env` file and running migration scripts.
```
- name: Run Migrations
run: |
touch .env

echo DATABASE_URL=${{ steps.create-branch.outputs.db_url_with_pooler }}?sslmode=require >> .env
- run: |
echo "DATABASE_URL=${{ steps.create-branch.outputs.db_url_with_pooler }}" >> "$GITHUB_ENV"

pnpm run db:migrate
- run: pnpm run db:migrate
```
9. Deploys the application with [`superfly/[email protected].0`](https://github.com/marketplace/actions/github-action-for-deplying-staging-apps-on-fly-io), while including the Neon database URL.
1. Deploys the application with [`superfly/[email protected].1`](https://github.com/marketplace/actions/github-action-for-deplying-staging-apps-on-fly-io), while including the Neon database URL.
```yaml
- name: Deploy
id: deploy
uses: superfly/[email protected].0
uses: superfly/[email protected].1
with:
secrets: DATABASE_URL=${{ steps.create-branch.outputs.db_url }}?sslmode=require
```
10. Comments on the pull request with deployment and database branch details using `thollander/actions-comment-pull-request@v2`. Here's an [example comment](https://github.com/neondatabase/preview-branches-with-fly/pull/9#issuecomment-1924660371)
1. Comments on the pull request with deployment and database branch details using `thollander/actions-comment-pull-request@v2`. Here's an [example comment](https://github.com/neondatabase/preview-branches-with-fly/pull/9#issuecomment-1924660371)
```yaml
- name: Comment on Pull Request
uses: thollander/actions-comment-pull-request@v2
Expand Down
Loading