diff --git a/.github/workflows/publish-apt.yml b/.github/workflows/publish-apt.yml index 0633446..d566e71 100644 --- a/.github/workflows/publish-apt.yml +++ b/.github/workflows/publish-apt.yml @@ -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" diff --git a/.github/workflows/script-checks.yml b/.github/workflows/script-checks.yml index b6b94ae..8da14d5 100644 --- a/.github/workflows/script-checks.yml +++ b/.github/workflows/script-checks.yml @@ -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. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index c7aede4..60d1f34 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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 diff --git a/README-zh-cn.md b/README-zh-cn.md index 75b19b6..ae491d2 100644 --- a/README-zh-cn.md +++ b/README-zh-cn.md @@ -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 @@ -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),目录结构一致 +(`<日期>-/-.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(每行一个), diff --git a/README.md b/README.md index 6b2d4d1..3583b35 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 `-/-.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 diff --git a/scripts/github-app-token.sh b/scripts/github-app-token.sh new file mode 100755 index 0000000..bb9c6a5 --- /dev/null +++ b/scripts/github-app-token.sh @@ -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 " >&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" diff --git a/scripts/publish-log-release.sh b/scripts/publish-log-release.sh new file mode 100755 index 0000000..bc989f8 --- /dev/null +++ b/scripts/publish-log-release.sh @@ -0,0 +1,139 @@ +#!/usr/bin/env bash +# Create (or update) the GitHub release for one archived build-log run and attach +# that run's log files as release assets. +# +# Releases are idempotent: re-running a publish for the same run updates the +# existing release and replaces its assets instead of failing, so a retried +# workflow does not error out. +set -euo pipefail + +if [[ $# -ne 3 ]]; then + echo "Usage: publish-log-release.sh " >&2 + echo " log-repo owner/name of the log repository" >&2 + echo " run-dir the archived run directory, e.g. 2026-12-01-46640252167" >&2 + echo " token GitHub App installation token" >&2 + exit 1 +fi + +repo="$1" +run_dir="$2" +token="$3" + +if [[ ! "$repo" =~ ^[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+$ ]]; then + echo "Error: invalid repository slug: $repo" >&2 + exit 1 +fi +if [[ ! "$run_dir" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]+$ ]]; then + echo "Error: invalid run directory: $run_dir" >&2 + exit 1 +fi +if [[ -z "$token" ]]; then + echo "Error: empty token" >&2 + exit 1 +fi + +script_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)" +repo_dir="$(git -C "$script_dir" rev-parse --show-toplevel)" +run_path="$repo_dir/$run_dir" + +if [[ ! -d "$run_path" ]]; then + echo "Error: run directory not found: $run_path" >&2 + exit 1 +fi + +api="https://api.github.com/repos/$repo" +tag="$run_dir" +title="Build logs $run_dir" +run_id="${run_dir##*-}" +body="Build logs of the ZGS publish run archived in \`$run_dir/\`. + +- Build run: https://github.com/zitzhen/ZGS/actions/runs/$run_id +- Browsable copy: https://apt-general.zitzhen.cn/log/$run_dir/" + +# The boolean fields must be real JSON booleans; "False" as a string is rejected. +create_payload="$(TAG="$tag" TITLE="$title" BODY="$body" python3 -c ' +import json, os +print(json.dumps({ + "tag_name": os.environ["TAG"], + "name": os.environ["TITLE"], + "body": os.environ["BODY"], + "draft": False, + "prerelease": False, + "generate_release_notes": False, +})) +')" + +update_payload="$(TITLE="$title" BODY="$body" python3 -c ' +import json, os +print(json.dumps({ + "name": os.environ["TITLE"], + "body": os.environ["BODY"], + "draft": False, + "prerelease": False, +})) +')" + +auth=(-H "Authorization: Bearer $token" -H 'Accept: application/vnd.github+json') + +existing_id="$(curl -fsS "${auth[@]}" "$api/releases/tags/$tag" 2>/dev/null \ + | python3 -c 'import json,sys; print(json.load(sys.stdin).get("id",""))' 2>/dev/null || true)" + +if [[ -n "$existing_id" ]]; then + echo "Updating the existing release $tag (id $existing_id)" >&2 + release_id="$existing_id" + curl -fsS -X PATCH "${auth[@]}" -H 'Content-Type: application/json' \ + --data "$update_payload" "$api/releases/$release_id" >/dev/null +else + echo "Creating release $tag" >&2 + release_id="$(curl -fsS -X POST "${auth[@]}" \ + -H 'Content-Type: application/json' --data "$create_payload" "$api/releases" \ + | python3 -c 'import json,sys; print(json.load(sys.stdin).get("id",""))')" +fi + +if [[ -z "$release_id" ]]; then + echo "Error: could not create or find the release for $tag" >&2 + exit 1 +fi + +shopt -s nullglob +assets=("$run_path"/*.log) +shopt -u nullglob + +if (( ${#assets[@]} == 0 )); then + echo "Warning: no .log files in $run_path; the release has no assets" >&2 + exit 0 +fi + +# Snapshot the existing assets once, so a retry replaces instead of duplicating. +declare -A asset_ids=() +while IFS=$'\t' read -r asset_name asset_id; do + [[ -n "$asset_name" ]] && asset_ids["$asset_name"]="$asset_id" +done < <( + curl -fsS "${auth[@]}" "$api/releases/$release_id/assets?per_page=100" \ + | python3 -c ' +import json, sys +for asset in json.load(sys.stdin): + print("%s\t%s" % (asset["name"], asset["id"])) +' +) + +uploaded=0 +for asset in "${assets[@]}"; do + asset_name="$(basename -- "$asset")" + + if [[ -n "${asset_ids[$asset_name]:-}" ]]; then + echo "Replacing the existing asset $asset_name" >&2 + curl -fsS -X DELETE "${auth[@]}" \ + "https://api.github.com/repos/$repo/releases/assets/${asset_ids[$asset_name]}" >/dev/null + fi + + curl -fsS -X POST -H "Authorization: Bearer $token" \ + -H 'Accept: application/vnd.github+json' \ + -H 'Content-Type: text/plain' \ + --data-binary "@$asset" \ + "https://uploads.github.com/repos/$repo/releases/$release_id/assets?name=$asset_name" >/dev/null + uploaded=$((uploaded + 1)) + echo "Uploaded $asset_name" >&2 +done + +echo "Release $tag is live with $uploaded asset(s)" >&2 diff --git a/scripts/render-log-readme.sh b/scripts/render-log-readme.sh new file mode 100755 index 0000000..f84f6bf --- /dev/null +++ b/scripts/render-log-readme.sh @@ -0,0 +1,64 @@ +#!/usr/bin/env bash +# Rebuild the README index of the log repository. +# +# The repository is browsed as plain directories, so the README is the only index +# a reader sees. It is regenerated from the run directories actually present, so +# adding a new run never drops an older one from the list. +set -euo pipefail + +if [[ $# -ne 2 ]]; then + echo "Usage: render-log-readme.sh " >&2 + echo " repository-slug e.g. zitzhen/ZGS-build-log" >&2 + exit 1 +fi + +repo_root="$1" +slug="$2" + +if [[ ! -d "$repo_root/.git" ]]; then + echo "Error: $repo_root is not a git clone" >&2 + exit 1 +fi + +readme="$repo_root/README.md" +base_url="https://github.com/$slug" + +# Collect the run directories (the - shape only), newest first. +mapfile -t run_dirs < <( + find "$repo_root" -mindepth 1 -maxdepth 1 -type d \ + -regextype posix-extended -regex '.*/[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]+' \ + -printf '%f\n' 2>/dev/null | sort -r +) + +{ + printf '# ZGS build logs\n\n' + printf 'Build logs of every publish run of [ZGS](https://github.com/zitzhen/ZGS),\n' + printf 'the ZIT General Software Source apt repository published at\n' + printf '.\n\n' + printf 'Each directory is one publish run, named `-` in UTC, and\n' + printf 'contains one `-.log` per package built in that run. Nothing\n' + printf 'is deleted, so older runs stay available.\n\n' + printf 'The same logs are also served over HTTPS under the apt repository at\n' + printf '.\n\n' + printf '## Run index\n\n' + + if (( ${#run_dirs[@]} == 0 )); then + printf '_No publish runs have been archived yet._\n' + else + printf '| Run | Date (UTC) | Packages | Release |\n' + printf '| --- | --- | ---: | --- |\n' + for run_dir in "${run_dirs[@]}"; do + run_date="${run_dir:0:10}" + # Count the log files in this run, tolerating a run directory with none. + count="$(find "$repo_root/$run_dir" -maxdepth 1 -type f -name '*.log' 2>/dev/null | wc -l)" + count="${count//[[:space:]]/}" + printf '| [`%s/`](%s) | %s | %s | [release](%s/releases/tag/%s) |\n' \ + "$run_dir" "$base_url/tree/main/$run_dir" \ + "$run_date" "$count" "$base_url" "$run_dir" + done + printf '\nEach release named after a run carries that run'"'"'s log files as\n' + printf 'release assets, so a run can be downloaded without cloning.\n' + fi +} >"$readme" + +echo "Wrote README index covering ${#run_dirs[@]} run(s)" >&2 diff --git a/scripts/stage-log-repo.sh b/scripts/stage-log-repo.sh new file mode 100755 index 0000000..23d3cd9 --- /dev/null +++ b/scripts/stage-log-repo.sh @@ -0,0 +1,64 @@ +#!/usr/bin/env bash +# Stage one workflow run's build logs into a clone of the log repository. +# +# The log repository keeps one directory per publish run at its root: +# -/-.log +# so every run is kept and nothing is overwritten. +set -euo pipefail + +if [[ $# -ne 3 ]]; then + echo "Usage: stage-log-repo.sh " >&2 + echo " work-root checked-out clone of the log repository" >&2 + echo " logs-dir directory holding the downloaded build-log artifacts" >&2 + echo " run-dir per-run directory name, e.g. 2026-12-01-46640252167" >&2 + exit 1 +fi + +work_root="$1" +logs_dir="$2" +run_dir="$3" + +if [[ ! -d "$work_root" ]]; then + echo "Error: log repository clone not found: $work_root" >&2 + exit 1 +fi + +# The run directory is a path component inside a git repository that is published +# publicly, so only a plain - shape is accepted. +if [[ ! "$run_dir" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]+$ ]]; then + echo "Error: invalid run directory name: $run_dir" >&2 + echo "Expected -, e.g. 2026-12-01-46640252167" >&2 + exit 1 +fi + +target_dir="$work_root/$run_dir" +mkdir -p "$target_dir" + +if [[ ! -d "$logs_dir" ]]; then + echo "Warning: no build logs found at $logs_dir; committing an empty $run_dir/" >&2 + exit 0 +fi + +shopt -s nullglob +logs=("$logs_dir"/*.log "$logs_dir"/*/*.log) +shopt -u nullglob + +if (( ${#logs[@]} == 0 )); then + echo "Warning: no .log files under $logs_dir; committing an empty $run_dir/" >&2 + exit 0 +fi + +copied=0 +for log in "${logs[@]}"; do + name="$(basename -- "$log")" + # Keep names flat and safe: they come from the upstream URL and end up in a + # public git repository. + name="${name//[^A-Za-z0-9._-]/_}" + case "$name" in + .|..|'') echo "Warning: skipping unusable log name for $log" >&2; continue ;; + esac + cp -f "$log" "$target_dir/$name" + copied=$((copied + 1)) +done + +echo "Staged $copied build log(s) into $target_dir" >&2