i have multiple apps in my turborepo and each app has a separate project created in vercel.
Each of the apps uses the same Database, where we are using Neon as the database provider and Prisma to manage migrations.
Here's our current CI/CD pipeline
1name: Deploy Preview23on: [pull_request]45env:6 NEON_API_KEY: ${{ secrets.NEON_API_KEY }}7 NEON_MAIN_BRANCH_NAME: ${{ secrets.NEON_MAIN_BRANCH_NAME }}8 NEON_PROJECT_ID: ${{ secrets.NEON_PROJECT_ID }}9 PG_DATABASE: ${{ secrets.PG_DATABASE }}10 PG_USERNAME: ${{ secrets.PG_USERNAME }}11 VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }}12 VERCEL_TEAM_ID: ${{ secrets.VERCEL_TEAM_ID }}1314jobs:15 deploy-preview:16 permissions: write-all17 runs-on: ubuntu-latest18 strategy:19 matrix:20 app: [web,admin]21 outputs:22 web_preview_url: ${{ steps.deploy.outputs.web_preview_url }}23 admin_preview_url: ${{ steps.deploy.outputs.admin_preview_url }}24 neon_branch_id: ${{ steps.create-branch.outputs.branch_id }}2526 steps:27 - name: Get branch name28 id: branch-name29 uses: tj-actions/branch-names@v83031 - uses: actions/checkout@v432 with:33 ref: ${{ steps.branch-name.outputs.current_branch }}3435 - name: Create Neon Branch36 id: create-branch37 uses: neondatabase/create-branch-action@v538 with:39 project_id: ${{ env.NEON_PROJECT_ID }}40 # parent: dev # optional (defaults to your primary branch)41 branch_name: preview/pr-${{ github.event.number }}-${{ steps.branch-name.outputs.current_branch }}42 username: ${{ env.PG_USERNAME }}43 api_key: ${{ env.NEON_API_KEY }}4445 - name: Update environment variables46 run: |47 touch .env48 echo DATABASE_URL=${{ steps.create-branch.outputs.db_url_with_pooler }} >> .env4950 - name: Install Vercel51 run: npm install --global vercel@34.2.55253 - name: Link Vercel to GitHub Repository54 run: vercel link --repo --yes --scope=${{ env.VERCEL_TEAM_ID}} --token=${{ env.VERCEL_TOKEN }}5556 - name: Pull Vercel Environment Information57 run: |58 if [ "${{ matrix.app }}" == "web" ]; then59 vercel pull --yes --scope=${{ env.VERCEL_TEAM_ID}} --environment=preview --token=${{ env.VERCEL_TOKEN }} --cwd apps/web60 elif [ "${{ matrix.app }}" == "admin" ]; then61 vercel pull --yes --scope=${{ env.VERCEL_TEAM_ID}} --environment=preview --token=${{ env.VERCEL_TOKEN }} --cwd apps/admin62 fi6364 - name: Build Project Artifacts65 run: |66 if [ "${{ matrix.app }}" == "web" ]; then67 vercel build --scope=${{ env.VERCEL_TEAM_ID}} --token=${{ env.VERCEL_TOKEN }} --cwd apps/web68 elif [ "${{ matrix.app }}" == "admin" ]; then69 vercel build --scope=${{ env.VERCEL_TEAM_ID}} --token=${{ env.VERCEL_TOKEN }} --cwd apps/admin70 fi7172 - name: Deploy Preview to Vercel73 id: deploy74 run: |75 if [ "${{ matrix.app }}" == "web" ]; then76 echo web_preview_url=$(vercel deploy --prebuilt --env DATABASE_URL=${{ steps.create-branch.outputs.db_url_with_pooler }} --scope=${{ env.VERCEL_TEAM_ID}} --token=${{ env.VERCEL_TOKEN }} --cwd apps/web) >> $GITHUB_OUTPUT77 elif [ "${{ matrix.app }}" == "admin" ]; then78 echo admin_preview_url=$(vercel deploy --prebuilt --env DATABASE_URL=${{ steps.create-branch.outputs.db_url_with_pooler }} --scope=${{ env.VERCEL_TEAM_ID}} --token=${{ env.VERCEL_TOKEN }} --cwd apps/admin) >> $GITHUB_OUTPUT79 fi8081 comment-preview:82 runs-on: ubuntu-latest83 needs: deploy-preview84 steps:85 - name: Comment on Pull Request86 uses: thollander/actions-comment-pull-request@v287 with:88 # GITHUB_TOKEN: ${{ env.GH_TOKEN }}89 message: |90 VERCEL Web Preview URL :rocket: : ${{ needs.deploy-preview.outputs.web_preview_url }}91 VERCEL Admin Preview URL :rocket: : ${{ needs.deploy-preview.outputs.admin_preview_url }}92 Neon branch :elephant: : https://console.neon.tech/app/projects/${{ env.NEON_PROJECT_ID }}/branches/${{ needs.deploy-preview.outputs.neon_branch_id }}93
2 things to note from the snippet above
- We are updating the DATABASE_URL env value generated from neon database
- After deployment is completed we are commenting on the PR with deployment url
The problem with the current approach
- We are building common packages twice (defeats the purpose of using turborepo)
- Prisma migrations must be ran on both the apps in parallel (Although, I would be somehow be able to avoid it in and run it only once ). Sometimes the builds fails because of this
I cannot use turbo build command since I wouldn't be able to dynamically update env variable, access deployment url etc which the vercel cli provides with.
It would be good to have more options in the vercel cli to support this, similar to the --repo flag that was introduced some time back.
https://vercel.com/docs/cli/link#repo-alpha
References:
https://vercel.com/guides/how-can-i-use-github-actions-with-vercel