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"