Skip to content

Example: create deployments with a custom preview URL #3

Example: create deployments with a custom preview URL

Example: create deployments with a custom preview URL #3

# 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 Preview URL
id: create-url
run: |
# Generate the preview URL (you can modify this logic to suit your needs)
PREVIEW_URL="https://example.org/example/preview/url/for/$GITHUB_SHA"
echo "Preview URL: $PREVIEW_URL"
# Set it as an output for the next step
echo "preview_url=$PREVIEW_URL" >> $GITHUB_OUTPUT
- name: Create Deployment
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_REPOSITORY: ${{ github.repository }}
GITHUB_SHA: ${{ github.sha }}
GITHUB_REF: ${{ github.ref }}
PREVIEW_URL: ${{ steps.create-url.outputs.preview_url }}
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