diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..a6e5f6b --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,35 @@ +name: Generate and Deploy Index + +on: + push: + branches: + - main + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: '3.12' + + - name: Generate index.json + run: ./script/deploy + + - name: Checkout deployment branch + run: | + git fetch + git checkout --orphan deployment + git reset --hard + git clean -fdx + git checkout main -- index.json + git add index.json + git commit -m 'Auto-generate index.json' + git push --force origin deployment + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/script/deploy b/script/deploy new file mode 100755 index 0000000..cffe602 --- /dev/null +++ b/script/deploy @@ -0,0 +1,49 @@ +#!/usr/bin/env python3 + +import json +import os + + +def get_file_size(file_path): + return os.path.getsize(file_path) + + +def generate_index(directory, output_file, base_url): + index = { + "type": "dir", + "size": 0, + "name": directory, + "path": directory, + "download_url": f"{base_url}/{output_file}", + "_links": { + "self": f"{base_url}/{output_file}", + }, + "entries": [], + } + + for root, dirs, files in os.walk(directory): + for file in files: + if file.endswith(".yaml"): + file_path = os.path.join(root, file) + relative_path = file_path.replace("\\", "/") + file_info = { + "type": "file", + "size": get_file_size(file_path), + "name": file, + "path": relative_path, + "download_url": f"{base_url}/{relative_path}", + "_links": { + "self": f"{base_url}/{relative_path}", + }, + } + index["entries"].append(file_info) + + with open(output_file, "w") as f: + json.dump(index, f, indent=4) + + +BASE_URL = "https://minbzk.github.io/instrument-registry" +DIRECTORY = "instruments" +OUTPUT_FILE = "index.json" + +generate_index(DIRECTORY, OUTPUT_FILE, BASE_URL)