Files
claude-howto/.github/workflows/release.yml
Luong NGUYEN 2fa192d02a ci(release): publish a draft naming missing languages when a build fails (#257)
When a language build fails, the release job now publishes a **draft** whose body starts with a note naming the missing languages. Before this change it published a normal **Latest** release that said nothing about the missing languages.

## Behaviour by case

| Case | Result |
|------|--------|
| All 6 builds succeed | Normal, non-draft release, as before: `generate_release_notes: true`, `fail_on_unmatched_files: true`, `draft: false`, empty `body` |
| A build fails (`needs.build.result != 'success'`) and at least one EPUB is missing | `draft: true`. The body starts with a `[!WARNING]` note naming the missing languages and linking the workflow run, followed by the generated release notes |
| A build job fails but all 6 EPUBs are present | `draft: true`. The note says a build job failed although all expected EPUBs are present |
| No EPUB at all | The `Check for missing languages` step fails with `::error::no EPUBs built, nothing to release (build result: <result>)`. Nothing is published |
| A draft for this tag already exists | The `Remove stale drafts for this tag` step deletes it before `Create Release`, so the new release, draft or not, gets a fresh body. This applies to **any** draft for the tag, including one written or edited by hand. Published releases and the tag are never touched |

The missing languages come from comparing `LANGUAGES` with the files in `dist/`. `claude-howto-guide.epub` is `en`, and `claude-howto-guide-<lang>.epub` covers every other language. The steps only read values passed through `env:`, with no `${{ }}` inside the scripts, so the tests below run the same scripts as the workflow.

## Re-running after a partial draft

The natural recovery after a partial draft is **Re-run failed jobs**. Without cleanup, `softprops/action-gh-release@v2` (`3bb1273`) would publish the old draft with its stale "Partial release" note:

- `findTagFromReleases` uses `getReleaseByTag` (`src/github.ts:739`), which only returns published releases, so it gets a 404 and goes down the create path.
- `createRelease` creates a new draft, and then `canonicalizeCreatedRelease` picks the lowest-id draft for the tag (`pickCanonicalRelease`) and deletes the new, asset-less duplicate. The old draft is kept with its old body. The create path never rewrites the body.
- Passing a different `body` doesn't help. It only reaches the new draft, which gets deleted. Even on the update path, an empty body keeps the old one (`src/github.ts:566`: `body = workflowBody || existingReleaseBody`).

So the release job now deletes the drafts for `$GITHUB_REF_NAME` first:

```bash
# shell: bash, i.e. bash --noprofile --norc -eo pipefail
ids=$(gh api --paginate "repos/$GH_REPO/releases?per_page=100" |
  jq -r --arg tag "$GITHUB_REF_NAME" '.[] | select(.draft and .tag_name == $tag) | .id')
for id in $ids; do
  gh api -X DELETE "repos/$GH_REPO/releases/$id"
done
```

- Releases are deleted **by id**, and only when `draft == true`. `gh release delete <tag>` was not used because it looks the tag up again through `FetchRelease`, which races a published-release lookup against a draft lookup and returns whichever succeeds first (`pkg/cmd/release/shared/fetch.go:183-223` in gh v2.102.0). If a published release and a draft shared the tag, a `view` followed by a `delete` by tag could hit different releases.
- Only the release is deleted. The tag stays.
- The REST list includes drafts only for users with push access ([docs](https://docs.github.com/en/rest/releases/releases#list-releases)). The job has `contents: write` and uses `GH_TOKEN: ${{ github.token }}`. It also sets `GH_REPO`, because the release job doesn't check out the repo.
- The step runs after `check`, so if no EPUB was built, the old draft isn't deleted. It runs on the draft path too, so a re-run that is still partial gets a note with the current missing list.
- If listing the releases fails, the step fails before any delete and nothing is published. This holds because the step sets `shell: bash`. A `run:` step without `shell:` runs as `bash -e {0}`, with no pipefail, so a `gh` failure with nothing on stdout would let `jq` exit 0. The [workflow syntax docs](https://docs.github.com/en/actions/reference/workflows-and-actions/workflow-syntax#jobsjob_idstepsshell) say: "By default, fail-fast behavior is enforced using `set -e` for both `sh` and `bash`. When `shell: bash` is specified, `-o pipefail` is also applied to enforce early exit from pipelines that generate a non-zero exit status." `Check for missing languages` sets `shell: bash` too.
- Every run that gets past the missing-languages check (a full success or a partial build, re-runs included) deletes **any** draft release for the tag, including one a maintainer wrote or edited by hand before pushing the tag or re-running. Save hand-written notes somewhere else first.
- The cleanup only removes drafts. If someone publishes a partial draft by hand, it keeps its "Partial release … saved as a draft" warning until someone edits the body. A later re-run takes the action's update path, which keeps an existing body when `body` is empty (`src/github.ts:566`). To recover, re-run while the release is still a draft, or remove the note when publishing by hand.
- If a tag whose release is already **published** is re-run and a build fails, the release stays published, but its body is replaced by the "saved as a draft" warning. This is rare and harmless; edit the body after a successful re-run.

## Language lists

`LANGUAGES: en vi zh ja ko fa` (top-level `env`) is what the check compares against. The build matrix is the plain `lang: [en, vi, zh, ja, ko, fa]`, because `strategy.matrix` can't read `env`. A comment next to each says to keep it in sync with the other, with `test.yml` build-epub and with `scripts/build_epub.py` `lang_map`. To add a language, edit all four.

Drift between the two lists can't publish a silent Latest:

- A language in the matrix but not in `LANGUAGES` that fails still makes `needs.build.result` `failure`, so the release is a draft. The note is just less specific.
- A language in `LANGUAGES` but not in the matrix is always reported missing, so every tag becomes a draft.

## Action inputs (softprops/action-gh-release v2)

Source: the inputs table in [README.md at `v2`](https://github.com/softprops/action-gh-release/blob/v2/README.md#inputs) and `action.yml` at `v2`, checked on 2026-11-12.

- `draft`: "Keep the release as a draft. Defaults to false."
- `generate_release_notes`: "If body is specified, the body will be pre-pended to the automatically generated notes." This keeps the note above the generated notes, so `append_body` isn't needed. That input only appends to the body of an existing release.
- The `src/util.ts` and `src/github.ts` source at `v2` shows that `draft: false` and an empty `body` act the same as leaving them unset. The action creates a draft, uploads the files, and publishes it unless `input_draft === true`. So a full build behaves as it did before.

## Verification

`release.yml` only runs on tags, so nothing was triggered and no release was touched. The `run:` scripts were taken from the parsed YAML. Each one ran under the command its `shell:` maps to: `bash --noprofile --norc -eo pipefail` for `shell: bash`, and `bash -e` for the old unset `shell:`.

`Check for missing languages` against fake `dist/` dirs:

```text
===== all6 (BUILD_RESULT=success; present: en vi zh ja ko fa) =====
  dist/claude-howto-guide-fa.epub
  dist/claude-howto-guide-ja.epub
  dist/claude-howto-guide-ko.epub
  dist/claude-howto-guide-vi.epub
  dist/claude-howto-guide-zh.epub
  dist/claude-howto-guide.epub
exit=0
--- GITHUB_OUTPUT:
draft=false
note=
===== missing-ja-fa (BUILD_RESULT=failure; present: en vi zh ko) =====
  dist/claude-howto-guide-ko.epub
  dist/claude-howto-guide-vi.epub
  dist/claude-howto-guide-zh.epub
  dist/claude-howto-guide.epub
::warning::partial build, publishing a draft (missing: ja fa)
exit=0
--- GITHUB_OUTPUT:
draft=true
note<<EOF_NOTE
> [!WARNING]
> **Partial release.** Missing languages: ja, fa.
> See the [workflow run](https://github.com/OWNER/REPO/actions/runs/1). This release was saved as a draft;
> review it before publishing.

EOF_NOTE
===== only-en (BUILD_RESULT=failure; present: en) =====
  dist/claude-howto-guide.epub
::warning::partial build, publishing a draft (missing: vi zh ja ko fa)
exit=0
--- GITHUB_OUTPUT:
draft=true
note<<EOF_NOTE
> [!WARNING]
> **Partial release.** Missing languages: vi, zh, ja, ko, fa.
> See the [workflow run](https://github.com/OWNER/REPO/actions/runs/1). This release was saved as a draft;
> review it before publishing.

EOF_NOTE
===== none (BUILD_RESULT=failure; present: none) =====
::error::no EPUBs built, nothing to release (build result: failure)
exit=1
--- GITHUB_OUTPUT:
===== no-dir (BUILD_RESULT=failure; present: NODIR) =====
::error::no EPUBs built, nothing to release (build result: failure)
exit=1
--- GITHUB_OUTPUT:
===== build-skipped (BUILD_RESULT=skipped; present: NODIR) =====
::error::no EPUBs built, nothing to release (build result: skipped)
exit=1
--- GITHUB_OUTPUT:
===== failure-but-all-present (BUILD_RESULT=failure; present: en vi zh ja ko fa) =====
  dist/claude-howto-guide-fa.epub
  dist/claude-howto-guide-ja.epub
  dist/claude-howto-guide-ko.epub
  dist/claude-howto-guide-vi.epub
  dist/claude-howto-guide-zh.epub
  dist/claude-howto-guide.epub
::warning::build result is failure although all EPUBs are present, publishing a draft
exit=0
--- GITHUB_OUTPUT:
draft=true
note<<EOF_NOTE
> [!WARNING]
> **Draft release.** A build job failed although all expected EPUBs are present.
> See the [workflow run](https://github.com/OWNER/REPO/actions/runs/1). This release was saved as a draft;
> review it before publishing.

EOF_NOTE
===== success-but-ko-missing (BUILD_RESULT=success; present: en vi zh ja fa) =====
  dist/claude-howto-guide-fa.epub
  dist/claude-howto-guide-ja.epub
  dist/claude-howto-guide-vi.epub
  dist/claude-howto-guide-zh.epub
  dist/claude-howto-guide.epub
::warning::partial build, publishing a draft (missing: ko)
exit=0
--- GITHUB_OUTPUT:
draft=true
note<<EOF_NOTE
> [!WARNING]
> **Partial release.** Missing languages: ko.
> See the [workflow run](https://github.com/OWNER/REPO/actions/runs/1). This release was saved as a draft;
> review it before publishing.

EOF_NOTE
```

`Remove stale drafts for this tag` with a fake `gh` on `PATH`. It serves fixture release pages, one JSON array per page (or one merged array in `merged` mode), and logs every call. It refuses (exit 99) to delete a release that isn't a draft in the fixtures. The fixtures are:

- `other-tag-drafts-*`: page 1 has a `v9.9.8` draft and a `v9.9.9` draft. Page 2 has a `v9.9.9` draft, a `v9.9.9-rc` draft and a published `v9.9.9`.
- `*-json-body`: gh prints a JSON error object on stdout and exits 1, which is what real gh does on an HTTP error.
- `*-empty`: nothing on stdout and exit 1, a transport error.
- `page2-*`: page 1 succeeds and then page 2 fails.

```text
===== no-release [bash -eo pipefail, gh mode: ok] =====
  exit=0  deleted ids: []
  gh calls:
    gh api --paginate repos/OWNER/REPO/releases?per_page=100
===== draft-exists [bash -eo pipefail, gh mode: ok] =====
  | Deleting stale draft release 101 for v9.9.9
  exit=0  deleted ids: [101]
  gh calls:
    gh api --paginate repos/OWNER/REPO/releases?per_page=100
    gh api -X DELETE repos/OWNER/REPO/releases/101
===== published-exists [bash -eo pipefail, gh mode: ok] =====
  exit=0  deleted ids: []
  gh calls:
    gh api --paginate repos/OWNER/REPO/releases?per_page=100
===== published-and-draft [bash -eo pipefail, gh mode: ok] =====
  | Deleting stale draft release 101 for v9.9.9
  exit=0  deleted ids: [101]
  gh calls:
    gh api --paginate repos/OWNER/REPO/releases?per_page=100
    gh api -X DELETE repos/OWNER/REPO/releases/101
===== other-tag-drafts-2pages [bash -eo pipefail, gh mode: ok] =====
  | Deleting stale draft release 101 for v9.9.9
  | Deleting stale draft release 102 for v9.9.9
  exit=0  deleted ids: [101,102]
  gh calls:
    gh api --paginate repos/OWNER/REPO/releases?per_page=100
    gh api -X DELETE repos/OWNER/REPO/releases/101
    gh api -X DELETE repos/OWNER/REPO/releases/102
===== other-tag-drafts-merged [bash -eo pipefail, gh mode: merged] =====
  | Deleting stale draft release 101 for v9.9.9
  | Deleting stale draft release 102 for v9.9.9
  exit=0  deleted ids: [101,102]
  gh calls:
    gh api --paginate repos/OWNER/REPO/releases?per_page=100
    gh api -X DELETE repos/OWNER/REPO/releases/101
    gh api -X DELETE repos/OWNER/REPO/releases/102
===== list-fails-json-body [bash -eo pipefail, gh mode: errjson] =====
  | gh: Server Error (HTTP 500)
  | jq: error (at <stdin>:1): Cannot index string with string "draft"
  exit=5  deleted ids: []
  gh calls:
    gh api --paginate repos/OWNER/REPO/releases?per_page=100
===== list-fails-empty-stdout [bash -eo pipefail, gh mode: empty] =====
  | gh: connection reset by peer
  exit=1  deleted ids: []
  gh calls:
    gh api --paginate repos/OWNER/REPO/releases?per_page=100
===== page2-fails-json-body [bash -eo pipefail, gh mode: p2err] =====
  | gh: HTTP 500
  | jq: error (at <stdin>:2): Cannot index string with string "draft"
  exit=5  deleted ids: []
  gh calls:
    gh api --paginate repos/OWNER/REPO/releases?per_page=100
===== page2-fails-empty [bash -eo pipefail, gh mode: p2empty] =====
  | gh: connection reset by peer
  exit=1  deleted ids: []
  gh calls:
    gh api --paginate repos/OWNER/REPO/releases?per_page=100
```

For contrast, the intermediate version of this step at `41ab1e3` (never on `main`), with no `shell:` so it ran as `bash -e`, on the two cases where the result differs. The step passed after the list call failed, and on a page-2 failure it deleted the page-1 draft. The other 8 cases give the same result under both shells.

```text
===== list-fails-empty-stdout [bash -e, gh mode: empty] =====
  | gh: connection reset by peer
  exit=0  deleted ids: []
  gh calls:
    gh api --paginate repos/OWNER/REPO/releases
===== page2-fails-empty [bash -e, gh mode: p2empty] =====
  | gh: connection reset by peer
  | Deleting stale draft release 101 for v9.9.9
  exit=0  deleted ids: [101]
  gh calls:
    gh api --paginate repos/OWNER/REPO/releases
    gh api -X DELETE repos/OWNER/REPO/releases/101
```

Other checks:

- The YAML parses. Both release-job `run:` steps have `shell: bash`, and there is no `defaults:` block. `actionlint` 1.7.12 (release binary, checksum verified) with `shellcheck` 0.11.0 reports nothing on `release.yml` (exit 0). A probe workflow confirmed that actionlint does call shellcheck. `shellcheck -s bash` on both extracted scripts exits 0.
- `pre-commit run --files .github/workflows/release.yml` passes. The mermaid hooks were skipped (`SKIP=mermaid,…`) because they don't check `.yml` files.
- The EPUB build was not run.

Closes #254
2026-11-12 14:16:21 +01:00

164 lines
5.7 KiB
YAML

name: Build and Release EPUB
on:
push:
tags:
- 'v*'
env:
# Languages the release job expects an EPUB for. Keep in sync with the build
# matrix below, `test.yml` build-epub, and `scripts/build_epub.py` lang_map.
LANGUAGES: en vi zh ja ko fa
jobs:
build:
name: Build EPUB (${{ matrix.lang }})
runs-on: ubuntu-latest
env:
# Pin every uv command (uv venv and the PEP 723 env of `uv run`) to 3.11.
UV_PYTHON: '3.11'
strategy:
# Don't cancel other languages if one fails — the release job still
# drafts a release with whichever languages built successfully.
fail-fast: false
matrix:
# Keep in sync with LANGUAGES above, `test.yml` build-epub, and
# `scripts/build_epub.py` lang_map.
lang: [en, vi, zh, ja, ko, fa]
steps:
- uses: actions/checkout@v4
- name: Install uv
uses: astral-sh/setup-uv@v4
# Pele renders the diagrams and needs Node.js >= 20.
- name: Install Node.js
uses: actions/setup-node@v4
with:
node-version: '22'
- name: Set up Python
run: uv python install 3.11
- name: Install Python dependencies
run: |
uv venv
uv pip install -r scripts/requirements-dev.txt
- name: Build EPUB
run: uv run scripts/build_epub.py --lang ${{ matrix.lang }}
- name: Verify EPUB diagrams
run: python3 scripts/check_epub_diagrams.py claude-howto-guide*.epub
- name: Upload EPUB artifact
uses: actions/upload-artifact@v4
with:
name: epub-${{ matrix.lang }}
path: claude-howto-guide*.epub
if-no-files-found: error
retention-days: 7
release:
name: Publish GitHub Release
needs: build
# Run even if some language builds failed: the release is then a draft
# whose body names the missing languages. `needs.build.result !=
# 'cancelled'` guards against manually cancelled runs.
if: ${{ always() && needs.build.result != 'cancelled' }}
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Download all EPUB artifacts
uses: actions/download-artifact@v4
with:
path: dist
pattern: epub-*
merge-multiple: true
- name: Check for missing languages
id: check
env:
BUILD_RESULT: ${{ needs.build.result }}
RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
# Explicit `shell: bash` adds `-o pipefail`; the default is `bash -e` only.
shell: bash
run: |
shopt -s nullglob
epubs=(dist/claude-howto-guide*.epub)
if [ "${#epubs[@]}" -eq 0 ]; then
echo "::error::no EPUBs built, nothing to release (build result: $BUILD_RESULT)"
exit 1
fi
ls -lh "${epubs[@]}"
missing=()
for lang in $LANGUAGES; do
if [ "$lang" = en ]; then
file=dist/claude-howto-guide.epub
else
file="dist/claude-howto-guide-$lang.epub"
fi
[ -f "$file" ] || missing+=("$lang")
done
if [ "$BUILD_RESULT" = success ] && [ "${#missing[@]}" -eq 0 ]; then
echo "draft=false" >> "$GITHUB_OUTPUT"
echo "note=" >> "$GITHUB_OUTPUT"
exit 0
fi
if [ "${#missing[@]}" -eq 0 ]; then
headline="**Draft release.** A build job failed although all expected EPUBs are present."
echo "::warning::build result is $BUILD_RESULT although all EPUBs are present, publishing a draft"
else
list="${missing[*]}"
headline="**Partial release.** Missing languages: ${list// /, }."
echo "::warning::partial build, publishing a draft (missing: $list)"
fi
{
echo "draft=true"
echo "note<<EOF_NOTE"
echo "> [!WARNING]"
echo "> $headline"
echo "> See the [workflow run]($RUN_URL). This release was saved as a draft;"
echo "> review it before publishing."
echo ""
echo "EOF_NOTE"
} >> "$GITHUB_OUTPUT"
# A draft left for this tag by an earlier partial run (e.g. before "Re-run
# failed jobs") would be reused by action-gh-release, which keeps the
# oldest draft and never rewrites its body on create, so its stale
# warning would survive. Delete such drafts first. Only releases with
# `draft == true` are deleted, by id: a published release is never
# touched, and the tag stays.
- name: Remove stale drafts for this tag
env:
GH_TOKEN: ${{ github.token }}
GH_REPO: ${{ github.repository }}
# Explicit `shell: bash` adds `-o pipefail`, so a failed list call fails
# the step before any delete; the default is `bash -e` only.
shell: bash
run: |
ids=$(gh api --paginate "repos/$GH_REPO/releases?per_page=100" |
jq -r --arg tag "$GITHUB_REF_NAME" '.[] | select(.draft and .tag_name == $tag) | .id')
for id in $ids; do
echo "Deleting stale draft release $id for $GITHUB_REF_NAME"
gh api -X DELETE "repos/$GH_REPO/releases/$id"
done
# softprops/action-gh-release v2: `body` is prepended to the generated
# notes; `draft: true` keeps the release unpublished.
- name: Create Release
uses: softprops/action-gh-release@v2
with:
files: dist/*.epub
draft: ${{ steps.check.outputs.draft }}
body: ${{ steps.check.outputs.note }}
generate_release_notes: true
fail_on_unmatched_files: true