commit bcb77261f42223f378ee0bfe5e0233f61757d32c Author: Gergo Date: Thu Nov 19 13:13:36 2026 +0100 AnnoBase release download step for product workflows Co-Authored-By: Claude Opus 5.5 diff --git a/README.md b/README.md new file mode 100644 index 0000000..5d1c1de --- /dev/null +++ b/README.md @@ -0,0 +1,16 @@ +# annobase-fetch + +Forgejo Actions step that puts the AnnoBase release named in the app settings next to the app. + +```yaml +- uses: https://git.annodev.software/annodev/annobase-fetch@v1 + with: + settings: Product/backend/Product.Api/appsettings.json + token: ${{ secrets.ANNOBASE_TOKEN }} +``` + +- `settings`: JSON file of the app, relative to the workspace. It holds `"AnnoBase": { "Version": "1.0.4" }` (backend `appsettings.json`) or `"annobase": { "version": "1.0.4" }` with `key: annobase.version` (frontend `package.json`). +- `token`: the organisation secret `ANNOBASE_TOKEN` (read-only token of the `annobase-ci` user for `annodev/anno-base`). +- `destination`: default `/AnnoBase`, which is where the `AnnoBaseBackendPath` and frontend `link:` paths of the products point. + +The step reads release `v` of `annodev/anno-base`, downloads `manifest.json` and the archive it names, checks the sha256 and unpacks the archive. AnnoBase itself stays private; this repository holds only the download step. diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..80e7e96 --- /dev/null +++ b/action.yml @@ -0,0 +1,31 @@ +name: AnnoBase +description: Downloads the AnnoBase release that the app settings name and unpacks it next to the app +inputs: + settings: + description: JSON settings file of the app, relative to the workspace + required: true + key: + description: Dot path of the AnnoBase version in that file + required: false + default: AnnoBase.Version + token: + description: Token that can read annodev/anno-base + required: true + destination: + description: Folder that receives AnnoBase; empty means AnnoBase in the workspace + required: false + default: "" +runs: + using: composite + steps: + - shell: bash + env: + ANNOBASE_TOKEN: ${{ inputs.token }} + ANNOBASE_SETTINGS: ${{ inputs.settings }} + ANNOBASE_KEY: ${{ inputs.key }} + ANNOBASE_DESTINATION: ${{ inputs.destination }} + run: | + set -euo pipefail + args=(--settings "$ANNOBASE_SETTINGS" --key "$ANNOBASE_KEY") + if [ -n "$ANNOBASE_DESTINATION" ]; then args+=(--destination "$ANNOBASE_DESTINATION"); fi + python3 "${{ github.action_path }}/fetch.py" "${args[@]}" diff --git a/fetch.py b/fetch.py new file mode 100644 index 0000000..6870f29 --- /dev/null +++ b/fetch.py @@ -0,0 +1,97 @@ +import argparse +import hashlib +import json +import os +import sys +import tarfile +import tempfile +import time +import urllib.parse +import urllib.request +from pathlib import Path + +SERVER = "https://git.annodev.software" +REPOSITORY = "annodev/anno-base" +MARKER = ".annobase-version" + + +def setting(path, key): + value = json.loads(Path(path).read_text(encoding="utf-8-sig")) + for part in key.split("."): + value = value[part] + text = str(value).strip() + if not text: + raise ValueError(f"{path} has an empty {key}") + return text + + +def open_url(url, token): + if urllib.parse.urlparse(url).netloc != urllib.parse.urlparse(SERVER).netloc: + raise ValueError(f"{url} is not on {SERVER}") + request = urllib.request.Request(url, headers={"Authorization": "token " + token}) + return urllib.request.urlopen(request, timeout=900) + + +def release_assets(version, token): + with open_url(f"{SERVER}/api/v1/repos/{REPOSITORY}/releases/tags/v{version}", token) as response: + release = json.load(response) + if release.get("draft"): + raise ValueError(f"AnnoBase v{version} is a draft release") + return {asset["name"]: asset["browser_download_url"] for asset in release["assets"]} + + +def download(url, token, target): + digest = hashlib.sha256() + with open_url(url, token) as response, target.open("wb") as stream: + while chunk := response.read(1024 * 1024): + stream.write(chunk) + digest.update(chunk) + return digest.hexdigest() + + +def extract(archive, destination): + with tarfile.open(archive) as tar: + if hasattr(tarfile, "data_filter"): + tar.extractall(destination, filter="data") + else: + tar.extractall(destination) + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("--settings", required=True) + parser.add_argument("--key", default="AnnoBase.Version") + parser.add_argument("--destination", default=os.path.join(os.environ.get("GITHUB_WORKSPACE", "."), "AnnoBase")) + args = parser.parse_args() + token = os.environ.get("ANNOBASE_TOKEN", "").strip() + if not token: + sys.exit("ANNOBASE_TOKEN is empty") + version = setting(args.settings, args.key) + destination = Path(args.destination) + marker = destination / MARKER + if marker.is_file() and marker.read_text().strip() == version: + print(f"AnnoBase {version} is already in {destination}") + return + if destination.exists(): + if not marker.is_file(): + sys.exit(f"{destination} exists and was not made by this step") + destination.rename(destination.with_name(f"{destination.name}.replaced.{int(time.time())}")) + assets = release_assets(version, token) + with tempfile.TemporaryDirectory(prefix="annobase-") as temp: + temp = Path(temp) + manifest_path = temp / "manifest.json" + download(assets["manifest.json"], token, manifest_path) + manifest = json.loads(manifest_path.read_text()) + if manifest["version"] != version: + sys.exit(f"The manifest of v{version} names version {manifest['version']}") + archive = temp / manifest["file"] + if download(assets[manifest["file"]], token, archive) != manifest["sha256"]: + sys.exit(f"{manifest['file']} does not match the sha256 of its manifest") + destination.mkdir(parents=True) + extract(archive, destination) + marker.write_text(version + "\n") + print(f"AnnoBase {version} ({manifest['commit']}) is in {destination}") + + +if __name__ == "__main__": + main()