From 3148d19cecea72e00fa099bfaece4a147da16262 Mon Sep 17 00:00:00 2001 From: Pratik Borole Date: Tue, 6 Aug 2024 10:59:46 +0530 Subject: [PATCH] ci: FRON-71 vercel deploy workflow with CORS support in heroku --- .github/workflows/deploy.yml | 64 ++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 .github/workflows/deploy.yml diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..c9ab641 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,64 @@ +name: Deploy to Vercel + +on: + workflow_run: + workflows: ["Lint", "CodeQL"] + types: + - completed + +jobs: + deploy: + # This condition checks if the workflow run was successful and if the target branch is 'dev'. + if: ${{ github.event.workflow_run.conclusion == 'success' && github.event_name == 'pull_request' && github.event.pull_request.base.ref == 'dev' }} + runs-on: ubuntu-latest + + steps: + - name: ⬇️ Checkout code + uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 + + - name: ⎔ Setup node + uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 + with: + node-version: 20 + + - name: Install pnpm + uses: pnpm/action-setup@a3252b78c470c02df07e9d59298aecedc3ccdd6d + with: + version: 9 + run_install: true + + - name: Install dependencies + run: pnpm install + + - name: Install Vercel CLI + run: npm install -g vercel@latest + + - name: Deploy to Vercel + run: vercel --token ${{ secrets.VERCEL_TOKEN }} + env: + VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }} + VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }} + + - name: Set CORS origin on Heroku + if: github.event.action == 'opened' || github.event.action == 'reopened' || github.event.action == 'synchronize' + run: | + PR_NUMBER=${{ github.event.number }} + DEPLOYMENT_URL=$(vercel --token ${{ secrets.VERCEL_TOKEN }} --confirm) + CURRENT_ORIGINS=$(heroku config:get ORIGINS --app ${{ secrets.HEROKU_APP_NAME }}) + NEW_ORIGINS="${CURRENT_ORIGINS},${DEPLOYMENT_URL}" + heroku config:set ORIGINS=$NEW_ORIGINS --app ${{ secrets.HEROKU_APP_NAME }} + heroku config:set PR_${PR_NUMBER}_DEPLOYMENT_URL=$DEPLOYMENT_URL --app ${{ secrets.HEROKU_APP_NAME }} + env: + HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }} + + - name: Remove CORS origin from Heroku + if: github.event.action == 'closed' + run: | + PR_NUMBER=${{ github.event.number }} + DEPLOYMENT_URL=$(heroku config:get PR_${PR_NUMBER}_DEPLOYMENT_URL --app ${{ secrets.HEROKU_APP_NAME }}) + CURRENT_ORIGINS=$(heroku config:get ORIGINS --app ${{ secrets.HEROKU_APP_NAME }}) + NEW_ORIGINS=$(echo $CURRENT_ORIGINS | sed "s|,$DEPLOYMENT_URL||g" | sed "s|$DEPLOYMENT_URL,||g" | sed "s|$DEPLOYMENT_URL||g") + heroku config:set ORIGINS=$NEW_ORIGINS --app ${{ secrets.HEROKU_APP_NAME }} + heroku config:unset PR_${PR_NUMBER}_DEPLOYMENT_URL --app ${{ secrets.HEROKU_APP_NAME }} + env: + HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}