Skip to content

Process preview deployment #13

Process preview deployment

Process preview deployment #13

name: Process preview deployment
permissions:
pull-requests: write
on: [deployment_status]
concurrency:
group: deployment-${{ github.event.deployment.id }}-${{ github.event.deployment.environment }}
cancel-in-progress: false
jobs:
notify_deploy:
name: '"${{ github.event.deployment.environment }}"'
if: startsWith(github.event.deployment.environment, 'Preview')
runs-on: ubuntu-latest
steps:
- name: Get project name
id: project_name
run: |
echo "lower=$(echo '${{ github.event.deployment.environment }}' | rev | cut -d' ' -f1 | rev)" >> $GITHUB_OUTPUT
echo "caps=$(echo '${{ github.event.deployment.environment }}' | rev | cut -d' ' -f1 | rev | tr '[:lower:]' '[:upper:]')" >> $GITHUB_OUTPUT
echo "title=$(echo '${{ github.event.deployment.environment }}' | rev | cut -d' ' -f1 | rev | sed 's/^./\u&/')" >> $GITHUB_OUTPUT
- name: Get Pull Request from Deployment Ref
id: get_pr
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
DEPLOYMENT_SHA="${{ github.event.deployment.sha }}"
echo "Looking for PR associated with commit $DEPLOYMENT_SHA"
# Query GitHub API for PR associated with the commit and branch
PR_DATA=$(gh api repos/${{ github.repository }}/commits/$DEPLOYMENT_SHA/pulls --jq ".[] | .")
# 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: Define Preview URL
id: preview_url
run: echo "value=${{ steps.project_name.outputs.lower }}.pr-${{steps.get_pr.outputs.pr_number}}.props.host" >> $GITHUB_OUTPUT
- name: Install Vercel CLI
run: npm install --global vercel@latest
- name: Check if cert already exists
if: github.event.deployment_status.state == 'success'
id: check_cert
run: |
OUTPUT=$(vercel --scope ${{ vars.VERCEL_TEAM_ID }} certs ls --token ${{ secrets.VERCEL_ACCESS_TOKEN }})
if echo "$OUTPUT" | grep -q "${{steps.preview_url.outputs.value}}"; then
echo "exists=true" >> $GITHUB_OUTPUT
else
echo "exists=false" >> $GITHUB_OUTPUT
fi
- name: Generate Domain Cert in Vercel
if: github.event.deployment_status.state == 'success' && steps.check_cert.outputs.exists == 'false'
run: vercel --scope ${{ vars.VERCEL_TEAM_ID }} cert issue ${{steps.preview_url.outputs.value}} --token ${{ secrets.VERCEL_ACCESS_TOKEN }}
- name: Get Vercel's Alias Preview URL
id: alias-preview-url
if: github.event.deployment_status.state == 'success'
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[format('VERCEL_{0}_PROJECT_ID', steps.project_name.outputs.caps)] }}
alias_template: ${{steps.preview_url.outputs.value}}
- name: GitHub comment on success
if: steps.alias-preview-url.outcome == 'success' && github.event.deployment_status.state == 'success'
uses: actions/github-script@v6
with:
script: |
github.rest.issues.createComment({
issue_number: ${{steps.get_pr.outputs.pr_number}},
owner: context.repo.owner,
repo: context.repo.repo,
body: '🚀 ${{ steps.project_name.outputs.title }} preview deployment successfull at https://${{steps.alias-preview-url.outputs.preview_url_alias}}'
})
- name: GitHub comment on failute
if: github.event.deployment_status.state == 'failure'
uses: actions/github-script@v6
with:
script: |
github.rest.issues.createComment({
issue_number: ${{steps.get_pr.outputs.pr_number}},
owner: context.repo.owner,
repo: context.repo.repo,
body: '🛑 ${{ steps.project_name.outputs.title }} preview deployment failed'
})