From 02c785e0969241cd947c293f20aafe33ff1d4dcd Mon Sep 17 00:00:00 2001
From: yujonglee <yujonglee.dev@gmail.com>
Date: Sun, 20 Oct 2024 20:19:24 +0900
Subject: [PATCH] add workflows for preview deployment

---
 .github/workflows/preview_core.yaml         | 34 +++++++++++++++++++++
 .github/workflows/preview_core_cleanup.yaml | 17 +++++++++++
 2 files changed, 51 insertions(+)
 create mode 100644 .github/workflows/preview_core.yaml
 create mode 100644 .github/workflows/preview_core_cleanup.yaml

diff --git a/.github/workflows/preview_core.yaml b/.github/workflows/preview_core.yaml
new file mode 100644
index 00000000..559a1417
--- /dev/null
+++ b/.github/workflows/preview_core.yaml
@@ -0,0 +1,34 @@
+# https://github.com/neondatabase/preview-branches-with-fly
+on: [pull_request]
+env:
+  NEON_PROJECT_ID: ${{ vars.NEON_PROJECT_ID }}
+  FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
+jobs:
+  preview:
+    runs-on: ubuntu-latest
+    concurrency:
+      group: pr-${{ github.event.number }}
+    steps:
+      - uses: actions/checkout@v4
+      - id: branch-name
+        uses: tj-actions/branch-names@v8
+      - id: create-branch
+        uses: neondatabase/create-branch-action@v5
+        with:
+          project_id: ${{ env.NEON_PROJECT_ID }}
+          username: canary_prod_owner
+          database: canary_prod
+          branch_name: preview/${{ steps.branch-name.outputs.current_branch }}
+          api_key: ${{ secrets.NEON_API_KEY }}
+      - run: |
+          echo "DATABASE_URL=${{ steps.create-branch.outputs.db_url_with_pooler }}" >> "$GITHUB_ENV"
+      - id: deploy
+        uses: superfly/fly-pr-review-apps@1.2.1
+        with:
+          path: core
+          secrets: DATABASE_URL=$DATABASE_URL
+      - uses: thollander/actions-comment-pull-request@v2
+        with:
+          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 }}
diff --git a/.github/workflows/preview_core_cleanup.yaml b/.github/workflows/preview_core_cleanup.yaml
new file mode 100644
index 00000000..901139ae
--- /dev/null
+++ b/.github/workflows/preview_core_cleanup.yaml
@@ -0,0 +1,17 @@
+# https://github.com/neondatabase/preview-branches-with-fly
+on:
+  pull_request:
+    types: [closed]
+env:
+  NEON_PROJECT_ID: ${{ vars.NEON_PROJECT_ID }}
+  FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
+jobs:
+  cleanup:
+    runs-on: ubuntu-latest
+    steps:
+      - uses: superfly/fly-pr-review-apps@1.2.0
+      - uses: neondatabase/delete-branch-action@v3.1.3
+        with:
+          project_id: ${{ env.NEON_PROJECT_ID }}
+          branch: preview/${{ github.event.pull_request.head.ref }}
+          api_key: ${{ secrets.NEON_API_KEY }}