Skip to content

Example: create deployments with a custom preview URL #9

Example: create deployments with a custom preview URL

Example: create deployments with a custom preview URL #9

# 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
on:
push:
branches:
- main
- releases/*
pull_request: {}
workflow_dispatch: {}
permissions:
deployments: write
jobs:
create-deployment:
name: Create GitHub deployment
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"
echo "$GITHUB_REPOSITORY"
echo "Curling https://api.github.com/repos/$OWNER/$REPO/deployments"
# Create deployment
RESPONSE=$(curl -s -X POST \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-H "Accept: application/vnd.github+json" \
https://api.github.com/repos/$OWNER/$REPO/deployments \
-d "{\"ref\":\"$GITHUB_REF\",\"environment\":\"$ENVIRONMENT_NAME\",\"payload\":{ \"web_url\": \"$PREVIEW_URL\" },\"description\":\"$DESCRIPTION\"}")
echo $RESPONSE
DEPLOYMENT_ID=$(echo $RESPONSE | jq -r '.id')
echo "Created deployment with ID: $DEPLOYMENT_ID"
# Create deployment status
curl -s -X POST \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-H "Accept: application/vnd.github+json" \
https://api.github.com/repos/$OWNER/$REPO/deployments/$DEPLOYMENT_ID/statuses \
-d @- <<EOF
{
"state": "success",
"log_url": "$LOG_URL",
"environment": "$ENVIRONMENT_NAME",
"environment_url": "$PREVIEW_URL",
"description": "$DESCRIPTION"
}
EOF
echo "Created deployment status"