refactor(release): consolidate GitHub release publication (#1474)

* test(release): share release label and tag fixtures

* refactor(release): consolidate GitHub release publication

* fix(release): include publication helper in container builds

---------

Co-authored-by: mldangelo-oai <269034524+mldangelo-oai@users.noreply.github.com>
This commit is contained in:
mldangelo-oai
2026-10-09 01:34:47 -04:00
committed by GitHub
co-authored by mldangelo-oai
parent 97d27e4a05
commit 32ceaff7db
3 changed files with 540 additions and 579 deletions
+9 -242
View File
@@ -431,248 +431,15 @@ jobs:
MAKE_LATEST: ${{ steps.history.outputs.make-latest }}
run: |
set -euo pipefail
release_workspace="$(mktemp -d)"
trap 'rm -rf "$release_workspace"' EXIT
verify_release_tag() {
local live_ref live_type live_object live_commit
if ! live_ref="$(
gh api \
"repos/$GITHUB_REPOSITORY/git/ref/tags/$RELEASE_TAG" \
--jq '[.object.type, .object.sha] | @tsv'
)"; then
echo "GitHub release tag must still point to the verified commit." >&2
return 1
fi
IFS=$'\t' read -r live_type live_object <<< "$live_ref"
if [[ ! "$live_object" =~ ^[0-9a-fA-F]{40}$ ]]; then
echo "GitHub release tag must still point to the verified commit." >&2
return 1
fi
case "$live_type" in
commit)
live_commit="$live_object"
;;
tag)
if ! live_commit="$(
gh api \
"repos/$GITHUB_REPOSITORY/git/tags/$live_object" \
--jq 'if .object.type == "commit" then .object.sha else empty end'
)"; then
echo "GitHub release tag must still point to the verified commit." >&2
return 1
fi
;;
*)
echo "GitHub release tag must still point to the verified commit." >&2
return 1
;;
esac
if [[ ! "$live_commit" =~ ^[0-9a-fA-F]{40}$ ||
"$live_commit" != "$RELEASE_SHA" ]]; then
echo "GitHub release tag must still point to the verified commit." >&2
return 1
fi
}
github_release_or_missing() {
local endpoint="$1" label="$2" query_status=0 response
response="$(gh api --include "$endpoint" 2>/dev/null)" ||
query_status=$?
printf '%s\n' "$response" |
CODEX_SECURITY_RELEASE_QUERY_STATUS="$query_status" \
CODEX_SECURITY_RELEASE_LABEL="$label" \
node --input-type=module --eval '
import { readFileSync } from "node:fs";
const fail = () => {
throw new Error(
"Unable to resolve the " +
process.env.CODEX_SECURITY_RELEASE_LABEL +
".",
);
};
const raw = readFileSync(0, "utf8");
const separator = raw.search(/\r?\n\r?\n/u);
const status = /^HTTP\/\d+(?:\.\d+)?\s+(\d{3})(?:\s|$)/u.exec(
raw.slice(0, separator),
);
if (separator < 0 || status == null) fail();
let response;
try {
response = JSON.parse(
raw.slice(separator + (raw[separator] === "\r" ? 4 : 2)),
);
} catch {
fail();
}
if (response == null || typeof response !== "object") fail();
if (process.env.CODEX_SECURITY_RELEASE_QUERY_STATUS === "0") {
if (Number(status[1]) < 200 || Number(status[1]) >= 300) fail();
process.stdout.write(JSON.stringify(response));
} else if (status[1] !== "404") {
fail();
}
'
}
existing_release="$(
github_release_or_missing \
"repos/$GITHUB_REPOSITORY/releases/tags/$RELEASE_TAG" \
"existing GitHub Release"
)"
if [[ -n "$existing_release" ]]; then
asset_name="$(basename "$RELEASE_ARCHIVE")"
downloaded_assets="$release_workspace/downloaded-assets"
mkdir "$downloaded_assets"
gh release download "$RELEASE_TAG" \
--repo "$GITHUB_REPOSITORY" \
--pattern "$asset_name" \
--dir "$downloaded_assets"
downloaded_archive="$downloaded_assets/$asset_name"
if [[ ! -f "$downloaded_archive" || -L "$downloaded_archive" ]]; then
echo "Expected the exact existing GitHub release asset." >&2
exit 1
fi
printf '%s\n' "$existing_release" |
node sdk/typescript/scripts/release-automation.mjs \
verify-github-release "$RELEASE_ARCHIVE" "$RELEASE_TAG" \
"$downloaded_archive"
fi
existing_notes_file="$release_workspace/existing-notes.md"
: > "$existing_notes_file"
if [[ -n "$existing_release" ]]; then
printf '%s\n' "$existing_release" |
node --input-type=module --eval '
import { readFileSync, writeFileSync } from "node:fs";
const release = JSON.parse(readFileSync(0, "utf8"));
if (release.body != null && typeof release.body !== "string") {
throw new Error("Existing release notes must be a string.");
}
writeFileSync(
process.argv[1],
(release.body ?? "").replace(/(?:\r?\n)+$/u, ""),
);
' "$existing_notes_file"
fi
notes_args=(
--method POST
"repos/$GITHUB_REPOSITORY/releases/generate-notes"
-f "tag_name=$RELEASE_TAG"
-f "target_commitish=$RELEASE_SHA"
)
if git cat-file -e "$RELEASE_SHA:.github/release.yml" 2>/dev/null; then
notes_args+=(-f "configuration_file_path=.github/release.yml")
fi
if [[ -n "$PREVIOUS_TAG" ]]; then
notes_args+=(-f "previous_tag_name=$PREVIOUS_TAG")
fi
generated_notes_file="$release_workspace/generated-notes.md"
gh api "${notes_args[@]}" |
node --input-type=module --eval '
import { readFileSync, writeFileSync } from "node:fs";
const response = JSON.parse(readFileSync(0, "utf8"));
if (typeof response.body !== "string") {
throw new Error("Generated GitHub release notes must be a string.");
}
writeFileSync(process.argv[1], response.body);
' "$generated_notes_file"
release_note_args=()
tagged_notes_file="$release_workspace/tagged-notes.md"
if git show "$RELEASE_SHA:.github/release-notes.md" \
> "$tagged_notes_file" 2>/dev/null; then
release_note_args+=(
--tagged-notes-file "$tagged_notes_file"
)
fi
if [[ -s "$existing_notes_file" ]]; then
release_note_args+=(
--existing-notes-file "$existing_notes_file"
)
fi
published_notes_file="$release_workspace/published-notes.md"
node sdk/typescript/scripts/release-automation.mjs \
compose-release-notes \
"$RELEASE_VERSION" \
"$generated_notes_file" \
${release_note_args[@]+"${release_note_args[@]}"} \
> "$published_notes_file"
if [[ -n "$existing_release" ]]; then
latest_response="$(
github_release_or_missing \
"repos/$GITHUB_REPOSITORY/releases/latest" \
"current Latest GitHub Release"
)"
current_latest_tag=""
if [[ -n "$latest_response" ]]; then
current_latest_tag="$(
printf '%s\n' "$latest_response" |
node --input-type=module --eval '
import { readFileSync } from "node:fs";
const response = JSON.parse(readFileSync(0, "utf8"));
if (
typeof response.tag_name !== "string" ||
response.tag_name.length === 0
) {
throw new Error(
"Unable to resolve the current Latest GitHub Release.",
);
}
process.stdout.write(response.tag_name);
'
)"
fi
currently_latest=false
if [[ "$current_latest_tag" == "$RELEASE_TAG" ]]; then
currently_latest=true
fi
notes_changed=false
if ! cmp -s "$existing_notes_file" "$published_notes_file"; then
notes_changed=true
fi
if [[ "$notes_changed" == true ||
"$currently_latest" != "$MAKE_LATEST" ]]; then
verify_release_tag
if [[ "$notes_changed" == true ]]; then
gh release edit "$RELEASE_TAG" \
--repo "$GITHUB_REPOSITORY" \
--latest="$MAKE_LATEST" \
--notes-file "$published_notes_file"
echo "Updated existing GitHub Release with reviewed and generated notes."
else
gh release edit "$RELEASE_TAG" \
--repo "$GITHUB_REPOSITORY" \
--latest="$MAKE_LATEST"
fi
if [[ "$currently_latest" != "$MAKE_LATEST" ]]; then
echo "Updated existing GitHub Release Latest status."
fi
else
echo "The verified GitHub Release already exists."
fi
exit 0
fi
release_args=(
"$RELEASE_TAG"
"$RELEASE_ARCHIVE"
--repo "$GITHUB_REPOSITORY"
--title "Codex Security $RELEASE_VERSION"
--verify-tag
--notes-file "$published_notes_file"
--latest="$MAKE_LATEST"
)
verify_release_tag
gh release create "${release_args[@]}"
node --input-type=module --eval '
import { publishGitHubRelease } from "./sdk/typescript/scripts/publish-github-release.mjs";
try {
publishGitHubRelease(process.env, process.argv[1]);
} catch (error) {
if (error.status === undefined) throw error;
process.exit(error.status);
}
' "$release_workspace"