deploy_preview.yml 2.6 KB

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