From db89f922f60f75e0644c0c5a0f79984dc95821fc Mon Sep 17 00:00:00 2001 From: Oliver Lin Date: Tue, 1 Dec 2026 21:16:11 +0800 Subject: [PATCH] fix: derive the log run date in the script, not from a context field MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Every publish failed at "Stage build logs into the repository tree" with: Error: invalid run start time: Expected an ISO-8601 timestamp such as 2026-12-01T11:50:08Z The timestamp was empty because the workflow passed ${{ github.run_started_at }}, which is not a field of the github context — it only exists in the REST API. GitHub does not fail on an unknown context field; it substitutes an empty string, so log-run-dir.sh received an empty argument and correctly refused it. The publish job therefore never reached the metadata and sync steps, so the repository was not updated. Fix: the date now comes from `date -u` inside log-run-dir.sh, and the workflow passes only the run id. Nothing in the workflow interpolates a timestamp any more, which removes the whole class of failure rather than only this instance. The script still accepts an optional date argument so tests and reproductions can pin it. While updating the script, also harden the date validation: the shape-only check let impossible dates through, so 2026-13-99 and 2026-02-31 were accepted. Months and days are now range-checked and the date is normalised through `date` so a non-existent calendar day is rejected, while a valid leap day still passes. Regression coverage, since an unknown context field fails silently rather than erroring: - script-checks.yml now scans every workflow for ${{ github. }} and fails on a field that is not part of the documented github context. Verified it reports github.run_started_at and exits non-zero when the old expression is reintroduced. - The run directory test now covers the real call shape (run id only), asserts the date matches today in UTC, and covers the impossible-date rejections. Verified by extracting the workflow's own run block, substituting the expression the way GitHub does, and executing it under bash: the run directory resolves and the logs land in log/-/ with the expected GITHUB_OUTPUT. Signed-off-by: Oliver Lin --- .github/workflows/publish-apt.yml | 10 +++-- .github/workflows/script-checks.yml | 58 ++++++++++++++++++++++-- README-zh-cn.md | 7 +-- README.md | 10 +++-- scripts/log-run-dir.sh | 68 +++++++++++++++++++---------- 5 files changed, 117 insertions(+), 36 deletions(-) diff --git a/.github/workflows/publish-apt.yml b/.github/workflows/publish-apt.yml index d566e71..8d41ac1 100644 --- a/.github/workflows/publish-apt.yml +++ b/.github/workflows/publish-apt.yml @@ -130,8 +130,11 @@ jobs: id: stage run: | mkdir -p "$RUNNER_TEMP/repo" - # github.run_started_at is an ISO-8601 UTC timestamp for every trigger. - run_dir="$(./scripts/log-run-dir.sh "${{ github.run_started_at }}" "${{ github.run_id }}")" + # The date is computed inside the script. Do not pass a timestamp in + # from here: the run-start timestamp looks like a github context field + # but is only available from the REST API, so GitHub substituted an + # empty string and every publish failed with "invalid run start time:". + run_dir="$(./scripts/log-run-dir.sh "${{ github.run_id }}")" echo "Build log directory: /log/$run_dir/" ./scripts/collect-logs.sh "$RUNNER_TEMP/logs" "$RUNNER_TEMP/repo" "$run_dir" echo "run_dir=$run_dir" >> "$GITHUB_OUTPUT" @@ -291,7 +294,8 @@ jobs: - name: Stage the run's logs and rebuild the index id: stage run: | - run_dir="$(./scripts/log-run-dir.sh "${{ github.run_started_at }}" "${{ github.run_id }}")" + # The date comes from the script itself; see the stage step above. + run_dir="$(./scripts/log-run-dir.sh "${{ github.run_id }}")" echo "Run directory: $run_dir" ./scripts/stage-log-repo.sh "$RUNNER_TEMP/logrepo" "$RUNNER_TEMP/logs" "$run_dir" ./scripts/render-log-readme.sh "$RUNNER_TEMP/logrepo" "$LOG_REPO" diff --git a/.github/workflows/script-checks.yml b/.github/workflows/script-checks.yml index 8da14d5..f02b36c 100644 --- a/.github/workflows/script-checks.yml +++ b/.github/workflows/script-checks.yml @@ -30,6 +30,44 @@ jobs: - name: Check shell syntax run: bash -n scripts/*.sh .githooks/* + - name: Check the workflows only use real github context fields + # An unknown ${{ github.* }} field is not an error at run time: GitHub + # substitutes an empty string. That is how a non-existent run-start + # timestamp silently broke every publish, so the fields are checked here. + run: | + python3 - <<'PY' + import glob + import re + import sys + + # Fields of the github context; extend only from the official docs. + valid = { + "action", "actor", "actor_id", "api_url", "base_ref", "env", "event", + "event_name", "graphql_url", "head_ref", "job", "path", "ref", + "ref_name", "ref_type", "repository", "repository_id", + "repository_owner", "retention_days", "run_attempt", "run_id", + "run_number", "secret_source", "server_url", "sha", "token", + "triggering_actor", "workflow", "workflow_ref", "workflow_sha", + "workspace", + } + + bad = [] + for path in sorted(glob.glob(".github/workflows/*.yml")): + text = open(path).read() + for match in re.finditer(r"\$\{\{\s*github\.([A-Za-z_][A-Za-z0-9_.]*)", text): + field = match.group(1).split(".")[0] + if field not in valid: + bad.append(f"{path}: github.{match.group(1)}") + + if bad: + print("Unknown github context fields:", file=sys.stderr) + for entry in bad: + print(" " + entry, file=sys.stderr) + sys.exit(1) + + print("All github context fields are valid") + PY + - name: Test the log name derivation run: | test "$(./scripts/log-file-name.sh https://github.com/zitzhen/patchsplit)" = \ @@ -43,14 +81,26 @@ jobs: ! ./scripts/log-file-name.sh https://github.com/onlyowner 2>/dev/null - name: Test the run directory derivation + # The script must be callable with just a run id: the workflow passes no + # date, because the timestamp it used to pass came from a context field + # that does not exist and expanded to an empty string. run: | - test "$(./scripts/log-run-dir.sh 2026-12-01T11:50:08Z 46640252167)" = \ + test "$(./scripts/log-run-dir.sh 46640252167 2026-12-01)" = \ "2026-12-01-46640252167" - test "$(./scripts/log-run-dir.sh 2026-12-01T11:50:08+08:00 42)" = \ + test "$(./scripts/log-run-dir.sh 42 2026-12-01T11:50:08Z)" = \ "2026-12-01-42" - ! ./scripts/log-run-dir.sh not-a-date 42 2>/dev/null + + # Only a run id: the date comes from the clock and must be today in UTC. + derived="$(./scripts/log-run-dir.sh 46640252167)" + test "$derived" = "$(date -u '+%Y-%m-%d')-46640252167" + ! ./scripts/log-run-dir.sh "" 2>/dev/null # The run id becomes a path component, so anything but digits is out. - ! ./scripts/log-run-dir.sh 2026-12-01T11:50:08Z ../../etc 2>/dev/null + ! ./scripts/log-run-dir.sh ../../etc 2>/dev/null + ! ./scripts/log-run-dir.sh abc 2>/dev/null + # Impossible calendar dates must not create directories either. + ! ./scripts/log-run-dir.sh 42 2026-13-99 2>/dev/null + ! ./scripts/log-run-dir.sh 42 2026-02-31 2>/dev/null + ./scripts/log-run-dir.sh 42 2024-02-29 >/dev/null # leap day is valid - name: Test the archived log layout # Logs must land in log/-/, be listed from /log/ and stay diff --git a/README-zh-cn.md b/README-zh-cn.md index ae491d2..dd0d1c8 100644 --- a/README-zh-cn.md +++ b/README-zh-cn.md @@ -115,9 +115,10 @@ https://apt-general.zitzhen.cn/log/2026-12-01-46640252167/ # 单次运 https://apt-general.zitzhen.cn/log/2026-12-01-46640252167/zitzhen-patchsplit.log ``` -- 运行目录名为 `<日期>-`(如 `2026-12-01-46640252167`),由工作流的开始日期 - 与数字运行 ID 通过 `scripts/log-run-dir.sh` 推导。每次运行都有独立目录,不会覆盖 - 上一次。 +- 运行目录名为 `<日期>-`(如 `2026-12-01-46640252167`),由当前 UTC 日期 + 与数字运行 ID 通过 `scripts/log-run-dir.sh` 生成。日期由脚本内部读取系统时钟, + 而不是从工作流传入——取自不存在的 `github` 上下文字段的时间戳会被静默替换为空 + 字符串。每次运行都有独立目录,不会覆盖上一次。 - 目录内的文件名规则为 `-.log`,由 `Software.list` 中的 URL 推导 (`https://github.com/zitzhen/patchsplit` → `zitzhen-patchsplit.log`)。 - 日志开头记录上游地址、开始时间、runner 与 commit,结尾记录结束时间与构建退出码, diff --git a/README.md b/README.md index 3583b35..59721c5 100644 --- a/README.md +++ b/README.md @@ -129,10 +129,12 @@ https://apt-general.zitzhen.cn/log/2026-12-01-46640252167/ # one run https://apt-general.zitzhen.cn/log/2026-12-01-46640252167/zitzhen-patchsplit.log ``` -- The run directory is `-` (`2026-12-01-46640252167`), built from - the workflow's start date and its numeric run id by - `scripts/log-run-dir.sh`. Each run therefore gets a fresh directory and - nothing from an earlier run is replaced. +- The run directory is `-` (`2026-12-01-46640252167`): the current + UTC date plus the numeric run id, produced by `scripts/log-run-dir.sh`. The date + is read from the clock inside the script rather than passed in from the + workflow, because a timestamp taken from an unknown `github` context field is + silently substituted with an empty string. Each run therefore gets a fresh + directory and nothing from an earlier run is replaced. - Inside a run directory the file name is `-.log`, derived from the `Software.list` URL (`https://github.com/zitzhen/patchsplit` → `zitzhen-patchsplit.log`). diff --git a/scripts/log-run-dir.sh b/scripts/log-run-dir.sh index 1100eb4..ed9e702 100755 --- a/scripts/log-run-dir.sh +++ b/scripts/log-run-dir.sh @@ -1,33 +1,27 @@ #!/usr/bin/env bash -# Print the archive directory used for one workflow run's build logs, e.g. -# run started 2026-12-01T11:50:08Z, run id 46640252167 -> 2026-12-01-46640252167 +# Print the archive directory name for one workflow run, e.g. +# run id 46640252167 on 2026-12-01 -> 2026-12-01-46640252167 # # Logs are published under /log//-.log so that every run -# keeps its own directory and older runs are never overwritten. The name is -# derived here rather than in the workflow for the same reason as -# log-file-name.sh: shell mistakes inside a `run:` block only surface in CI. +# keeps its own directory and older runs are never overwritten. +# +# The date is computed here with `date -u` rather than passed in from the +# workflow. `${{ github.run_started_at }}` looks plausible but is not a field of +# the github context (it only exists in the REST API), so GitHub substituted an +# empty string and every publish failed with "invalid run start time:". +# Keeping the clock inside the script removes that whole failure mode. set -euo pipefail -if [[ $# -ne 2 ]]; then - echo "Usage: log-run-dir.sh " >&2 - echo " run-started-at ISO-8601 timestamp, e.g. 2026-12-01T11:50:08Z" >&2 - echo " run-id numeric workflow run id" >&2 +if [[ $# -lt 1 || $# -gt 2 ]]; then + echo "Usage: log-run-dir.sh [date]" >&2 + echo " run-id numeric workflow run id" >&2 + echo " date optional YYYY-MM-DD or ISO-8601 override; defaults to today" >&2 + echo " in UTC. Pass it only for tests and reproduction." >&2 exit 1 fi -started_at="$1" -run_id="$2" - -# Accept an ISO-8601 timestamp and take its UTC date. Everything after the date -# part is deliberately ignored: the run id already makes the directory unique. -if [[ ! "$started_at" =~ ^([0-9]{4})-([0-9]{2})-([0-9]{2})T[0-9]{2}:[0-9]{2}:[0-9]{2} ]]; then - echo "Error: invalid run start time: $started_at" >&2 - echo "Expected an ISO-8601 timestamp such as 2026-12-01T11:50:08Z" >&2 - exit 1 -fi -year="${BASH_REMATCH[1]}" -month="${BASH_REMATCH[2]}" -day="${BASH_REMATCH[3]}" +run_id="$1" +date_override="${2:-}" # The run id becomes a path component, so it must be digits only. if [[ ! "$run_id" =~ ^[0-9]+$ ]]; then @@ -35,4 +29,34 @@ if [[ ! "$run_id" =~ ^[0-9]+$ ]]; then exit 1 fi +if [[ -n "$date_override" ]]; then + # Accept a bare date or a full ISO-8601 timestamp and take its date part. + if [[ ! "$date_override" =~ ^([0-9]{4})-([0-9]{2})-([0-9]{2})(T[0-9]{2}:[0-9]{2}:[0-9]{2}.*)?$ ]]; then + echo "Error: invalid date: $date_override" >&2 + echo "Expected YYYY-MM-DD or an ISO-8601 timestamp such as 2026-12-01T11:50:08Z" >&2 + exit 1 + fi + year="${BASH_REMATCH[1]}" + month="${BASH_REMATCH[2]}" + day="${BASH_REMATCH[3]}" + + # The pattern above only checks the shape; reject impossible dates too so a + # typo cannot create a directory like 2026-13-99 in a public repository. + if [[ "$month" -lt 1 || "$month" -gt 12 || "$day" -lt 1 || "$day" -gt 31 ]]; then + echo "Error: invalid date: $date_override" >&2 + echo "Expected a real calendar date such as 2026-12-01" >&2 + exit 1 + fi + # Normalise to a canonical form; 2026-02-31 must not be accepted. + if [[ "$(date -u -d "$year-$month-$day" '+%Y-%m-%d' 2>/dev/null)" != "$year-$month-$day" ]]; then + echo "Error: invalid date: $date_override" >&2 + echo "Expected a real calendar date such as 2026-12-01" >&2 + exit 1 + fi +else + year="$(date -u '+%Y')" + month="$(date -u '+%m')" + day="$(date -u '+%d')" +fi + printf '%s-%s-%s-%s\n' "$year" "$month" "$day" "$run_id"