Skip to content

Commit

Permalink
Merge pull request #15 from MinBZK/feature/deploy-json
Browse files Browse the repository at this point in the history
Adds a deploy script and workflow
  • Loading branch information
anneschuth authored Jul 8, 2024
2 parents 56ae69b + 41322d1 commit 9ef04a4
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/deploy.yml
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 }}
49 changes: 49 additions & 0 deletions script/deploy
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)

0 comments on commit 9ef04a4

Please sign in to comment.