From 7ccacec68f702595f687379fc13b7893fb565976 Mon Sep 17 00:00:00 2001 From: Manuel Weiser Date: Sun, 16 Feb 2025 10:26:43 +0100 Subject: [PATCH] feat: add GitHub Actions workflow for automated release creation and update CHANGELOG.md structure --- .github/workflows/release.yml | 33 ++++++++++++++++++++ CHANGELOG.md | 14 +++++++++ platformio.ini | 12 ++++++-- release.sh | 34 ++++++++++++++++++++ scripts/combine_html.py | 31 +++++++++++++++++++ extra_script.py => scripts/extra_script.py | 0 gzip_files.py => scripts/gzip_files.py | 2 +- scripts/pre_build.py | 25 +++++++++++++++ scripts/pre_spiffs.py | 7 +++++ scripts/update_changelog.py | 36 ++++++++++++++++++++++ 10 files changed, 191 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/release.yml create mode 100644 CHANGELOG.md create mode 100644 release.sh create mode 100644 scripts/combine_html.py rename extra_script.py => scripts/extra_script.py (100%) rename gzip_files.py => scripts/gzip_files.py (95%) create mode 100644 scripts/pre_build.py create mode 100644 scripts/pre_spiffs.py create mode 100644 scripts/update_changelog.py diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..5385246 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,33 @@ +name: Create Release + +on: + push: + tags: + - 'v*' + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - name: Get version from tag + id: get_version + run: echo ::set-output name=VERSION::${GITHUB_REF/refs\/tags\/v/} + + - name: Read CHANGELOG.md + id: changelog + run: | + CHANGELOG=$(awk "/## \[${{ steps.get_version.outputs.VERSION }}\]/{p=1;print;next} /## \[/{p=0} p" CHANGELOG.md) + echo "::set-output name=CHANGES::$CHANGELOG" + + - name: Create Release + uses: actions/create-release@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + tag_name: ${{ github.ref }} + release_name: Release ${{ steps.get_version.outputs.VERSION }} + body: ${{ steps.changelog.outputs.CHANGES }} + draft: false + prerelease: false \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..286351e --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,14 @@ +# Changelog + +## [1.0.2] - 2025-02-16 +### Added +- Feature 1 +- Feature 2 + +### Changed +- Change 1 +- Change 2 + +### Fixed +- Fix 1 +- Fix 2 \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index d4d055e..825a2f3 100644 --- a/platformio.ini +++ b/platformio.ini @@ -8,6 +8,9 @@ ; Please visit documentation for the other options and examples ; https://docs.platformio.org/page/projectconf.html +[common] +version = "1.0.2" + [env:esp32dev] platform = espressif32 board = esp32dev @@ -37,7 +40,12 @@ build_flags = -fdata-sections -DNDEBUG -mtext-section-literals + '-D VERSION="${common.version}"' extra_scripts = - pre:gzip_files.py - pre:extra_script.py + pre:scripts/combine_html.py + pre:scripts/pre_build.py + pre:scripts/pre_spiffs.py + pre:scripts/gzip_files.py + pre:scripts/extra_script.py + pre:scripts/update_changelog.py diff --git a/release.sh b/release.sh new file mode 100644 index 0000000..3c5011f --- /dev/null +++ b/release.sh @@ -0,0 +1,34 @@ +#!/bin/bash + +# Get version from platformio.ini +VERSION=$(grep '^version = ' platformio.ini | sed 's/version = "\(.*\)"/\1/') + +if [ -z "$VERSION" ]; then + echo "Error: Could not find version in platformio.ini" + exit 1 +fi + +echo "Creating release for version $VERSION" + +# Check if working directory is clean +if [ -n "$(git status --porcelain)" ]; then + echo "Error: Working directory is not clean. Please commit all changes first." + exit 1 +fi + +# Create and push tag +git tag "v$VERSION" +if [ $? -ne 0 ]; then + echo "Error: Failed to create tag v$VERSION" + exit 1 +fi + +git push origin "v$VERSION" +if [ $? -ne 0 ]; then + echo "Error: Failed to push tag v$VERSION" + git tag -d "v$VERSION" + exit 1 +fi + +echo "Successfully created and pushed tag v$VERSION" +echo "GitHub Actions workflow will now create the release" \ No newline at end of file diff --git a/scripts/combine_html.py b/scripts/combine_html.py new file mode 100644 index 0000000..b13ccd6 --- /dev/null +++ b/scripts/combine_html.py @@ -0,0 +1,31 @@ +Import("env") +import os + +def combine_html_files(source, target, env): + html_dir = "./html" + header_file = os.path.join(html_dir, "header.html") + + # Read header content + with open(header_file, 'r') as f: + header_content = f.read() + + # Process all HTML files except header.html + for filename in os.listdir(html_dir): + if filename.endswith('.html') and filename != 'header.html': + file_path = os.path.join(html_dir, filename) + + # Read content + with open(file_path, 'r') as f: + content = f.read() + + # Replace placeholder with header content + if '{{header}}' in content: + new_content = content.replace('{{header}}', header_content) + + # Write back combined content + with open(file_path, 'w') as f: + f.write(new_content) + print(f"Combined header with {filename}") + +# Register the script to run before building SPIFFS +env.AddPreAction("buildfs", combine_html_files) \ No newline at end of file diff --git a/extra_script.py b/scripts/extra_script.py similarity index 100% rename from extra_script.py rename to scripts/extra_script.py diff --git a/gzip_files.py b/scripts/gzip_files.py similarity index 95% rename from gzip_files.py rename to scripts/gzip_files.py index 4bb48b2..9035dc5 100644 --- a/gzip_files.py +++ b/scripts/gzip_files.py @@ -14,7 +14,7 @@ def copy_file(input_file, output_file): def should_compress(file): # Komprimiere nur bestimmte Dateitypen - return file.endswith(('.js', '.png', '.css')) + return file.endswith(('.js', '.png', '.css', '.html')) def main(source_dir, target_dir): for root, dirs, files in os.walk(source_dir): diff --git a/scripts/pre_build.py b/scripts/pre_build.py new file mode 100644 index 0000000..a047d85 --- /dev/null +++ b/scripts/pre_build.py @@ -0,0 +1,25 @@ +Import("env") +import os + +def replace_version(source, target, env): + # Get version from common section + version = env.GetProjectConfig().get("common", "version").strip('"') + header_file = "./html/header.html" + + with open(header_file, 'r') as file: + content = file.read() + + # Replace version in header.html using string manipulation instead of regex + search = '

FilaManv' + end = '' + start_pos = content.find(search) + if start_pos != -1: + start_pos += len(search) + end_pos = content.find(end, start_pos) + if end_pos != -1: + content = content[:start_pos] + version + content[end_pos:] + + with open(header_file, 'w') as file: + file.write(content) + +env.AddPreAction("buildfs", replace_version) \ No newline at end of file diff --git a/scripts/pre_spiffs.py b/scripts/pre_spiffs.py new file mode 100644 index 0000000..2901895 --- /dev/null +++ b/scripts/pre_spiffs.py @@ -0,0 +1,7 @@ +Import("env") + +# Wiederverwendung der replace_version Funktion +exec(open("./scripts/pre_build.py").read()) + +# Bind to SPIFFS build +env.AddPreAction("buildfs", replace_version) \ No newline at end of file diff --git a/scripts/update_changelog.py b/scripts/update_changelog.py new file mode 100644 index 0000000..6c43d28 --- /dev/null +++ b/scripts/update_changelog.py @@ -0,0 +1,36 @@ +import os +import re +from datetime import datetime + +def get_version(): + with open('../platformio.ini', 'r') as f: + content = f.read() + version_match = re.search(r'version\s*=\s*"([^"]+)"', content) + return version_match.group(1) if version_match else None + +def update_changelog(): + version = get_version() + today = datetime.now().strftime('%Y-%m-%d') + + changelog_template = f"""## [{version}] - {today} +### Added +- + +### Changed +- + +### Fixed +- +""" + + with open('../CHANGELOG.md', 'r') as f: + content = f.read() + + # Insert new version template after the header + updated_content = content.replace("# Changelog\n", f"# Changelog\n\n{changelog_template}") + + with open('../CHANGELOG.md', 'w') as f: + f.write(updated_content) + +if __name__ == "__main__": + update_changelog() \ No newline at end of file