fix(workflow): announce-discussion unbound var + literal $VAR text (#21)

Two bugs in `.github/workflows/announce-discussion.yml` surfaced when
v1.3.1 published:

1. `DEPLOY_URL: unbound variable` when triggered by `release` event.
   `${{ github.event.workflow_run.* }}` evaluates to null on a release
   event, and the resulting env-var assignment leaves the variable
   unset (not empty), which `set -u` then trips on. Guard every
   dynamic value with `|| ''` so each env var is always at least an
   empty string.

2. Body strings used `$'...'` ANSI-C quoting (which expands backslash
   escapes but NOT variables), so the discussion body would have
   contained literal `$RELEASE_TAG` text. Rewrite to plain double-quoted
   strings, using `$'\n'` only for embedded newlines.

Also pass dynamic values (REPO_ID / CAT_ID / TITLE / BODY) into the
Create Discussion step through env vars instead of literal `${{ ... }}`
substitution — protects against titles/bodies containing quotes or
other shell-special characters that would otherwise break the command
line.

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
poli0981andClaude Opus 4.7 authored and GitHub committed 2026-05-13 18:32:59 +07:00
1 parent 6b9cc64331
commit 4bf239e089
1 file changed
+31 -16
+31 -16
View File
@@ -76,30 +76,38 @@ jobs:
id: msg
env:
EVENT: ${{ github.event_name }}
RELEASE_TAG: ${{ github.event.release.tag_name }}
RELEASE_URL: ${{ github.event.release.html_url }}
RELEASE_BODY: ${{ github.event.release.body }}
DEPLOY_SHA: ${{ github.event.workflow_run.head_sha }}
DEPLOY_URL: ${{ github.event.workflow_run.html_url }}
DISPATCH_MSG: ${{ inputs.message }}
# `|| ''` guards each value so the env var is always set to an
# empty string (not unset) when the source field is missing for
# the active event type. Without this, `set -u` blows up below.
RELEASE_TAG: ${{ github.event.release.tag_name || '' }}
RELEASE_URL: ${{ github.event.release.html_url || '' }}
RELEASE_BODY: ${{ github.event.release.body || '' }}
DEPLOY_SHA: ${{ github.event.workflow_run.head_sha || '' }}
DEPLOY_URL: ${{ github.event.workflow_run.html_url || '' }}
DISPATCH_MSG: ${{ inputs.message || '' }}
run: |
set -euo pipefail
# NL is a literal newline used to assemble multi-line bodies
# without breaking the YAML literal-block-scalar indentation.
# Concatenating "$VAR""$NL""$NL""$OTHER" gives both interpolation
# AND embedded blank lines.
NL=$'\n'
case "$EVENT" in
release)
title="Release $RELEASE_TAG"
body=$'## $RELEASE_TAG\n\n$RELEASE_BODY\n\n[View release →]($RELEASE_URL)'
title="Release ${RELEASE_TAG}"
body="## ${RELEASE_TAG}${NL}${NL}${RELEASE_BODY}${NL}${NL}[View release →](${RELEASE_URL})"
;;
workflow_run)
short_sha="${DEPLOY_SHA:0:7}"
title="Deploy succeeded — $short_sha"
body=$'Latest commit deployed to GitHub Pages.\n\n[Deploy run →]($DEPLOY_URL)'
title="Deploy succeeded — ${short_sha}"
body="Latest commit deployed to GitHub Pages.${NL}${NL}[Deploy run →](${DEPLOY_URL})"
;;
workflow_dispatch)
title="Announcement"
body="$DISPATCH_MSG"
body="${DISPATCH_MSG}"
;;
*)
echo "::error::Unsupported event: $EVENT"
echo "::error::Unsupported event: ${EVENT}"
exit 1
;;
esac
@@ -115,6 +123,13 @@ jobs:
- name: Create Discussion
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Pass dynamic values through env vars (not literal `${{ ... }}`
# substitution) so a title/body containing quotes, backticks, or
# other shell-special characters doesn't break the command line.
REPO_ID: ${{ steps.ids.outputs.repo_id }}
CAT_ID: ${{ steps.ids.outputs.cat_id }}
TITLE: ${{ steps.msg.outputs.title }}
BODY: ${{ steps.msg.outputs.body }}
run: |
set -euo pipefail
MUTATION='mutation($repo: ID!, $cat: ID!, $title: String!, $body: String!) {
@@ -124,8 +139,8 @@ jobs:
}'
gh api graphql \
-f query="$MUTATION" \
-F repo="${{ steps.ids.outputs.repo_id }}" \
-F cat="${{ steps.ids.outputs.cat_id }}" \
-F title="${{ steps.msg.outputs.title }}" \
-F body="${{ steps.msg.outputs.body }}" \
-F repo="$REPO_ID" \
-F cat="$CAT_ID" \
-F title="$TITLE" \
-F body="$BODY" \
--jq '.data.createDiscussion.discussion.url'