deploy_preview.yml 2.7 KB

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