diff --git a/.github/workflows/CI.yaml b/.github/workflows/CI.yaml index f6d4b53..a7fe5d0 100644 --- a/.github/workflows/CI.yaml +++ b/.github/workflows/CI.yaml @@ -1,16 +1,27 @@ name: Vercel Preview Deployment + +on: + workflow_dispatch: + pull_request: + types: [opened, synchronize, reopened] + env: VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }} VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }} -on: - workflow_dispatch: - # Uncomment when vercel project setup - # push: - # branches-ignore: - # - main + +concurrency: + group: preview-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true + jobs: Deploy-Preview: runs-on: ubuntu-latest + if: >- + github.event_name == 'workflow_dispatch' || + github.event.pull_request.head.repo.full_name == github.repository + permissions: + contents: read + pull-requests: write steps: - uses: actions/checkout@v5 @@ -29,4 +40,18 @@ jobs: run: vercel build --token=${{ secrets.VERCEL_TOKEN }} - name: Deploy Project Artifacts to Vercel - run: vercel deploy --prebuilt --token=${{ secrets.VERCEL_TOKEN }} + id: deploy + run: | + url=$(vercel deploy --prebuilt --token=${{ secrets.VERCEL_TOKEN }} | tail -n1) + echo "url=$url" >> "$GITHUB_OUTPUT" + + - name: Comment the preview URL on the pull request + if: github.event_name == 'pull_request' + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + URL: ${{ steps.deploy.outputs.url }} + run: | + gh pr comment ${{ github.event.pull_request.number }} \ + --repo ${{ github.repository }} \ + --edit-last --create-if-none \ + --body "Preview deployment: $URL"