Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions .github/workflows/publish-apt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
58 changes: 54 additions & 4 deletions .github/workflows/script-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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)" = \
Expand All @@ -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/<date>-<run-id>/, be listed from /log/ and stay
Expand Down
7 changes: 4 additions & 3 deletions README-zh-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

- 运行目录名为 `<日期>-<runID>`(如 `2026-12-01-46640252167`),由工作流的开始日期
与数字运行 ID 通过 `scripts/log-run-dir.sh` 推导。每次运行都有独立目录,不会覆盖
上一次。
- 运行目录名为 `<日期>-<runID>`(如 `2026-12-01-46640252167`),由当前 UTC 日期
与数字运行 ID 通过 `scripts/log-run-dir.sh` 生成。日期由脚本内部读取系统时钟,
而不是从工作流传入——取自不存在的 `github` 上下文字段的时间戳会被静默替换为空
字符串。每次运行都有独立目录,不会覆盖上一次。
- 目录内的文件名规则为 `<owner>-<repo>.log`,由 `Software.list` 中的 URL 推导
(`https://github.com/zitzhen/patchsplit` → `zitzhen-patchsplit.log`)。
- 日志开头记录上游地址、开始时间、runner 与 commit,结尾记录结束时间与构建退出码,
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<date>-<run-id>` (`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 `<date>-<run-id>` (`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 `<owner>-<repo>.log`, derived from the
`Software.list` URL (`https://github.com/zitzhen/patchsplit` →
`zitzhen-patchsplit.log`).
Expand Down
68 changes: 46 additions & 22 deletions scripts/log-run-dir.sh
Original file line number Diff line number Diff line change
@@ -1,38 +1,62 @@
#!/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/<run-dir>/<owner>-<repo>.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 <run-started-at> <run-id>" >&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 <run-id> [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
echo "Error: invalid workflow run id: $run_id" >&2
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"
Loading