Skip to content

Commit 2ccf589

Browse files
committed
add PR preview link
1 parent 7697ad6 commit 2ccf589

File tree

5 files changed

+206
-31
lines changed

5 files changed

+206
-31
lines changed

.env.example

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
NEXT_PUBLIC_BASE_URL_LOCAL=http://127.0.0.1:3000
1+
NEXT_PUBLIC_BASE_URL_LOCAL="http://127.0.0.1:3000"
22
ADMIN_SECRET="ADMIN_SECRET"
33
JWT_SECRET="JWT_SECRET"
44
# DONT CHANGE FOR RUNNING WITH DOCKER
@@ -13,7 +13,7 @@ DISCORD_ACCESS_SECRET = "123"
1313
DISCORD_REDIRECT_URL = "https://app.100xdevs.com/discord/redirect"
1414
BOT_TOKEN = "123"
1515
GUILD_ID = "123"
16-
LOCAL_CMS_PROVIDER = true
16+
LOCAL_CMS_PROVIDER = "true"
1717
CACHE_EXPIRE_S = 10
1818
SUBTITLE_SECRET=SubSecret
1919
ADMINS = "Random,[email protected]"

.github/workflows/cd_PR.yml

+100-28
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
name: Pull Request build+deploy
1+
name: Pull Request Preview Deployment
22

33
on:
44
pull_request:
55
types: ['opened', 'edited', 'synchronize']
66
branches:
7-
- '**'
7+
- '**'
8+
89

910
jobs:
10-
deploy:
11-
environment: branch-deploy
11+
12+
Deploy-PR-Preview:
13+
# needs: [Continuous-Integration]
14+
# environment: branch-deploy
1215
name: Build and deploy
1316
runs-on: ubuntu-latest
17+
1418
steps:
1519
- name: Checkout code
1620
uses: actions/checkout@v4
@@ -20,17 +24,13 @@ jobs:
2024
with:
2125
version: 10
2226

23-
24-
- name: Set up Docker
25-
uses: docker/setup-docker-action@v4
26-
2727
- name: Setup Node
2828
uses: actions/setup-node@v4
2929
with:
3030
node-version: 20
3131
cache: 'pnpm'
3232

33-
- name: Setup deployment
33+
- name: Initialize deployment status
3434
uses: bobheadxi/deployments@v1
3535
id: deployment
3636
with:
@@ -39,24 +39,46 @@ jobs:
3939
env: branch-deploy-${{ github.head_ref }}
4040
ref: ${{ github.head_ref }}
4141

42-
- name: Install dependencies
43-
run: ./setup.sh
44-
45-
- name: Deploy to Netlify
46-
uses: nwtgck/[email protected]
47-
with:
48-
publish-dir: './next'
49-
production-branch: main
50-
github-token: ${{ secrets.GITHUB_TOKEN }}
51-
deploy-message: "Deploy from GitHub Actions"
52-
enable-pull-request-comment: false
53-
enable-commit-comment: true
54-
overwrites-pull-request-comment: true
55-
alias: deploy-preview-${{ github.event.number }}-100xdevs
42+
- name: Install dependencies and build
43+
run: |
44+
pnpm i -g vercel
45+
pnpm install
46+
5647
env:
57-
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
58-
NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}
59-
timeout-minutes: 1
48+
DATABASE_URL: ${{ secrets.DB_URL }}
49+
50+
- name: Deploy to vercel
51+
env:
52+
DB_URL: ${{ secrets.DB_URL }}
53+
run: |
54+
chmod +x ./scripts/set-vercel-env.sh
55+
cp .env.example .env
56+
sed -i '/^DATABASE_URL=/d' .env
57+
echo "DATABASE_URL=${{ secrets.DB_URL }}" >> .env
58+
vercel link --yes --project pr-${{ github.event.pull_request.number }}-cms --token ${{ secrets.VERCEL_TOKEN }}
59+
60+
sed -i '/^NEXTAUTH_URL=/d' .env
61+
echo "NEXTAUTH_URL=https://pr-${{ github.event.pull_request.number }}-cms.vercel.app" >> .env
62+
63+
if ! vercel env ls --token ${{ secrets.VERCEL_TOKEN }} | grep "DATABASE_URL"; then
64+
echo "Setting up Vercel env..."
65+
./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..."
66+
fi
67+
68+
69+
vercel build --prod --token ${{ secrets.VERCEL_TOKEN }} --yes
70+
vercel deploy --prebuilt --prod --token=${{ secrets.VERCEL_TOKEN }} > deployment-url.txt
71+
vercel alias `cat deployment-url.txt` pr-${{ github.event.pull_request.number }}-cms --token ${{ secrets.VERCEL_TOKEN }}
72+
73+
echo "DEPLOYMENT_URL=$(cat deployment-url.txt)" >> $GITHUB_ENV
74+
75+
- name: Upload Deployment Artifacts
76+
uses: actions/upload-artifact@v4
77+
with:
78+
name: deployment-url
79+
path: |
80+
deployment-url.txt
81+
6082
6183
- name: Update deployment status
6284
uses: bobheadxi/deployments@v1
@@ -66,5 +88,55 @@ jobs:
6688
token: ${{ secrets.GITHUB_TOKEN }}
6789
status: ${{ job.status }}
6890
deployment_id: ${{ steps.deployment.outputs.deployment_id }}
69-
logs: ${{ steps.deploy-netlify.outputs.netlify_logs_url }}
70-
env_url: ${{ steps.deploy-netlify.outputs.netlify_preview_url }}
91+
env_url: https://pr-${{ github.event.pull_request.number }}-cms.vercel.app
92+
env: ${{ steps.deployment.outputs.env }}
93+
94+
95+
Prisma-Migrations:
96+
needs: [Deploy-PR-Preview]
97+
runs-on: ubuntu-latest
98+
99+
steps:
100+
- name: Checkout code
101+
uses: actions/checkout@v4
102+
103+
- name: Check for migration changes
104+
id: check-migrations
105+
run: |
106+
if git diff --quiet HEAD^ HEAD -- prisma/migrations; then
107+
echo "migrations_changed=false" >> $GITHUB_OUTPUT
108+
else
109+
echo "migrations_changed=true" >> $GITHUB_OUTPUT
110+
fi
111+
112+
- name: Install pnpm
113+
if: steps.check-migrations.outputs.migrations_changed == 'true'
114+
uses: pnpm/action-setup@v4
115+
with:
116+
version: 10
117+
118+
- name: Install dependencies and build
119+
if: steps.check-migrations.outputs.migrations_changed == 'true'
120+
run: |
121+
pnpm i -g vercel
122+
pnpm install
123+
124+
- name: Setup Node
125+
if: steps.check-migrations.outputs.migrations_changed == 'true'
126+
uses: actions/setup-node@v4
127+
with:
128+
node-version: 20
129+
cache: 'pnpm'
130+
131+
- name: Apply all pending migrations
132+
if: steps.check-migrations.outputs.migrations_changed == 'true'
133+
env:
134+
DATABASE_URL: ${{ secrets.DB_URL }}
135+
run: |
136+
echo "Applying migrations"
137+
pnpm prisma generate
138+
echo "Deploying migrations"
139+
pnpm prisma migrate deploy > migrate.log
140+
echo "Resetting migrations"
141+
pnpm prisma migrate reset --force > reset.log
142+
echo "Migrations applied"
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: Cleanup PR Deployment
2+
3+
on:
4+
pull_request:
5+
types:
6+
- closed
7+
8+
jobs:
9+
cleanup:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout code
13+
uses: actions/checkout@v4
14+
15+
- name: Install pnpm
16+
uses: pnpm/action-setup@v4
17+
with:
18+
version: 10
19+
20+
- name: Install dependencies and build
21+
run: |
22+
pnpm i -g vercel
23+
24+
- name: Download Deployment URL
25+
uses: actions/download-artifact@v4
26+
27+
28+
- name: Read Deployment URL
29+
run: |
30+
31+
ls -a | grep deployment
32+
if [[ -f "deployment-url.txt" ]]; then
33+
echo "DEPLOYMENT_URL=$(cat deployment-url.txt)" >> $GITHUB_ENV
34+
else
35+
echo "No deployment URL found, skipping cleanup."
36+
exit 0
37+
fi
38+
39+
- name: Remove Deployment
40+
if: env.DEPLOYMENT_URL != ''
41+
run: |
42+
echo "Removing deployment: $DEPLOYMENT_URL"
43+
echo y| vercel project rm pr-${{ github.event.pull_request.number }}-cms --token ${{ secrets.VERCEL_TOKEN }}

prisma/schema.prisma

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
generator client {
22
provider = "prisma-client-js"
3-
binaryTargets = ["native", "linux-musl-arm64-openssl-3.0.x"]
3+
binaryTargets = ["native", "linux-musl-arm64-openssl-3.0.x", "rhel-openssl-3.0.x", "debian-openssl-3.0.x"]
4+
45
}
56

67
datasource db {

scripts/set-vercel-env.sh

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/bin/bash
2+
3+
# Check if .env file exists
4+
if [ ! -f .env ]; then
5+
echo "❌ Error: .env file not found!"
6+
exit 1
7+
fi
8+
9+
# Set the target environment (default to 'development' if not provided)
10+
ENVIRONMENT=${1:-development}
11+
VERCEL_TOKEN=${2}
12+
NEXTAUTH_URL=${3}
13+
14+
# Read .env file line by line
15+
while IFS='=' read -r key value || [ -n "$key" ]; do
16+
# Ignore empty lines and comments
17+
if [[ -z "$key" || "$key" =~ ^# ]]; then
18+
continue
19+
fi
20+
21+
# Trim whitespace
22+
key=$(echo "$key" | xargs)
23+
value=$(echo "$value" | xargs)
24+
25+
# Ensure key-value pair is valid
26+
if [[ -z "$key" || -z "$value" ]]; then
27+
echo "⚠️ Warning: Skipping invalid line in .env"
28+
continue
29+
fi
30+
31+
# Override DBURL with the system environment variable if set
32+
if [[ "$key" == "DATABASE_URL" && -n "$DB_URL" ]]; then
33+
value="$DB_URL"
34+
echo "🔐 Using external DATABASE_URL from system environment"
35+
fi
36+
37+
# Override NEXTAUTHURL and base url with the system environment variable if set
38+
if [[ "$key" == "NEXTAUTH_URL" && -n "$NEXTAUTH_URL" ]]; then
39+
value="$NEXTAUTH_URL"
40+
echo "🔐 Added Nextauth url"
41+
fi
42+
43+
if [[ "$key" == "NEXT_PUBLIC_BASE_URL_LOCAL" && -n "$NEXTAUTH_URL" ]]; then
44+
value="$NEXTAUTH_URL"
45+
echo "🔐 Added base url"
46+
fi
47+
48+
49+
# Add the environment variable to Vercel
50+
echo -n "$value" | vercel env add "$key" "$ENVIRONMENT" --token="$VERCEL_TOKEN"
51+
52+
if [ $? -eq 0 ]; then
53+
echo "✅ Added $key to Vercel ($ENVIRONMENT)"
54+
else
55+
echo "❌ Error: Failed to add $key to Vercel"
56+
exit 1
57+
fi
58+
done < .env
59+

0 commit comments

Comments
 (0)