From 7697ad6857d37dca2ad86f44b3c163d87447391a Mon Sep 17 00:00:00 2001 From: Manash Pratim Bhuyan Date: Thu, 20 Mar 2025 01:41:45 +0530 Subject: [PATCH 1/2] add PR deploy preview --- .github/workflows/cd_PR.yml | 70 +++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 .github/workflows/cd_PR.yml diff --git a/.github/workflows/cd_PR.yml b/.github/workflows/cd_PR.yml new file mode 100644 index 000000000..38d61f363 --- /dev/null +++ b/.github/workflows/cd_PR.yml @@ -0,0 +1,70 @@ +name: Pull Request build+deploy + +on: + pull_request: + types: ['opened', 'edited', 'synchronize'] + branches: + - '**' + +jobs: + deploy: + environment: branch-deploy + name: Build and deploy + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Install pnpm + uses: pnpm/action-setup@v4 + with: + version: 10 + + + - name: Set up Docker + uses: docker/setup-docker-action@v4 + + - name: Setup Node + uses: actions/setup-node@v4 + with: + node-version: 20 + cache: 'pnpm' + + - name: Setup deployment + uses: bobheadxi/deployments@v1 + id: deployment + with: + step: start + token: ${{ secrets.GITHUB_TOKEN }} + env: branch-deploy-${{ github.head_ref }} + ref: ${{ github.head_ref }} + + - name: Install dependencies + run: ./setup.sh + + - name: Deploy to Netlify + uses: nwtgck/actions-netlify@v3.0 + with: + publish-dir: './next' + production-branch: main + github-token: ${{ secrets.GITHUB_TOKEN }} + deploy-message: "Deploy from GitHub Actions" + enable-pull-request-comment: false + enable-commit-comment: true + overwrites-pull-request-comment: true + alias: deploy-preview-${{ github.event.number }}-100xdevs + env: + NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }} + NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }} + timeout-minutes: 1 + + - name: Update deployment status + uses: bobheadxi/deployments@v1 + if: always() + with: + step: finish + token: ${{ secrets.GITHUB_TOKEN }} + status: ${{ job.status }} + deployment_id: ${{ steps.deployment.outputs.deployment_id }} + logs: ${{ steps.deploy-netlify.outputs.netlify_logs_url }} + env_url: ${{ steps.deploy-netlify.outputs.netlify_preview_url }} From 2ccf5894c5101850c38a3832a30a3622d574a32d Mon Sep 17 00:00:00 2001 From: Manash Pratim Bhuyan Date: Thu, 20 Mar 2025 03:39:56 +0530 Subject: [PATCH 2/2] add PR preview link --- .env.example | 4 +- .github/workflows/cd_PR.yml | 128 ++++++++++++++++++------ .github/workflows/pr_deploy_cleanup.yml | 43 ++++++++ prisma/schema.prisma | 3 +- scripts/set-vercel-env.sh | 59 +++++++++++ 5 files changed, 206 insertions(+), 31 deletions(-) create mode 100644 .github/workflows/pr_deploy_cleanup.yml create mode 100755 scripts/set-vercel-env.sh diff --git a/.env.example b/.env.example index 1a8fe7f37..24481c7b0 100644 --- a/.env.example +++ b/.env.example @@ -1,4 +1,4 @@ -NEXT_PUBLIC_BASE_URL_LOCAL=http://127.0.0.1:3000 +NEXT_PUBLIC_BASE_URL_LOCAL="http://127.0.0.1:3000" ADMIN_SECRET="ADMIN_SECRET" JWT_SECRET="JWT_SECRET" # DONT CHANGE FOR RUNNING WITH DOCKER @@ -13,7 +13,7 @@ DISCORD_ACCESS_SECRET = "123" DISCORD_REDIRECT_URL = "https://app.100xdevs.com/discord/redirect" BOT_TOKEN = "123" GUILD_ID = "123" -LOCAL_CMS_PROVIDER = true +LOCAL_CMS_PROVIDER = "true" CACHE_EXPIRE_S = 10 SUBTITLE_SECRET=SubSecret ADMINS = "Random,example@gmail.com" diff --git a/.github/workflows/cd_PR.yml b/.github/workflows/cd_PR.yml index 38d61f363..d23fb49e6 100644 --- a/.github/workflows/cd_PR.yml +++ b/.github/workflows/cd_PR.yml @@ -1,16 +1,20 @@ -name: Pull Request build+deploy +name: Pull Request Preview Deployment on: pull_request: types: ['opened', 'edited', 'synchronize'] branches: - - '**' + - '**' + jobs: - deploy: - environment: branch-deploy + + Deploy-PR-Preview: + # needs: [Continuous-Integration] + # environment: branch-deploy name: Build and deploy runs-on: ubuntu-latest + steps: - name: Checkout code uses: actions/checkout@v4 @@ -20,17 +24,13 @@ jobs: with: version: 10 - - - name: Set up Docker - uses: docker/setup-docker-action@v4 - - name: Setup Node uses: actions/setup-node@v4 with: node-version: 20 cache: 'pnpm' - - name: Setup deployment + - name: Initialize deployment status uses: bobheadxi/deployments@v1 id: deployment with: @@ -39,24 +39,46 @@ jobs: env: branch-deploy-${{ github.head_ref }} ref: ${{ github.head_ref }} - - name: Install dependencies - run: ./setup.sh - - - name: Deploy to Netlify - uses: nwtgck/actions-netlify@v3.0 - with: - publish-dir: './next' - production-branch: main - github-token: ${{ secrets.GITHUB_TOKEN }} - deploy-message: "Deploy from GitHub Actions" - enable-pull-request-comment: false - enable-commit-comment: true - overwrites-pull-request-comment: true - alias: deploy-preview-${{ github.event.number }}-100xdevs + - name: Install dependencies and build + run: | + pnpm i -g vercel + pnpm install + env: - NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }} - NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }} - timeout-minutes: 1 + DATABASE_URL: ${{ secrets.DB_URL }} + + - name: Deploy to vercel + env: + DB_URL: ${{ secrets.DB_URL }} + run: | + chmod +x ./scripts/set-vercel-env.sh + cp .env.example .env + sed -i '/^DATABASE_URL=/d' .env + echo "DATABASE_URL=${{ secrets.DB_URL }}" >> .env + vercel link --yes --project pr-${{ github.event.pull_request.number }}-cms --token ${{ secrets.VERCEL_TOKEN }} + + sed -i '/^NEXTAUTH_URL=/d' .env + echo "NEXTAUTH_URL=https://pr-${{ github.event.pull_request.number }}-cms.vercel.app" >> .env + + if ! vercel env ls --token ${{ secrets.VERCEL_TOKEN }} | grep "DATABASE_URL"; then + echo "Setting up Vercel env..." + ./scripts/set-vercel-env.sh production ${{ secrets.VERCEL_TOKEN }} https://pr-${{ github.event.pull_request.number }}-cms.vercel.app || echo "Warning: Failed to set up Vercel env, but continuing..." + fi + + + vercel build --prod --token ${{ secrets.VERCEL_TOKEN }} --yes + vercel deploy --prebuilt --prod --token=${{ secrets.VERCEL_TOKEN }} > deployment-url.txt + vercel alias `cat deployment-url.txt` pr-${{ github.event.pull_request.number }}-cms --token ${{ secrets.VERCEL_TOKEN }} + + echo "DEPLOYMENT_URL=$(cat deployment-url.txt)" >> $GITHUB_ENV + + - name: Upload Deployment Artifacts + uses: actions/upload-artifact@v4 + with: + name: deployment-url + path: | + deployment-url.txt + - name: Update deployment status uses: bobheadxi/deployments@v1 @@ -66,5 +88,55 @@ jobs: token: ${{ secrets.GITHUB_TOKEN }} status: ${{ job.status }} deployment_id: ${{ steps.deployment.outputs.deployment_id }} - logs: ${{ steps.deploy-netlify.outputs.netlify_logs_url }} - env_url: ${{ steps.deploy-netlify.outputs.netlify_preview_url }} + env_url: https://pr-${{ github.event.pull_request.number }}-cms.vercel.app + env: ${{ steps.deployment.outputs.env }} + + + Prisma-Migrations: + needs: [Deploy-PR-Preview] + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Check for migration changes + id: check-migrations + run: | + if git diff --quiet HEAD^ HEAD -- prisma/migrations; then + echo "migrations_changed=false" >> $GITHUB_OUTPUT + else + echo "migrations_changed=true" >> $GITHUB_OUTPUT + fi + + - name: Install pnpm + if: steps.check-migrations.outputs.migrations_changed == 'true' + uses: pnpm/action-setup@v4 + with: + version: 10 + + - name: Install dependencies and build + if: steps.check-migrations.outputs.migrations_changed == 'true' + run: | + pnpm i -g vercel + pnpm install + + - name: Setup Node + if: steps.check-migrations.outputs.migrations_changed == 'true' + uses: actions/setup-node@v4 + with: + node-version: 20 + cache: 'pnpm' + + - name: Apply all pending migrations + if: steps.check-migrations.outputs.migrations_changed == 'true' + env: + DATABASE_URL: ${{ secrets.DB_URL }} + run: | + echo "Applying migrations" + pnpm prisma generate + echo "Deploying migrations" + pnpm prisma migrate deploy > migrate.log + echo "Resetting migrations" + pnpm prisma migrate reset --force > reset.log + echo "Migrations applied" \ No newline at end of file diff --git a/.github/workflows/pr_deploy_cleanup.yml b/.github/workflows/pr_deploy_cleanup.yml new file mode 100644 index 000000000..422346ce1 --- /dev/null +++ b/.github/workflows/pr_deploy_cleanup.yml @@ -0,0 +1,43 @@ +name: Cleanup PR Deployment + +on: + pull_request: + types: + - closed + +jobs: + cleanup: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Install pnpm + uses: pnpm/action-setup@v4 + with: + version: 10 + + - name: Install dependencies and build + run: | + pnpm i -g vercel + + - name: Download Deployment URL + uses: actions/download-artifact@v4 + + + - name: Read Deployment URL + run: | + + ls -a | grep deployment + if [[ -f "deployment-url.txt" ]]; then + echo "DEPLOYMENT_URL=$(cat deployment-url.txt)" >> $GITHUB_ENV + else + echo "No deployment URL found, skipping cleanup." + exit 0 + fi + + - name: Remove Deployment + if: env.DEPLOYMENT_URL != '' + run: | + echo "Removing deployment: $DEPLOYMENT_URL" + echo y| vercel project rm pr-${{ github.event.pull_request.number }}-cms --token ${{ secrets.VERCEL_TOKEN }} diff --git a/prisma/schema.prisma b/prisma/schema.prisma index ca8e4431c..50445e2b6 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -1,6 +1,7 @@ generator client { provider = "prisma-client-js" - binaryTargets = ["native", "linux-musl-arm64-openssl-3.0.x"] + binaryTargets = ["native", "linux-musl-arm64-openssl-3.0.x", "rhel-openssl-3.0.x", "debian-openssl-3.0.x"] + } datasource db { diff --git a/scripts/set-vercel-env.sh b/scripts/set-vercel-env.sh new file mode 100755 index 000000000..54de727dc --- /dev/null +++ b/scripts/set-vercel-env.sh @@ -0,0 +1,59 @@ +#!/bin/bash + +# Check if .env file exists +if [ ! -f .env ]; then + echo "❌ Error: .env file not found!" + exit 1 +fi + +# Set the target environment (default to 'development' if not provided) +ENVIRONMENT=${1:-development} +VERCEL_TOKEN=${2} +NEXTAUTH_URL=${3} + +# Read .env file line by line +while IFS='=' read -r key value || [ -n "$key" ]; do + # Ignore empty lines and comments + if [[ -z "$key" || "$key" =~ ^# ]]; then + continue + fi + + # Trim whitespace + key=$(echo "$key" | xargs) + value=$(echo "$value" | xargs) + + # Ensure key-value pair is valid + if [[ -z "$key" || -z "$value" ]]; then + echo "⚠️ Warning: Skipping invalid line in .env" + continue + fi + + # Override DBURL with the system environment variable if set + if [[ "$key" == "DATABASE_URL" && -n "$DB_URL" ]]; then + value="$DB_URL" + echo "🔐 Using external DATABASE_URL from system environment" + fi + + # Override NEXTAUTHURL and base url with the system environment variable if set + if [[ "$key" == "NEXTAUTH_URL" && -n "$NEXTAUTH_URL" ]]; then + value="$NEXTAUTH_URL" + echo "🔐 Added Nextauth url" + fi + + if [[ "$key" == "NEXT_PUBLIC_BASE_URL_LOCAL" && -n "$NEXTAUTH_URL" ]]; then + value="$NEXTAUTH_URL" + echo "🔐 Added base url" + fi + + + # Add the environment variable to Vercel + echo -n "$value" | vercel env add "$key" "$ENVIRONMENT" --token="$VERCEL_TOKEN" + + if [ $? -eq 0 ]; then + echo "✅ Added $key to Vercel ($ENVIRONMENT)" + else + echo "❌ Error: Failed to add $key to Vercel" + exit 1 + fi +done < .env +