-
Notifications
You must be signed in to change notification settings - Fork 111
ci: surface stable preview URLs in PRs #2799
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| #!/usr/bin/env bash | ||
| set -euo pipefail | ||
|
|
||
| SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||
| # shellcheck source=.github/scripts/preview/common.sh | ||
| source "${SCRIPT_DIR}/common.sh" | ||
|
|
||
| require_env_vars \ | ||
| GITHUB_TOKEN \ | ||
| GITHUB_API_URL \ | ||
| GITHUB_REPOSITORY \ | ||
| PR_NUMBER \ | ||
| API_URL \ | ||
| UI_URL | ||
|
|
||
| COMMENT_MARKER="<!-- preview-environments:stable-urls -->" | ||
| COMMENTS_ENDPOINT="${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/comments" | ||
| API_HEALTH_URL="${API_URL%/}/health" | ||
| COMMENT_BODY_FILE="$(mktemp)" | ||
|
|
||
| cat > "${COMMENT_BODY_FILE}" <<EOF | ||
| ${COMMENT_MARKER} | ||
| ## Preview URLs | ||
|
|
||
| Use these stable preview aliases for testing this PR: | ||
|
|
||
| - UI: [${UI_URL}](${UI_URL}) | ||
| - API: [${API_URL}](${API_URL}) | ||
| - API health: [${API_HEALTH_URL}](${API_HEALTH_URL}) | ||
|
|
||
| These point to the same Vercel preview deployment as the bot comment, but they stay stable and easier to find. | ||
| EOF | ||
|
|
||
| if [ -n "${UI_DEPLOYMENT_URL:-}" ] || [ -n "${API_DEPLOYMENT_URL:-}" ]; then | ||
| { | ||
| echo | ||
| echo "<details>" | ||
| echo "<summary>Raw Vercel deployment URLs</summary>" | ||
| echo | ||
| if [ -n "${UI_DEPLOYMENT_URL:-}" ]; then | ||
| echo "- UI deployment: [${UI_DEPLOYMENT_URL}](${UI_DEPLOYMENT_URL})" | ||
| fi | ||
| if [ -n "${API_DEPLOYMENT_URL:-}" ]; then | ||
| echo "- API deployment: [${API_DEPLOYMENT_URL}](${API_DEPLOYMENT_URL})" | ||
| fi | ||
| echo "</details>" | ||
| } >> "${COMMENT_BODY_FILE}" | ||
| fi | ||
|
|
||
| COMMENTS_JSON="$( | ||
| curl --connect-timeout 10 --max-time 30 -fsS \ | ||
| -H "Authorization: Bearer ${GITHUB_TOKEN}" \ | ||
| -H "Accept: application/vnd.github+json" \ | ||
| "${COMMENTS_ENDPOINT}?per_page=100" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💭 Consider: Pagination limited to 100 comments Issue: The GitHub API comments endpoint is paginated and the query fetches only Why: While uncommon, PRs with extensive discussion or automated bot activity could exceed this limit. The jq query correctly selects the Fix: This is a known edge case that's acceptable for most PRs. If you want complete coverage, you'd need to iterate through pages. Alternatively, document this as a known limitation since busy PRs with 100+ comments are rare. Refs: |
||
| )" | ||
|
|
||
| EXISTING_COMMENT_ID="$( | ||
| jq -r \ | ||
| --arg marker "${COMMENT_MARKER}" \ | ||
| '[.[] | select(.user.login == "github-actions[bot]" and (.body | contains($marker)))] | last | .id // empty' \ | ||
| <<< "${COMMENTS_JSON}" | ||
| )" | ||
|
|
||
| COMMENT_PAYLOAD="$(jq -Rs '{body: .}' < "${COMMENT_BODY_FILE}")" | ||
|
|
||
| if [ -n "${EXISTING_COMMENT_ID}" ]; then | ||
| curl --connect-timeout 10 --max-time 30 -fsS \ | ||
| -X PATCH \ | ||
| -H "Authorization: Bearer ${GITHUB_TOKEN}" \ | ||
| -H "Accept: application/vnd.github+json" \ | ||
| -H "Content-Type: application/json" \ | ||
| "${COMMENTS_ENDPOINT}/${EXISTING_COMMENT_ID}" \ | ||
| --data "${COMMENT_PAYLOAD}" >/dev/null | ||
| else | ||
| curl --connect-timeout 10 --max-time 30 -fsS \ | ||
| -X POST \ | ||
| -H "Authorization: Bearer ${GITHUB_TOKEN}" \ | ||
| -H "Accept: application/vnd.github+json" \ | ||
| -H "Content-Type: application/json" \ | ||
| "${COMMENTS_ENDPOINT}" \ | ||
| --data "${COMMENT_PAYLOAD}" >/dev/null | ||
| fi | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💭 Consider: Add cleanup trap for temp file
Issue: The temporary file created with
mktempis not explicitly cleaned up on exit.Why: While GitHub Actions runners are ephemeral and the file will be deleted when the runner terminates, explicit cleanup is a best practice and improves local testability of the script.
Fix: Add a trap after this line:
Refs: