Auth - Vercel Preview Alias #5
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: Auth - Vercel Preview Alias | |
on: [deployment_status] | |
jobs: | |
event_test: | |
if: github.event.deployment_status.state == 'success' && github.event.deployment.environment == 'Preview – auth' | |
runs-on: ubuntu-latest | |
steps: | |
- name: Get Pull Request from Deployment Ref | |
id: get_pr | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
run: | | |
DEPLOYMENT_SHA="${{ github.event.deployment.sha }}" | |
DEPLOYMENT_REF="${{ github.event.deployment.ref }}" | |
echo "Looking for PR associated with commit $DEPLOYMENT_SHA on branch $DEPLOYMENT_REF" | |
# Query GitHub API for PR associated with the commit and branch | |
PR_DATA=$(gh api repos/${{ github.repository }}/commits/$DEPLOYMENT_SHA/pulls --jq ".[] | select(.head.ref == \"$DEPLOYMENT_REF\") | .") | |
# Extract PR number if it exists | |
if [ -z "$PR_DATA" ]; then | |
echo "No PR associated with commit $DEPLOYMENT_SHA on branch $DEPLOYMENT_REF." | |
exit 1 | |
else | |
PR_NUMBER=$(echo "$PR_DATA" | jq '.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 – auth' | |
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_AUTH_PROJECT_ID }} | |
alias_template: 'auth.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: '🚀 Auth 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: '🛑 Auth preview deployment failed' | |
}) |