Features: Continous deployment of Pull Requests and cleanup of closed PR deployed links #2
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | | |
echo "Checking for changes in prisma/migrations folder..." | |
git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }} | |
if git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }} | grep -q '^prisma/migrations/'; then | |
echo "Migrations have changed" | |
echo "migrations_changed=true" >> $GITHUB_OUTPUT | |
else | |
echo "No migration changes detected" | |
echo "migrations_changed=false" >> $GITHUB_OUTPUT | |
- 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" |