deploy_preview.yml 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. name: VercelPreviewDeployment
  2. on:
  3. pull_request_target:
  4. types:
  5. - review_requested
  6. env:
  7. VERCEL_TEAM: ${{ secrets.VERCEL_TEAM }}
  8. VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }}
  9. VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }}
  10. VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }}
  11. VERCEL_PR_DOMAIN_SUFFIX: ${{ secrets.VERCEL_PR_DOMAIN_SUFFIX }}
  12. permissions:
  13. contents: read
  14. statuses: write
  15. pull-requests: write
  16. jobs:
  17. deploy-preview:
  18. runs-on: ubuntu-latest
  19. steps:
  20. - uses: actions/checkout@v2
  21. with:
  22. ref: ${{ github.event.pull_request.head.sha }}
  23. - name: Extract branch name
  24. shell: bash
  25. run: echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> "$GITHUB_OUTPUT"
  26. id: extract_branch
  27. - name: Hash branch name
  28. uses: pplanel/hash-calculator-action@v1.3.1
  29. id: hash_branch
  30. with:
  31. input: ${{ steps.extract_branch.outputs.branch }}
  32. method: MD5
  33. - name: Set Environment Variables
  34. id: set_env
  35. if: github.event_name == 'pull_request_target'
  36. run: |
  37. echo "VERCEL_ALIAS_DOMAIN=${{ github.event.pull_request.number }}-${{ github.workflow }}.${VERCEL_PR_DOMAIN_SUFFIX}" >> $GITHUB_OUTPUT
  38. - name: Install Vercel CLI
  39. run: npm install --global vercel@latest
  40. - name: Cache dependencies
  41. uses: actions/cache@v4
  42. id: cache-npm
  43. with:
  44. path: ~/.npm
  45. key: npm-${{ hashFiles('package-lock.json') }}
  46. restore-keys: npm-
  47. - name: Pull Vercel Environment Information
  48. run: vercel pull --yes --environment=preview --token=${VERCEL_TOKEN}
  49. - name: Deploy Project Artifacts to Vercel
  50. id: vercel
  51. env:
  52. META_TAG: ${{ steps.hash_branch.outputs.digest }}-${{ github.run_number }}-${{ github.run_attempt}}
  53. run: |
  54. set -e
  55. vercel pull --yes --environment=preview --token=${VERCEL_TOKEN}
  56. vercel build --token=${VERCEL_TOKEN}
  57. vercel deploy --prebuilt --archive=tgz --token=${VERCEL_TOKEN} --meta base_hash=${{ env.META_TAG }}
  58. DEFAULT_URL=$(vercel ls --token=${VERCEL_TOKEN} --meta base_hash=${{ env.META_TAG }})
  59. ALIAS_URL=$(vercel alias set ${DEFAULT_URL} ${{ steps.set_env.outputs.VERCEL_ALIAS_DOMAIN }} --token=${VERCEL_TOKEN} --scope ${VERCEL_TEAM}| awk '{print $3}')
  60. echo "New preview URL: ${DEFAULT_URL}"
  61. echo "New alias URL: ${ALIAS_URL}"
  62. echo "VERCEL_URL=${ALIAS_URL}" >> "$GITHUB_OUTPUT"
  63. - uses: mshick/add-pr-comment@v2
  64. with:
  65. message: |
  66. Your build has completed!
  67. [Preview deployment](${{ steps.vercel.outputs.VERCEL_URL }})