Web - Vercel Preview Alias #1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Web - Vercel Preview Alias | |
on: [deployment_status] | |
jobs: | |
event_test: | |
if: github.event.deployment_status.state == 'success' && github.event.deployment.environment == 'Preview – web' | |
runs-on: ubuntu-latest | |
steps: | |
- name: Get Pull Request from Deployment Ref | |
id: get_pr | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
run: | | |
DEPLOYMENT_REF="${{ github.event.deployment.ref }}" | |
echo "Deployment Ref: $DEPLOYMENT_REF" | |
# Query the PR associated with the branch | |
PR_NUMBER=$(gh api repos/${{ github.repository }}/pulls --jq '.[] | select(.head.ref == env.DEPLOYMENT_REF) | .number') | |
if [ -z "$PR_NUMBER" ]; then | |
echo "No associated PR found." | |
exit 1 | |
else | |
echo "Pull Request Number: $PR_NUMBER" | |
echo "pr_number=$PR_NUMBER" >> $GITHUB_OUTPUT | |
fi | |
- name: Get Vercel's Alias Preview URL | |
if: github.event.deployment_status.state == 'success' && github.event.deployment.environment == 'Preview – web' | |
id: alias-preview-url | |
uses: justincase-jp/vercel-preview-url-alias@0.2.1 | |
with: | |
vercel_access_token: ${{ secrets.VERCEL_ACCESS_TOKEN }} | |
vercel_team_id: ${{ vars.VERCEL_TEAM_ID }} | |
vercel_project_id: ${{ vars.VERCEL_WEB_PROJECT_ID }} | |
alias_template: 'web.pr-${{steps.get_pr.outputs.pr_number}}.props.host' | |
- name: GitHub comment on success | |
uses: actions/github-script@v6 | |
if: steps.alias-preview-url.outputs.status == 'SUCCESS' && github.event.deployment_status.state == 'success' | |
with: | |
script: | | |
github.rest.issues.createComment({ | |
issue_number: ${{steps.get_pr.outputs.pr_number}}, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: '🚀 Web preview deployment successfull at ${{steps.alias-preview-url.outputs.preview_url_alias}}' | |
}) | |
- name: GitHub comment on failute | |
uses: actions/github-script@v6 | |
if: github.event.deployment_status.state == 'failure' | |
with: | |
script: | | |
github.rest.issues.createComment({ | |
issue_number: ${{steps.get_pr.outputs.pr_number}}, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: '🛑 Web preview deployment failed' | |
}) |