Example: create deployments with a custom preview URL #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
# Demo workflow for tagging GitHub commits with existing preview URLs | |
# | |
# For this to work preview URLs must be: | |
# | |
# 1. Per commit, so that even if new commits are pushed to the same branch the old build override URL still serves the original commit | |
# 2. Live for at least one week, ideally 2 weeks | |
# | |
# Rolling your own preview URLs is complex. We strongly recommend using [Vercel](https://app.meticulous.ai/docs/cloud-replay) or | |
# the [Meticulous GitHub action to run Meticulous tests](https://app.meticulous.ai/docs/github-actions-v2). | |
name: Create GitHub deployment - react-bmi-calculator | |
on: | |
push: | |
branches: | |
- main | |
- releases/* | |
pull_request: {} | |
workflow_dispatch: {} | |
permissions: | |
actions: write | |
contents: read | |
statuses: write | |
pull-requests: write | |
packages: read | |
jobs: | |
test: | |
name: Report diffs (using deployment url) | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v3 | |
- name: Create Deployment | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
GITHUB_REPOSITORY: ${{ github.repository }} | |
GITHUB_SHA: ${{ github.sha }} | |
GITHUB_REF: ${{ github.ref }} | |
PREVIEW_URL: "https://www.add-your-preview-url-for-the-commit-here.com" | |
ENVIRONMENT_NAME: "preview" | |
run: | | |
OWNER=$(echo $GITHUB_REPOSITORY | cut -d'/' -f1) | |
REPO=$(echo $GITHUB_REPOSITORY | cut -d'/' -f2) | |
DESCRIPTION="Deployment of $GITHUB_SHA" | |
LOG_URL="https://github.com/$OWNER/$REPO/commit/$GITHUB_SHA/checks" | |
# Create deployment | |
DEPLOYMENT_ID=$(curl -s -X POST \ | |
-H "Authorization: token $GITHUB_TOKEN" \ | |
-H "Accept: application/vnd.github.flash-preview+json, application/vnd.github.ant-man-preview+json" \ | |
https://api.github.com/repos/$OWNER/$REPO/deployments \ | |
-d @- <<EOF | jq -r '.id' | |
{ | |
"ref": "$GITHUB_REF", | |
"required_contexts": [], | |
"environment": "$ENVIRONMENT_NAME", | |
"description": "$DESCRIPTION", | |
"payload": { | |
"web_url": "$PREVIEW_URL" | |
} | |
} | |
EOF | |
) | |
echo "Created deployment with ID: $DEPLOYMENT_ID" | |
# Create deployment status | |
curl -s -X POST \ | |
-H "Authorization: token $GITHUB_TOKEN" \ | |
-H "Accept: application/vnd.github.flash-preview+json, application/vnd.github.ant-man-preview+json" \ | |
https://api.github.com/repos/$OWNER/$REPO/deployments/$DEPLOYMENT_ID/statuses \ | |
-d @- <<EOF | |
{ | |
"state": "success", | |
"log_url": "$LOG_URL", | |
"environment_url": "$PREVIEW_URL", | |
"description": "$DESCRIPTION" | |
} | |
EOF |