Merge branch 'main' of https://github.com/smarthomeshop/p1meterkit #1
This file contains hidden or 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
| name: Build and Publish P1MeterKit firmware | ||
| on: | ||
| push: | ||
| branches: | ||
| - main | ||
| paths: | ||
| - 'p1meterkit-v2/**' | ||
| pull_request: | ||
| paths: | ||
| - 'p1meterkit-v2/**' | ||
| workflow_dispatch: | ||
| permissions: | ||
| contents: write | ||
| jobs: | ||
| build: | ||
| name: Build firmware | ||
| runs-on: ubuntu-latest | ||
| outputs: | ||
| project_version: ${{ steps.version.outputs.new_version }} | ||
| firmware_name: ${{ steps.esphome_build.outputs.name }} | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| with: | ||
| token: ${{ secrets.GITHUB_TOKEN }} | ||
| - name: Determine firmware version | ||
| id: version | ||
| shell: bash | ||
| run: | | ||
| set -euo pipefail | ||
| YAML_FILE="p1meterkit-v2/p1meterkit.yaml" | ||
| python - <<'PY' | ||
| import os | ||
| import re | ||
| from pathlib import Path | ||
| path = Path("p1meterkit-v2/p1meterkit.yaml") | ||
| text = path.read_text() | ||
| match = re.search(r'p1meterkit_software_version:\s*"([^"]+)"', text) | ||
| if not match: | ||
| raise SystemExit("Kon huidige versie niet vinden in p1meterkit-v2/p1meterkit.yaml") | ||
| current = match.group(1) | ||
| event_name = os.getenv("GITHUB_EVENT_NAME", "") | ||
| ref = os.getenv("GITHUB_REF", "") | ||
| ref_type = os.getenv("GITHUB_REF_TYPE", "") | ||
| ref_name = os.getenv("GITHUB_REF_NAME", "") | ||
| sha = os.getenv("GITHUB_SHA", "")[:7] | ||
| new_version = current | ||
| version_changed = False | ||
| if ref_type == "tag" and re.fullmatch(r"v\d+(?:\.\d+)*", ref_name or ""): | ||
| new_version = (ref_name or "")[1:] | ||
| elif event_name == "push" and ref == "refs/heads/main": | ||
| parts = current.split('.') | ||
| try: | ||
| numbers = [int(part) for part in parts] | ||
| except ValueError: | ||
| numbers = None | ||
| if numbers: | ||
| numbers[-1] += 1 | ||
| new_version = '.'.join(str(n) for n in numbers) | ||
| else: | ||
| new_version = current | ||
| else: | ||
| new_version = f"dev-{sha}" | ||
| if new_version != current: | ||
| version_changed = True | ||
| pattern_version = re.compile(r'(p1meterkit_software_version:\s*)"[^"]+"') | ||
| pattern_project = re.compile( | ||
| r'(project:\s*\n\s*name:\s*"smarthomeshop\.p1meterkit"\s*\n\s*version:\s*)"[^"]+"' | ||
| ) | ||
| updated = pattern_version.sub(rf"\1\"{new_version}\"", text) | ||
| updated = pattern_project.sub(rf"\1\"{new_version}\"", updated) | ||
| path.write_text(updated) | ||
| print(f"Huidige versie: {current}") | ||
| print(f"Nieuwe versie: {new_version}") | ||
| with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as fh: | ||
| fh.write(f"new_version={new_version}\n") | ||
| fh.write(f"version_changed={'true' if version_changed else 'false'}\n") | ||
| PY | ||
| - name: Commit versie bump | ||
| if: github.event_name == 'push' && github.ref == 'refs/heads/main' && steps.version.outputs.version_changed == 'true' | ||
| shell: bash | ||
| run: | | ||
| set -euo pipefail | ||
| git config --local user.email "action@github.com" | ||
| git config --local user.name "GitHub Action" | ||
| git add p1meterkit-v2/p1meterkit.yaml | ||
| git commit -m "chore: bump firmware version to ${{ steps.version.outputs.new_version }}" || exit 0 | ||
| git push || echo "Geen wijzigingen om te pushen" | ||
| - name: Dummy secrets aanmaken | ||
| shell: bash | ||
| run: | | ||
| set -euo pipefail | ||
| cat <<'SECRETS' > p1meterkit-v2/secrets.yaml | ||
| wifi_ssid: "DUMMY" | ||
| wifi_password: "DUMMYPASS" | ||
| SECRETS | ||
| - name: ESPHome build uitvoeren | ||
| id: esphome_build | ||
| uses: esphome/build-action@v6 | ||
| with: | ||
| yaml-file: p1meterkit-v2/p1meterkit.yaml | ||
| - name: Build artefact verzamelen | ||
| shell: bash | ||
| run: | | ||
| set -euo pipefail | ||
| mkdir -p output/p1meterkit-v2 | ||
| mv "${{ steps.esphome_build.outputs.name }}" output/p1meterkit-v2/ | ||
| rm -f p1meterkit-v2/secrets.yaml | ||
| - name: Artefact uploaden | ||
| uses: actions/upload-artifact@v4 | ||
| with: | ||
| name: p1meterkit-v2-${{ steps.version.outputs.new_version }} | ||
| path: output | ||
| publish: | ||
| name: Publiceer naar GitHub Pages | ||
| runs-on: ubuntu-latest | ||
| needs: build | ||
| if: github.event_name == 'push' && github.ref == 'refs/heads/main' | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| - uses: actions/download-artifact@v4 | ||
| with: | ||
| name: p1meterkit-v2-${{ needs.build.outputs.project_version }} | ||
| path: output | ||
| - name: Manifest voorbereiden | ||
| shell: bash | ||
| run: | | ||
| set -euo pipefail | ||
| VERSION="${{ needs.build.outputs.project_version }}" | ||
| FIRMWARE_DIR="$PWD/output/p1meterkit-v2/${{ needs.build.outputs.firmware_name }}" | ||
| MANIFEST="$FIRMWARE_DIR/manifest.json" | ||
| if [[ -f "$MANIFEST" ]]; then | ||
| PREFIX="p1meterkit-v2/${{ needs.build.outputs.firmware_name }}/" | ||
| jq \ | ||
| --arg version "$VERSION" \ | ||
| --arg prefix "$PREFIX" \ | ||
| '(.name = "P1MeterKit") | ||
| | (.version = $version) | ||
| | (.builds[].ota.path |= $prefix + .) | ||
| | (.builds[].parts[].path |= $prefix + .)' \ | ||
| "$MANIFEST" > "$MANIFEST.tmp" | ||
| mv "$MANIFEST.tmp" "$MANIFEST" | ||
| cp "$MANIFEST" "$PWD/output/p1meterkit-v2/manifest.json" | ||
| fi | ||
| - name: Deploy naar gh-pages | ||
| uses: JamesIves/github-pages-deploy-action@v4 | ||
| with: | ||
| branch: gh-pages | ||
| folder: output | ||
| clean: false | ||