diff --git a/.github/workflows/build-kernel.yml b/.github/workflows/build-kernel.yml index b3fcd7f..d732a04 100644 --- a/.github/workflows/build-kernel.yml +++ b/.github/workflows/build-kernel.yml @@ -285,6 +285,9 @@ jobs: needs: resolve if: ${{ inputs.use_susfs }} runs-on: ubuntu-latest + permissions: + contents: read + actions: read # gh run download of this run's snapshots (kernel/scripts/fetch_snapshots.sh) env: GH_TOKEN: ${{ github.token }} steps: @@ -294,11 +297,14 @@ jobs: sparse-checkout: | kernel/susfs_sidex.py kernel/susfs_hybrid.py + kernel/scripts/fetch_snapshots.sh sparse-checkout-cone-mode: false - - uses: actions/download-artifact@v8.0.2 - with: - name: snapshots-${{ github.run_number }} - path: snapshots + # Not actions/download-artifact: it cannot retry, and a just-uploaded artifact can be invisible for a + # few seconds (run 71: 8 jobs "Artifact not found"). The script retries through gh. + - name: Ambil snapshot / fetch snapshots + env: + GH_TOKEN: ${{ github.token }} + run: bash kernel/scripts/fetch_snapshots.sh - name: Ambil sidex15 / fetch sidex15's run: python3 kernel/susfs_sidex.py --snapshots snapshots --out out - name: Upload 5 - SUSFS module (sidex15) @@ -328,6 +334,9 @@ jobs: name: build (${{ matrix.name }}) needs: resolve runs-on: ubuntu-latest + permissions: + contents: read + actions: read # gh run download of this run's snapshots (kernel/scripts/fetch_snapshots.sh) strategy: fail-fast: false matrix: ${{ fromJson(needs.resolve.outputs.matrix) }} @@ -336,10 +345,12 @@ jobs: with: persist-credentials: false - - uses: actions/download-artifact@v8.0.2 - with: - name: snapshots-${{ github.run_number }} - path: snapshots + # Not actions/download-artifact: it cannot retry, and a just-uploaded artifact can be invisible for a + # few seconds (run 71: 8 jobs "Artifact not found"). The script retries through gh. + - name: Ambil snapshot / fetch snapshots + env: + GH_TOKEN: ${{ github.token }} + run: bash kernel/scripts/fetch_snapshots.sh - name: Siapkan runner / prepare the runner run: | diff --git a/kernel/scripts/fetch_snapshots.sh b/kernel/scripts/fetch_snapshots.sh new file mode 100755 index 0000000..ce650b2 --- /dev/null +++ b/kernel/scripts/fetch_snapshots.sh @@ -0,0 +1,21 @@ +#!/usr/bin/env bash +# Download this run's snapshots- artifact into ./snapshots, retrying while GitHub has not +# indexed it yet. Builder run 71 (22 Nov 2026): the resolve job uploaded snapshots-71 at 03:50:23 UTC, +# 8 jobs that started 4-9 seconds later got "Artifact not found" from actions/download-artifact, which +# cannot retry, while jobs that started a few seconds later downloaded the same artifact. Needs GH_TOKEN +# with actions: read. Waits up to TRIES x WAIT seconds (default 12 x 10 s), then fails the step. +set -uo pipefail +name="snapshots-${GITHUB_RUN_NUMBER:?}" +tries="${SNAPSHOT_TRIES:-12}" +wait="${SNAPSHOT_WAIT:-10}" +for ((i = 1; i <= tries; i++)); do + rm -rf snapshots + if gh run download "${GITHUB_RUN_ID:?}" --repo "${GITHUB_REPOSITORY:?}" --name "$name" --dir snapshots; then + ls snapshots + exit 0 + fi + echo "$name belum terlihat / not visible yet ($i/$tries); coba lagi $wait detik / retry in $wait s" + sleep "$wait" +done +echo "::error::$name tidak bisa diunduh setelah $tries kali / could not be downloaded after $tries tries" +exit 1 diff --git a/tests/test_fetch_snapshots.py b/tests/test_fetch_snapshots.py new file mode 100644 index 0000000..604f3b8 --- /dev/null +++ b/tests/test_fetch_snapshots.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python3 +"""kernel/scripts/fetch_snapshots.sh retries while a just-uploaded snapshots artifact is not visible yet +(Builder run 71: 8 jobs got "Artifact not found" from actions/download-artifact a few seconds after upload).""" +import os +import subprocess +import tempfile +import unittest +from pathlib import Path + +try: + import yaml +except ImportError: # Validate installs PyYAML for its YAML step; locally it may be missing + yaml = None + +ROOT = Path(__file__).resolve().parents[1] +SCRIPT = ROOT / "kernel/scripts/fetch_snapshots.sh" +# Fake gh: fails until the call count reaches $FAIL_UNTIL, then writes the artifact like gh run download. +FAKE_GH = """#!/usr/bin/env bash +n=$(( $(cat "$CALLS" 2>/dev/null || echo 0) + 1 )); echo "$n" > "$CALLS" +echo "$*" >> "$ARGS" +[ "$n" -ge "$FAIL_UNTIL" ] || { echo "no valid artifacts found to download" >&2; exit 1; } +while [ $# -gt 0 ]; do [ "$1" = --dir ] && dir=$2; shift; done +mkdir -p "$dir" && echo '{"root": "KSU"}' > "$dir/KSU.json" +""" + + +class FetchSnapshotsTest(unittest.TestCase): + def run_script(self, fail_until, tries=4): + tmp = Path(tempfile.mkdtemp()) + (tmp / "bin").mkdir() + gh = tmp / "bin/gh" + gh.write_text(FAKE_GH) + gh.chmod(0o755) + env = dict(os.environ, PATH=f"{tmp / 'bin'}:{os.environ['PATH']}", CALLS=str(tmp / "calls"), + ARGS=str(tmp / "args"), FAIL_UNTIL=str(fail_until), SNAPSHOT_TRIES=str(tries), SNAPSHOT_WAIT="0", + GITHUB_RUN_NUMBER="71", GITHUB_RUN_ID="44981877723", GITHUB_REPOSITORY="cogan17/Cogan-Kernel") + result = subprocess.run(["bash", str(SCRIPT)], cwd=tmp, env=env, capture_output=True, text=True) + return tmp, result + + def test_retries_until_the_artifact_is_visible(self): + tmp, result = self.run_script(fail_until=3) + self.assertEqual(result.returncode, 0, result.stdout + result.stderr) + self.assertEqual((tmp / "calls").read_text().strip(), "3") + self.assertTrue((tmp / "snapshots/KSU.json").is_file()) + self.assertIn("run download 44981877723 --repo cogan17/Cogan-Kernel --name snapshots-71 --dir snapshots", + (tmp / "args").read_text()) + + def test_gives_up_after_the_last_try(self): + tmp, result = self.run_script(fail_until=99, tries=3) + self.assertNotEqual(result.returncode, 0) + self.assertEqual((tmp / "calls").read_text().strip(), "3") + self.assertIn("::error::snapshots-71", result.stdout) + + @unittest.skipUnless(yaml, "PyYAML") + def test_jobs_that_need_the_snapshots_fetch_them_with_retries(self): + jobs = yaml.safe_load((ROOT / ".github/workflows/build-kernel.yml").read_text())["jobs"] + for name in ("susfs", "build"): + job = jobs[name] + steps = job["steps"] + self.assertFalse([s for s in steps if "download-artifact" in str(s.get("uses")) + and "snapshots" in str((s.get("with") or {}).get("name"))], name) + self.assertTrue([s for s in steps if s.get("run") == "bash kernel/scripts/fetch_snapshots.sh"], name) + self.assertEqual(job["permissions"].get("actions"), "read", name) + sparse = jobs["susfs"]["steps"][0]["with"]["sparse-checkout"] + self.assertIn("kernel/scripts/fetch_snapshots.sh", sparse) + + +if __name__ == "__main__": + unittest.main()