deploy_preview.yml 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. name: VercelPreviewDeployment
  2. on:
  3. workflow_dispatch:
  4. inputs:
  5. branchName:
  6. description: 'Branch to deploy'
  7. required: true
  8. env:
  9. VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }}
  10. VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }}
  11. VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }}
  12. VERCEL_DOMAIN_SUFFIX: ".pr.nextchat.dev"
  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.inputs.branchName }}
  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'
  37. run: |
  38. echo "VERCEL_ALIAS_DOMAIN=${{ github.event.pull_request.number }}-${{ github.workflow }}.${VERCEL_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. env
  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. vercel ls --token=${VERCEL_TOKEN} --meta base_hash=${{ env.META_TAG }} &> vercel-output
  60. DEFAULT_URL=$(cat vercel-output | grep http | awk '{print $2}')
  61. ALIAS_URL=$(vercel alias set ${DEFAULT_URL} ${{ steps.set_env.outputs.VERCEL_ALIAS_DOMAIN }} --token=${VERCEL_TOKEN} | awk '{print $3}')
  62. echo "New preview URL: ${DEFAULT_URL}"
  63. echo "New alias URL: ${ALIAS_URL}"
  64. echo "META_TAG=${META_TAG}"
  65. echo "VERCEL_URL=${ALIAS_URL}" >> "$GITHUB_OUTPUT"
  66. - uses: mshick/add-pr-comment@v2
  67. with:
  68. message: |
  69. Your build has completed!
  70. [Preview deployment](${{ steps.vercel.outputs.VERCEL_URL }})