deploy_preview.yml 2.6 KB

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