-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from MinBZK/feature/deploy-json
Adds a deploy script and workflow
- Loading branch information
Showing
2 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |