deploy_preview.yml 2.7 KB

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