Skip to content

Example: create deployments with a custom preview URL #13

Example: create deployments with a custom preview URL

Example: create deployments with a custom preview URL #13

# 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
# In the case of CI runs for commits on pull requests GitHub creates a temporary merge commit, merging in the current
# head of the main/master/develop branch, and then runs the workflow on that commit, ${{ github.sha }}. However
# if our $PREVIEW_URL points to a deployment of the commit at the head of the pull request, rather than a deployment
# of the temporary merge commit then we want to tag the commit at the head of the pull request with our deployment.
# We call this the 'triggering commit SHA'.
- name: Get triggering commit SHA
id: get_sha
run: |
if [[ "${{ github.event_name }}" == "push" ]]; then
echo "TRIGGERING_COMMIT_SHA=${{ github.event.before }}" >> $GITHUB_ENV
elif [[ "${{ github.event_name }}" == "pull_request" ]]; then
echo "TRIGGERING_COMMIT_SHA=${{ github.event.pull_request.head.sha }}" >> $GITHUB_ENV
fi
- name: Create Deployment
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_REPOSITORY: ${{ github.repository }}
COMMIT_SHA: ${{ env.TRIGGERING_COMMIT_SHA }}
# TODO: Comment out the above line and uncommment the below line if your deployment is a deployment
# of the temporary merge commit (i.e. the code checked out in this workflow) rather than the commit
# at the head of the pull request branch.
# COMMIT_SHA: ${{ github.sha }}
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\":\"$COMMIT_SHA\",\"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"