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
89 changes: 89 additions & 0 deletions .github/workflows/publish-apt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,92 @@ jobs:
--cache-control 'no-cache, max-age=60'

echo "Repository published to $APT_REPO_URL"

publish-log-repo:
name: Mirror build logs to zitzhen/ZGS-build-log
# Archives the same logs into a dedicated repository as plain directories, one
# per run, and publishes a release named after the run with the log files as
# assets. It runs alongside the R2 publication: a failure here must not block
# the apt repository from being updated.
needs: build
runs-on: ubuntu-latest
env:
LOG_REPO: zitzhen/ZGS-build-log
APP_ID: '6008281'
APP_INSTALLATION_ID: '187856393'
steps:
- name: Check out repository
uses: actions/checkout@v4
with:
# Do not leave the default GITHUB_TOKEN in .git/config: the push below
# must use the App token, and a persisted credential helper would
# otherwise take precedence and push with the wrong identity.
persist-credentials: false

- name: Download build logs
continue-on-error: true
uses: actions/download-artifact@v4
with:
path: ${{ runner.temp }}/logs
pattern: build-log-*
merge-multiple: true

- name: Mint a GitHub App token
id: token
env:
APP_KEY: ${{ secrets.APP_KEY }}
run: |
token="$(./scripts/github-app-token.sh "$APP_ID" "$APP_INSTALLATION_ID")"
# Never echo the token; mask it so an incidental log line cannot leak it.
echo "::add-mask::$token"
echo "token=$token" >> "$GITHUB_OUTPUT"

- name: Check out the log repository
env:
TOKEN: ${{ steps.token.outputs.token }}
run: |
# The App token authenticates the push; the default GITHUB_TOKEN can
# only write to this repository, not to the log repository.
git clone --quiet \
"https://x-access-token:${TOKEN}@github.com/${LOG_REPO}.git" \
"$RUNNER_TEMP/logrepo"
# Drop the token from the remote URL so it cannot leak from .git/config
# or be captured by git output later in the job.
git -C "$RUNNER_TEMP/logrepo" remote set-url origin \
"https://github.com/${LOG_REPO}.git"

- 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 }}")"
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"
echo "run_dir=$run_dir" >> "$GITHUB_OUTPUT"

- name: Commit and push to the log repository
env:
TOKEN: ${{ steps.token.outputs.token }}
run: |
cd "$RUNNER_TEMP/logrepo"
git config user.name 'zgs-software[bot]'
git config user.email '363535165+zgs-software[bot]@users.noreply.github.com'

git add -A
if git diff --cached --quiet; then
echo "Nothing to commit: this run's logs are already archived"
else
git commit --quiet -m "logs: archive build logs for ${{ steps.stage.outputs.run_dir }}"
# Push with the App token supplied as an argument, not via a stored
# credential, so it never lands in .git/config.
git push --quiet \
"https://x-access-token:${TOKEN}@github.com/${LOG_REPO}.git" HEAD:main
echo "Pushed to $LOG_REPO main"
fi

- name: Publish the release for this run
env:
TOKEN: ${{ steps.token.outputs.token }}
run: |
./scripts/publish-log-release.sh \
"$LOG_REPO" "${{ steps.stage.outputs.run_dir }}" "$TOKEN"
41 changes: 41 additions & 0 deletions .github/workflows/script-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,47 @@ jobs:
grep -q '2026-11-29-46000000000/' "$tmp/logindex/index.html"
grep -q '2026-12-01-46640252167/' "$tmp/logindex/index.html"

- name: Test the log repository staging and index
# The log repository is a public, append-only archive: a new run must add
# a directory without dropping older ones from the README index.
run: |
tmp="$(mktemp -d)"
git init -q "$tmp/logrepo"
printf '# seed\n' > "$tmp/logrepo/README.md"

mkdir -p "$tmp/logs"
printf 'run a\n' > "$tmp/logs/zitzhen-patchsplit.log"
./scripts/stage-log-repo.sh "$tmp/logrepo" "$tmp/logs" 2026-12-01-46640252167
test -f "$tmp/logrepo/2026-12-01-46640252167/zitzhen-patchsplit.log"

mkdir -p "$tmp/logs2"
printf 'run b\n' > "$tmp/logs2/zitzhen-GitTune.log"
./scripts/stage-log-repo.sh "$tmp/logrepo" "$tmp/logs2" 2026-12-02-47000000000

./scripts/render-log-readme.sh "$tmp/logrepo" zitzhen/ZGS-build-log
# The index must list both runs, newest first.
grep -q '2026-12-02-47000000000' "$tmp/logrepo/README.md"
grep -q '2026-12-01-46640252167' "$tmp/logrepo/README.md"
head -n 1 <(grep -o '2026-[0-9-]*' "$tmp/logrepo/README.md" | sort -u -r) \
| grep -q '2026-12-02-47000000000'

# Path traversal and malformed run directories must be rejected.
! ./scripts/stage-log-repo.sh "$tmp/logrepo" "$tmp/logs" ../escape 2>/dev/null
! ./scripts/stage-log-repo.sh "$tmp/logrepo" "$tmp/logs" latest 2>/dev/null

- name: Test the GitHub App token guard
# The token script must fail closed rather than emit an empty token.
run: |
! env -u APP_KEY ./scripts/github-app-token.sh 6008281 187856393 2>/dev/null
! APP_KEY=x ./scripts/github-app-token.sh not-a-number 187856393 2>/dev/null
! APP_KEY=x ./scripts/github-app-token.sh 6008281 not-a-number 2>/dev/null

- name: Test the release script guards
run: |
! ./scripts/publish-log-release.sh 'bad/slug/here' 2026-12-01-1 tok 2>/dev/null
! ./scripts/publish-log-release.sh owner/repo not-a-run-dir tok 2>/dev/null
! ./scripts/publish-log-release.sh owner/repo 2026-12-01-1 '' 2>/dev/null

- name: Test build-deb.sh exit statuses
# A cleanup trap that ends on a failing test used to turn successful
# builds into exit 1, which skipped publish on every run.
Expand Down
13 changes: 13 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,19 @@ directory present in the bucket, so older runs stay browsable after a new publis
adds another directory. The sync never passes `--delete`; pruning old runs is a
manual decision, so do not assume logs disappear on their own.

The same logs are mirrored into `zitzhen/ZGS-build-log` by the `publish-log-repo`
job: `scripts/stage-log-repo.sh` adds the run directory,
`scripts/render-log-readme.sh` rebuilds that repository's README index, and
`scripts/publish-log-release.sh` publishes a release named after the run with the
log files as assets. Commits are attributed to `zgs-software[bot]` and pushed
straight to `main`.

Authentication comes from a GitHub App, not a personal access token. The app id
and installation id are set in the workflow, and the private key is the `APP_KEY`
secret, read only by `scripts/github-app-token.sh`. The token it mints is masked
in the logs and never written to `.git/config`. Keep it that way: do not replace
this with a long-lived token, and do not commit a key or a token.

`scripts/publish-repo.sh` also calls `scripts/generate-index.sh`, which walks the
staged tree and writes one `index.html` per directory. It only needs Bash and the
ordinary coreutils (`find`, `sort`, `date`), so you can exercise it against any
Expand Down
19 changes: 19 additions & 0 deletions README-zh-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ ZGS/
│ ├── log-file-name.sh # 由仓库 URL 推导发布用的日志文件名
│ ├── log-run-dir.sh # 推导单次运行的归档目录名
│ ├── stage-log-index.sh # 汇总所有运行目录重建 /log/ 索引
│ ├── stage-log-repo.sh # 把单次运行的日志放进日志仓库
│ ├── render-log-readme.sh # 重建日志仓库 README 索引
│ ├── publish-log-release.sh # 创建带日志附件的发行版
│ ├── github-app-token.sh # 生成 GitHub App 安装令牌
│ ├── generate-index.sh # 为仓库目录树生成可浏览的 index.html
│ └── test-workflow-build-step.sh # 在 bash 下执行 workflow 的 run 块
├── .github/workflows/ # 构建检查与发布 CI
Expand Down Expand Up @@ -129,6 +133,21 @@ https://apt-general.zitzhen.cn/log/2026-12-01-46640252167/zitzhen-patchsplit.log
- 日志是公开内容,并以 `Cache-Control: no-cache` 提供。切勿让构建把密钥、令牌等
敏感信息打进日志——工作流只会发布构建自己写出的内容。

### 日志仓库镜像

每次发布还会把相同的日志镜像到
[zitzhen/ZGS-build-log](https://github.com/zitzhen/ZGS-build-log),目录结构一致
(`<日期>-<runID>/<owner>-<repo>.log`):

- 该仓库的 README 在每次发布时重建,内含全部已归档运行的索引(按时间倒序),
并链接到每次运行的目录与发行版。
- 会创建以运行目录命名的发行版(如 `2026-12-01-46640252167`),把该次运行的
日志文件作为发行版附件上传,无需 clone 即可下载。发行版直接发布,不标记草稿。
- 提交者固定为 `zgs-software[bot]`
(`363535165+zgs-software[bot]@users.noreply.github.com`),直接提交到 `main`。
- 认证使用 `scripts/github-app-token.sh` 由 `APP_KEY` secret 生成的 GitHub App
安装令牌,不涉及长期个人令牌。该 job 与 R2 发布并行,即使失败也不影响 apt 发布。

## 添加软件

在 `Software.list` 中追加一个包含 `debian/` 目录的 GitHub 仓库 URL(每行一个),
Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ ZGS/
│ ├── log-file-name.sh # Derives the published log name from a repo URL
│ ├── log-run-dir.sh # Derives the per-run archive directory name
│ ├── stage-log-index.sh # Rebuilds /log/ from all run directories
│ ├── stage-log-repo.sh # Stages a run's logs into the log repository
│ ├── render-log-readme.sh # Rebuilds the log repository README index
│ ├── publish-log-release.sh # Publishes a release with the run's logs
│ ├── github-app-token.sh # Mints a GitHub App installation token
│ ├── generate-index.sh # Writes browsable index.html files for the tree
│ └── test-workflow-build-step.sh # Runs a workflow run: block under bash
├── .github/workflows/ # Build-check and publish CI
Expand Down Expand Up @@ -150,6 +154,24 @@ https://apt-general.zitzhen.cn/log/2026-12-01-46640252167/zitzhen-patchsplit.log
print secrets (tokens, credentials) into them; the workflow only publishes
what the build itself wrote.

### Mirror in the log repository

Every publish also mirrors the same logs into
[zitzhen/ZGS-build-log](https://github.com/zitzhen/ZGS-build-log), which uses the
same `<date>-<run-id>/<owner>-<repo>.log` layout:

- The README there is regenerated on every publish and carries an index of all
archived runs, newest first, linking each run's directory and release.
- A release named after the run (`2026-12-01-46640252167`) is created with the
run's log files attached as assets, so a run can be downloaded without
cloning. Releases are published directly, not as drafts.
- Commits are made on `main` as `zgs-software[bot]`
(`363535165+zgs-software[bot]@users.noreply.github.com`).
- Authentication uses a GitHub App installation token minted by
`scripts/github-app-token.sh` from the `APP_KEY` secret, so no long-lived
personal token is involved. The job runs alongside the R2 publication, so a
failure there never blocks an apt release.

## Adding software

Append a GitHub repository URL (containing a `debian/` directory) to
Expand Down
73 changes: 73 additions & 0 deletions scripts/github-app-token.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/usr/bin/env bash
# Mint a GitHub App installation token and print it on stdout.
#
# The token is scoped to the installations of the app and expires within an hour,
# which is what the log repository push and release steps need. The private key and
# the resulting token are never written to disk or echoed.
set -euo pipefail

if [[ $# -ne 2 ]]; then
echo "Usage: github-app-token.sh <app-id> <installation-id>" >&2
echo " The private key is read from the APP_KEY environment variable." >&2
exit 1
fi

app_id="$1"
installation_id="$2"

if [[ -z "${APP_KEY:-}" ]]; then
echo "Error: APP_KEY is not set" >&2
exit 1
fi

if [[ ! "$app_id" =~ ^[0-9]+$ ]]; then
echo "Error: invalid app id: $app_id" >&2
exit 1
fi
if [[ ! "$installation_id" =~ ^[0-9]+$ ]]; then
echo "Error: invalid installation id: $installation_id" >&2
exit 1
fi

tmp_dir="$(mktemp -d)"
trap 'rm -rf "$tmp_dir"' EXIT

key_file="$tmp_dir/app.pem"
# APP_KEY may hold the PEM with normalized newlines or escaped \n sequences,
# depending on how the secret was pasted.
printf '%s\n' "$APP_KEY" >"$key_file"
if ! grep -q '^-----BEGIN' "$key_file"; then
printf '%b\n' "$APP_KEY" >"$key_file"
fi
chmod 600 "$key_file"

b64url() {
# base64url without padding, as required by the JWT spec.
openssl base64 -A | tr '+/' '-_' | tr -d '='
}

now="$(date +%s)"
# GitHub rejects a JWT whose iat is in the future or older than 10 minutes.
iat="$((now - 60))"
exp="$((now + 540))"

header="$(printf '{"alg":"RS256","typ":"JWT"}' | b64url)"
payload="$(printf '{"iat":%s,"exp":%s,"iss":"%s"}' "$iat" "$exp" "$app_id" | b64url)"
signature="$(printf '%s.%s' "$header" "$payload" \
| openssl dgst -sha256 -sign "$key_file" -binary | b64url)"
jwt="$header.$payload.$signature"

response="$(curl -fsS -X POST \
-H 'Accept: application/vnd.github+json' \
-H "Authorization: Bearer $jwt" \
"https://api.github.com/app/installations/$installation_id/access_tokens")"

token="$(printf '%s' "$response" \
| python3 -c 'import json,sys; print(json.load(sys.stdin).get("token",""))')"

if [[ -z "$token" ]]; then
echo "Error: the installation token response did not contain a token" >&2
exit 1
fi

printf '%s\n' "$token"
Loading
Loading