Skip to content

Commit 952b21e

Browse files
committed
feat: add preview environment dispatch workflow
On a same-repo PR labeled `preview`, publish a repository_dispatch (preview-deploy) to triggerdotdev/cloud to create/update/destroy a per-PR preview environment. Fork PRs never trigger it (same-repo gate, and GitHub withholds secrets from fork runs); the job is metadata-only (no checkout) and GITHUB_TOKEN has no permissions.
1 parent 9cb6fd1 commit 952b21e

1 file changed

Lines changed: 67 additions & 0 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
name: 🌱 Preview environment dispatch
2+
3+
# Opt-in per-PR preview environments
4+
5+
on:
6+
pull_request:
7+
types: [opened, reopened, synchronize, closed, labeled, unlabeled]
8+
9+
permissions: {}
10+
11+
jobs:
12+
dispatch:
13+
name: Dispatch preview-deploy to cloud
14+
runs-on: ubuntu-latest
15+
# label added -> create
16+
# new commit while labeled -> update
17+
# label removed / PR closed -> destroy
18+
if: >-
19+
github.event.pull_request.head.repo.full_name == github.repository &&
20+
(
21+
(github.event.action == 'labeled' && github.event.label.name == 'preview') ||
22+
(github.event.action == 'unlabeled' && github.event.label.name == 'preview') ||
23+
(
24+
contains(github.event.pull_request.labels.*.name, 'preview') &&
25+
contains(fromJSON('["opened","reopened","synchronize","closed"]'), github.event.action)
26+
)
27+
)
28+
steps:
29+
- name: Build dispatch payload
30+
id: payload
31+
env:
32+
ACTION: ${{ github.event.action }}
33+
BRANCH: ${{ github.event.pull_request.head.ref }}
34+
COMMIT: ${{ github.event.pull_request.head.sha }}
35+
run: |
36+
set -euo pipefail
37+
# Map the GitHub PR action to the cloud pipeline's lifecycle event.
38+
case "$ACTION" in
39+
labeled | opened | reopened) EVENT=opened ;;
40+
synchronize) EVENT=synchronize ;;
41+
unlabeled | closed) EVENT=closed ;;
42+
*) echo "unexpected action: $ACTION" >&2; exit 1 ;;
43+
esac
44+
# jq --arg JSON-escapes every value, so a branch name containing
45+
# quotes/braces can't break or inject into the client payload.
46+
payload=$(jq -nc \
47+
--arg b "$BRANCH" \
48+
--arg c "$COMMIT" \
49+
--arg e "$EVENT" \
50+
'{branch_name: $b, commit: $c, pull_request_event: $e}')
51+
{
52+
echo "client_payload=$payload"
53+
echo "summary=$EVENT for $BRANCH @ ${COMMIT:0:7}"
54+
} >> "$GITHUB_OUTPUT"
55+
56+
- name: Log dispatch
57+
env:
58+
SUMMARY: ${{ steps.payload.outputs.summary }}
59+
run: echo "Dispatching preview-deploy even ($SUMMARY)"
60+
61+
- name: Send repository_dispatch
62+
uses: peter-evans/repository-dispatch@28959ce8df70de7be546dd1250a005dd32156697 # v4.0.1
63+
with:
64+
token: ${{ secrets.CROSS_REPO_PAT }}
65+
repository: triggerdotdev/cloud
66+
event-type: preview-deploy
67+
client-payload: ${{ steps.payload.outputs.client_payload }}

0 commit comments

Comments
 (0)