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 new file mode 100644 index 000000000..d23fb49e6 --- /dev/null +++ b/.github/workflows/cd_PR.yml @@ -0,0 +1,142 @@ +name: Pull Request Preview Deployment + +on: + pull_request: + types: ['opened', 'edited', 'synchronize'] + branches: + - '**' + + +jobs: + + 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 + + - name: Install pnpm + uses: pnpm/action-setup@v4 + with: + version: 10 + + - name: Setup Node + uses: actions/setup-node@v4 + with: + node-version: 20 + cache: 'pnpm' + + - name: Initialize deployment status + 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 and build + run: | + pnpm i -g vercel + pnpm install + + env: + 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 + if: always() + with: + step: finish + token: ${{ secrets.GITHUB_TOKEN }} + status: ${{ job.status }} + deployment_id: ${{ steps.deployment.outputs.deployment_id }} + 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 +