From d337e8a2070a17b704a41296036d24062e09cbce Mon Sep 17 00:00:00 2001 From: Kumi Date: Mon, 1 Jul 2024 10:24:24 +0200 Subject: [PATCH] feat(ci): automate build and deploy for dev and main branches - Added a new GitHub Actions workflow for the development branch to build and deploy the static site. - Modified the existing workflow to trigger on the main branch instead of the static branch. - Refactored the main.py script for consistency in string quotation. The new workflow simplifies deployment processes during development, ensuring seamless and automated integration and testing. --- .forgejo/workflows/build-dev.yml | 49 +++++++++++++++++++++++++++++++ .forgejo/workflows/build.yml | 2 +- main.py | 50 +++++++++++++++++++------------- 3 files changed, 80 insertions(+), 21 deletions(-) create mode 100644 .forgejo/workflows/build-dev.yml diff --git a/.forgejo/workflows/build-dev.yml b/.forgejo/workflows/build-dev.yml new file mode 100644 index 0000000..b62d22c --- /dev/null +++ b/.forgejo/workflows/build-dev.yml @@ -0,0 +1,49 @@ +name: Build and Deploy Static Site + +on: + push: + branches: + - dev + +jobs: + build: + container: node:20-bookworm + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Install dependencies + run: | + apt update + apt install -y python3 python3-pip + python3 -m pip install -r requirements.txt --break-system-packages + + - name: Generate static site + run: python3 main.py + + - name: Deploy to pages branch + run: | + # Configure Git + git config --global user.name "Forgejo" + git config --global user.email "noreply@private.coffee" + + # Move generated static site files to a temporary location + mv build ../static_site_temp + + # Create a new orphan branch named 'pages' + git checkout --orphan pages-dev + + # Remove all files from the working directory + git rm -rf . + + # Move the static site files back to the working directory + mv ../static_site_temp/* ./ + mv ../static_site_temp/.* ./ 2>/dev/null || true + + # Add and commit the static site files + git add . + git commit -m "Deploy static site" + + # Force push to the 'pages' branch + git push origin pages --force diff --git a/.forgejo/workflows/build.yml b/.forgejo/workflows/build.yml index 80b2389..aca70bc 100644 --- a/.forgejo/workflows/build.yml +++ b/.forgejo/workflows/build.yml @@ -3,7 +3,7 @@ name: Build and Deploy Static Site on: push: branches: - - static + - main jobs: build: diff --git a/main.py b/main.py index 394b61b..cdb6e62 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,6 @@ from jinja2 import Environment, FileSystemLoader, TemplateNotFound import json import pathlib -import os import datetime import shutil @@ -14,33 +13,37 @@ from helpers.finances import ( ) # Configure Jinja2 environment -env = Environment(loader=FileSystemLoader('templates')) +env = Environment(loader=FileSystemLoader("templates")) # Set up the output directory for static files -output_dir = pathlib.Path('build') +output_dir = pathlib.Path("build") output_dir.mkdir(exist_ok=True, parents=True) + # Define the icon filter def icon(icon_name): - icon_path = pathlib.Path('assets') / f"dist/icons/{icon_name}.svg" + icon_path = pathlib.Path("assets") / f"dist/icons/{icon_name}.svg" try: - with open(icon_path, 'r', encoding='utf-8') as file: + with open(icon_path, "r", encoding="utf-8") as file: file_content = file.read() except FileNotFoundError: - file_content = '' + file_content = "" return file_content -env.filters['icon'] = icon + +env.filters["icon"] = icon + def render_template_to_file(template_name, output_name, **kwargs): try: template = env.get_template(template_name) output_path = output_dir / output_name - with open(output_path, 'w', encoding='utf-8') as f: + with open(output_path, "w", encoding="utf-8") as f: f.write(template.render(**kwargs)) except TemplateNotFound: print(f"Template {template_name} not found.") + def generate_static_site(development_mode=False): # Common context kwargs = {} @@ -62,8 +65,8 @@ def generate_static_site(development_mode=False): ) # Iterate over all templates in the templates directory - templates_path = pathlib.Path('templates') - for template_file in templates_path.glob('*.html'): + templates_path = pathlib.Path("templates") + for template_file in templates_path.glob("*.html"): template_name = template_file.stem context = kwargs.copy() @@ -76,12 +79,16 @@ def generate_static_site(development_mode=False): finances_period = datetime.date(finances_year, finances_month, 1) finances_period_str = finances_period.strftime("%B %Y") finances_table = generate_transparency_table( - get_transparency_data(finances, finances_year, finances_month, allow_current) + get_transparency_data( + finances, finances_year, finances_month, allow_current + ) + ) + context.update( + { + "finances": finances_table, + "finances_period": finances_period_str, + } ) - context.update({ - "finances": finances_table, - "finances_period": finances_period_str, - }) if template_name == "transparency": finance_data = {} @@ -94,7 +101,9 @@ def generate_static_site(development_mode=False): ) context.update({"finances": finance_data}) - render_template_to_file(f"{template_name}.html", f"{template_name}.html", **context) + render_template_to_file( + f"{template_name}.html", f"{template_name}.html", **context + ) # Generate metrics balances = get_transparency_data(finances, allow_current=True)["end_balance"] @@ -108,21 +117,22 @@ def generate_static_site(development_mode=False): response += f'privatecoffee_balance{{currency="{currency}"}} {balance}\n' metrics_path = output_dir / "metrics.txt" - with open(metrics_path, 'w', encoding='utf-8') as f: + with open(metrics_path, "w", encoding="utf-8") as f: f.write(response) # Copy static assets - assets_src = pathlib.Path('assets') - assets_dst = output_dir / 'assets' + assets_src = pathlib.Path("assets") + assets_dst = output_dir / "assets" if assets_dst.exists(): shutil.rmtree(assets_dst) shutil.copytree(assets_src, assets_dst) print("Static site generated successfully.") + if __name__ == "__main__": parser = ArgumentParser(description="Generate the private.coffee static site.") parser.add_argument("--dev", action="store_true", help="Enable development mode") args = parser.parse_args() - generate_static_site(development_mode=args.dev) \ No newline at end of file + generate_static_site(development_mode=args.dev)