Skip to content

Features: Continous deployment of Pull Requests and cleanup of closed PR deployed links #1791

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,[email protected]"
Expand Down
142 changes: 142 additions & 0 deletions .github/workflows/cd_PR.yml
Original file line number Diff line number Diff line change
@@ -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"
43 changes: 43 additions & 0 deletions .github/workflows/pr_deploy_cleanup.yml
Original file line number Diff line number Diff line change
@@ -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 }}
3 changes: 2 additions & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
59 changes: 59 additions & 0 deletions scripts/set-vercel-env.sh
Original file line number Diff line number Diff line change
@@ -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

Loading