From 37777e4f63e512ef636dafafa69112910e629cc9 Mon Sep 17 00:00:00 2001 From: OverDsh Date: Tue, 23 Dec 2025 17:33:54 +0100 Subject: [PATCH 1/3] Enhancement: Feature branches now creates a deployment only on PRs. --- .github/scripts/generate_db_name.sh | 22 ----- .github/workflows/database_cleanup.yml | 30 +----- .github/workflows/dev_deploy.yml | 39 ++++++++ .github/workflows/pr_preview.yml | 124 +++++++++++++++++++++++++ .github/workflows/preview_deploy.yml | 83 ----------------- 5 files changed, 168 insertions(+), 130 deletions(-) delete mode 100644 .github/scripts/generate_db_name.sh create mode 100644 .github/workflows/dev_deploy.yml create mode 100644 .github/workflows/pr_preview.yml delete mode 100644 .github/workflows/preview_deploy.yml diff --git a/.github/scripts/generate_db_name.sh b/.github/scripts/generate_db_name.sh deleted file mode 100644 index 7125962..0000000 --- a/.github/scripts/generate_db_name.sh +++ /dev/null @@ -1,22 +0,0 @@ -#!/bin/bash - -# Get the branch name from the first argument -RAW_BRANCH=$1 - -# 1. Lowercase -# 2. Replace any non-alphanumeric with - -# 3. Trim leading/trailing - -# 4. Collapse multiple -- to single - -SAFE_NAME=$(echo "$RAW_BRANCH" \ - | tr '[:upper:]' '[:lower:]' \ - | sed 's/[^a-z0-9]/-/g' \ - | sed 's/--*/-/g' \ - | sed 's/^-//;s/-$//') - -# Ensure it starts with a letter (Turso requirement) -if [[ -z "$SAFE_NAME" || "$SAFE_NAME" =~ ^[0-9] ]]; then - SAFE_NAME="br-$SAFE_NAME" -fi - -# Output the result for GitHub Actions -echo "$SAFE_NAME" diff --git a/.github/workflows/database_cleanup.yml b/.github/workflows/database_cleanup.yml index ef31a71..311e490 100644 --- a/.github/workflows/database_cleanup.yml +++ b/.github/workflows/database_cleanup.yml @@ -1,42 +1,22 @@ name: Cleanup Database Branch on: - delete: - branches-ignore: - - main - - dev + pull_request: + types: + - closed workflow_dispatch: jobs: cleanup_database: - if: ${{ github.event.ref_type == 'branch' }} runs-on: ubuntu-latest steps: - - name: Checkout code - uses: actions/checkout@v4 - with: - # Checkout the main branch to retrieve the scripts - ref: main - - name: Install Turso CLI run: | curl -sSfL https://get.tur.so/install.sh | bash echo "$HOME/.turso" >> $GITHUB_PATH - - name: Generate Safe DB Name - run: | - # Make the script executable, might not be necessary - chmod +x .github/scripts/generate_db_name.sh - - # Get the deleted branch name - RAW_REF="${{ github.event.ref }}" - CLEAN_REF="${RAW_REF#refs/heads/}" - - # Generate the safe branch name and store it in an environment variable - BRANCH_NAME=$(.github/scripts/generate_db_name.sh "$CLEAN_REF") - echo "SAFE_BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV - - name: Destroy Turso Database env: TURSO_API_TOKEN: ${{ secrets.TURSO_API_TOKEN }} run: | - turso db destroy ${{env.SAFE_BRANCH_NAME}} --yes || echo "Database already gone" + DB_NAME="pr-${{ github.event.pull_request.number }}" + turso db destroy "$DB_NAME" --yes || echo "Database already gone or never existed" diff --git a/.github/workflows/dev_deploy.yml b/.github/workflows/dev_deploy.yml new file mode 100644 index 0000000..714fff6 --- /dev/null +++ b/.github/workflows/dev_deploy.yml @@ -0,0 +1,39 @@ +name: Make Development Deployment + +on: + push: + branches: + - dev + +concurrency: + group: deployment-dev + cancel-in-progress: true + +jobs: + deploy: + runs-on: ubuntu-latest + steps: + - name: Checkout Code + uses: actions/checkout@v4 + + - uses: actions/setup-node@v4 + with: + node-version: 20 + cache: "npm" + + - run: npm ci + + - name: Push changes to database + env: + TURSO_DATABASE_URL: ${{ secrets.DEV_DB_URL }} + TURSO_AUTH_TOKEN: ${{ secrets.DEV_DB_TOKEN }} + run: | + npx drizzle-kit push + + - name: Make Development Deployment + env: + VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }} + VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }} + VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }} + run: | + npx vercel deploy --target preview --yes --token ${{ secrets.VERCEL_TOKEN }} diff --git a/.github/workflows/pr_preview.yml b/.github/workflows/pr_preview.yml new file mode 100644 index 0000000..3d22746 --- /dev/null +++ b/.github/workflows/pr_preview.yml @@ -0,0 +1,124 @@ +name: Make Pull Request Preview Deployment +on: + pull_request: + types: + - opened + - reopened + - synchronize + workflow_dispatch: + +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number }} + cancel-in-progress: true + +permissions: + pull-requests: write + contents: read + +jobs: + deploy: + environment: Preview + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - uses: actions/setup-node@v4 + with: + node-version: 20 + cache: "npm" + + - run: npm ci + + - name: Install Turso CLI + run: | + curl -sSfL https://get.tur.so/install.sh | bash + echo "$HOME/.turso" >> $GITHUB_PATH + + - name: Set DB Name + run: | + echo "SAFE_BRANCH_NAME=pr-${{ github.event.pull_request.number }}" >> $GITHUB_ENV + + - name: Provision Turso Database + env: + TURSO_API_TOKEN: ${{ secrets.TURSO_API_TOKEN }} + run: | + # Create a branch from the dev database with the generated safe name + turso db create ${{ env.SAFE_BRANCH_NAME }} --from-db dev || true + + - name: Get Database Credentials + env: + TURSO_API_TOKEN: ${{ secrets.TURSO_API_TOKEN }} + run: | + DB_URL=$(turso db show ${{ env.SAFE_BRANCH_NAME }} --url) + DB_TOKEN=$(turso db tokens create ${{ env.SAFE_BRANCH_NAME }}) + + # Mask database credentials + echo "::add-mask::$DB_URL" + echo "::add-mask::$DB_TOKEN" + + # Add the database credentials to the environment variables + echo "DB_URL=$DB_URL" >> $GITHUB_ENV + echo "DB_TOKEN=$DB_TOKEN" >> $GITHUB_ENV + + - name: Sync schema to database + env: + TURSO_DATABASE_URL: ${{ env.DB_URL }} + TURSO_AUTH_TOKEN: ${{ env.DB_TOKEN }} + run: | + # Push changes to the created database + npx drizzle-kit push + + - name: Make Preview Deployment + id: vercel-deployment + env: + VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }} + VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }} + VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }} + run: | + # Make a preview deployment with the created database + DEPLOYMENT_URL=$(npx vercel deploy --target preview --token ${{ secrets.VERCEL_TOKEN }} \ + --build-env TURSO_DATABASE_URL=${{ env.DB_URL }} \ + --build-env TURSO_AUTH_TOKEN=${{ env.DB_TOKEN }} \ + --env TURSO_DATABASE_URL=${{ env.DB_URL }} \ + --env TURSO_AUTH_TOKEN=${{ env.DB_TOKEN }} \ + --yes --logs) + + echo "DEPLOYMENT_URL=$DEPLOYMENT_URL" >> $GITHUB_ENV + + - name: Comment on PR + if: always() + uses: actions/github-script@v8 + env: + DEPLOYMENT_URL: ${{ env.DEPLOYMENT_URL }} + DEPLOY_STATUS: ${{ steps.vercel-deployment.outcome }} + RUN_URL: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + const {DEPLOYMENT_URL, DEPLOY_STATUS, RUN_URL} = process.env; + const status = DEPLOY_STATUS == "success" ? "✅ Ready" : "❌ Failed to deploy"; + + const body = `### 🚀 Preview Deployment: + - **Vercel Deployment:** ${DEPLOYMENT_URL ? `[View Deployment](${DEPLOYMENT_URL})` : "N/A"} + - **Status:** ${status} + - **Workflow Logs:** [View Run](${RUN_URL})`; + + const { data: comments } = await github.rest.issues.listComments({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: context.issue.number, + }); + + const existingComment = comments.find(comment => comment.body.includes('### 🚀 Preview Deployment')); + const payload = { + owner: context.repo.owner, + repo: context.repo.repo, + body: body + }; + + if (existingComment) { + await github.rest.issues.updateComment({...payload, comment_id: existingComment.id}) + } else { + await github.rest.issues.createComment({...payload, issue_number: context.issue.number}) + } diff --git a/.github/workflows/preview_deploy.yml b/.github/workflows/preview_deploy.yml deleted file mode 100644 index de80d5c..0000000 --- a/.github/workflows/preview_deploy.yml +++ /dev/null @@ -1,83 +0,0 @@ -name: Preview Deployment -on: - push: - branches-ignore: - - main - workflow_dispatch: - -concurrency: - group: ${{ github.workflow }}-${{ github.ref }} - cancel-in-progress: true - -jobs: - deploy: - runs-on: ubuntu-latest - steps: - - name: Checkout code - uses: actions/checkout@v4 - - - uses: actions/setup-node@v4 - with: - node-version: 20 - cache: "npm" - - - run: npm ci - - - name: Install Turso CLI - run: | - curl -sSfL https://get.tur.so/install.sh | bash - echo "$HOME/.turso" >> $GITHUB_PATH - - - name: Generate Safe DB Name - run: | - # Make the script executable, might not be necessary - chmod +x .github/scripts/generate_db_name.sh - - # Generate the safe branch name and store it in an environment variable - BRANCH_NAME=$(.github/scripts/generate_db_name.sh "${{ github.ref_name }}") - echo "SAFE_BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV - - - name: Provision Turso Database - if: github.ref_name != 'dev' - env: - TURSO_API_TOKEN: ${{ secrets.TURSO_API_TOKEN }} - run: | - # Create a branch from the dev database with the generated safe name - turso db create ${{ env.SAFE_BRANCH_NAME }} --from-db dev || true - - - name: Get Database Credentials - env: - TURSO_API_TOKEN: ${{ secrets.TURSO_API_TOKEN }} - run: | - DB_URL=$(turso db show ${{ env.SAFE_BRANCH_NAME }} --url) - DB_TOKEN=$(turso db tokens create ${{ env.SAFE_BRANCH_NAME }}) - - # Mask database credentials - echo "::add-mask::$DB_URL" - echo "::add-mask::$DB_TOKEN" - - # Add the database credentials to the environment variables - echo "DB_URL=$DB_URL" >> $GITHUB_ENV - echo "DB_TOKEN=$DB_TOKEN" >> $GITHUB_ENV - - - name: Sync schema to database - env: - TURSO_DATABASE_URL: ${{ env.DB_URL }} - TURSO_AUTH_TOKEN: ${{ env.DB_TOKEN }} - run: | - # Push changes to the created database - npx drizzle-kit push - - - name: Make Preview Deployment - env: - VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }} - VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }} - VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }} - run: | - # Make a preview deployment with the created database - npx vercel deploy --target preview --token ${{ secrets.VERCEL_TOKEN }} \ - --build-env TURSO_DATABASE_URL=${{ env.DB_URL }} \ - --build-env TURSO_AUTH_TOKEN=${{ env.DB_TOKEN }} \ - --env TURSO_DATABASE_URL=${{ env.DB_URL }} \ - --env TURSO_AUTH_TOKEN=${{ env.DB_TOKEN }} \ - --yes From bb60cac10211ccfede5af62f5df774e215eebe49 Mon Sep 17 00:00:00 2001 From: OverDsh Date: Tue, 23 Dec 2025 17:42:01 +0100 Subject: [PATCH 2/3] Apply suggestions from code review Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- .github/workflows/dev_deploy.yml | 2 +- .github/workflows/pr_preview.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/dev_deploy.yml b/.github/workflows/dev_deploy.yml index 714fff6..1d2da0a 100644 --- a/.github/workflows/dev_deploy.yml +++ b/.github/workflows/dev_deploy.yml @@ -23,7 +23,7 @@ jobs: - run: npm ci - - name: Push changes to database + - name: Push schema changes to database env: TURSO_DATABASE_URL: ${{ secrets.DEV_DB_URL }} TURSO_AUTH_TOKEN: ${{ secrets.DEV_DB_TOKEN }} diff --git a/.github/workflows/pr_preview.yml b/.github/workflows/pr_preview.yml index 3d22746..fea1bc3 100644 --- a/.github/workflows/pr_preview.yml +++ b/.github/workflows/pr_preview.yml @@ -97,7 +97,7 @@ jobs: github-token: ${{ secrets.GITHUB_TOKEN }} script: | const {DEPLOYMENT_URL, DEPLOY_STATUS, RUN_URL} = process.env; - const status = DEPLOY_STATUS == "success" ? "✅ Ready" : "❌ Failed to deploy"; + const status = DEPLOY_STATUS === "success" ? "✅ Ready" : "❌ Failed to deploy"; const body = `### 🚀 Preview Deployment: - **Vercel Deployment:** ${DEPLOYMENT_URL ? `[View Deployment](${DEPLOYMENT_URL})` : "N/A"} From 4283e7f2da6fcfd65f9b47a0ca188a18543cbe1f Mon Sep 17 00:00:00 2001 From: OverDsh Date: Tue, 23 Dec 2025 17:56:31 +0100 Subject: [PATCH 3/3] Fix: Fixed workflow_dispatch lacking pr number parameter --- .github/workflows/database_cleanup.yml | 17 ++++++++++++++++- .github/workflows/dev_deploy.yml | 1 + .github/workflows/pr_preview.yml | 20 ++++++++++++++++---- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/.github/workflows/database_cleanup.yml b/.github/workflows/database_cleanup.yml index 311e490..f7a9e26 100644 --- a/.github/workflows/database_cleanup.yml +++ b/.github/workflows/database_cleanup.yml @@ -4,6 +4,11 @@ on: types: - closed workflow_dispatch: + inputs: + pr_number: + description: "Pull Request Number" + required: false + type: number jobs: cleanup_database: @@ -14,9 +19,19 @@ jobs: curl -sSfL https://get.tur.so/install.sh | bash echo "$HOME/.turso" >> $GITHUB_PATH + - name: Get DB Name + run: | + PR_NUM="${{ github.event.inputs.pr_number || github.event.pull_request.number }}" + if [ -z "$PR_NUM" ]; then + echo "No PR number provided" + exit 1 + fi + DB_NAME="pr-$PR_NUM" + echo "DB_NAME=$DB_NAME" >> $GITHUB_ENV + - name: Destroy Turso Database env: TURSO_API_TOKEN: ${{ secrets.TURSO_API_TOKEN }} + DB_NAME: ${{ env.DB_NAME }} run: | - DB_NAME="pr-${{ github.event.pull_request.number }}" turso db destroy "$DB_NAME" --yes || echo "Database already gone or never existed" diff --git a/.github/workflows/dev_deploy.yml b/.github/workflows/dev_deploy.yml index 1d2da0a..9ebd46d 100644 --- a/.github/workflows/dev_deploy.yml +++ b/.github/workflows/dev_deploy.yml @@ -4,6 +4,7 @@ on: push: branches: - dev + workflow_dispatch: concurrency: group: deployment-dev diff --git a/.github/workflows/pr_preview.yml b/.github/workflows/pr_preview.yml index fea1bc3..ac6d38a 100644 --- a/.github/workflows/pr_preview.yml +++ b/.github/workflows/pr_preview.yml @@ -6,6 +6,11 @@ on: - reopened - synchronize workflow_dispatch: + inputs: + pr_number: + description: "Pull Request Number" + required: false + type: number concurrency: group: ${{ github.workflow }}-${{ github.event.pull_request.number }} @@ -35,9 +40,15 @@ jobs: curl -sSfL https://get.tur.so/install.sh | bash echo "$HOME/.turso" >> $GITHUB_PATH - - name: Set DB Name + - name: Generate DB Name run: | - echo "SAFE_BRANCH_NAME=pr-${{ github.event.pull_request.number }}" >> $GITHUB_ENV + PR_NUM="${{ github.event.inputs.pr_number || github.event.pull_request.number }}" + if [ -z "$PR_NUM" ]; then + echo "Error: No PR number found. If running manually, please provide the PR number input." + exit 1 + fi + echo "PR_NUM=$PR_NUM" >> $GITHUB_ENV + echo "SAFE_BRANCH_NAME=pr-${PR_NUM}" >> $GITHUB_ENV - name: Provision Turso Database env: @@ -93,6 +104,7 @@ jobs: DEPLOYMENT_URL: ${{ env.DEPLOYMENT_URL }} DEPLOY_STATUS: ${{ steps.vercel-deployment.outcome }} RUN_URL: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} + PR_NUM: ${{ env.PR_NUM }} with: github-token: ${{ secrets.GITHUB_TOKEN }} script: | @@ -107,7 +119,7 @@ jobs: const { data: comments } = await github.rest.issues.listComments({ owner: context.repo.owner, repo: context.repo.repo, - issue_number: context.issue.number, + issue_number: Number(PR_NUM), }); const existingComment = comments.find(comment => comment.body.includes('### 🚀 Preview Deployment')); @@ -120,5 +132,5 @@ jobs: if (existingComment) { await github.rest.issues.updateComment({...payload, comment_id: existingComment.id}) } else { - await github.rest.issues.createComment({...payload, issue_number: context.issue.number}) + await github.rest.issues.createComment({...payload, issue_number: Number(PR_NUM)}) }