Skip to content

Update QMS binaries #79

Update QMS binaries

Update QMS binaries #79

name: Update QMS binaries
on:
schedule:
- cron: "17 3 * * *"
workflow_dispatch:
permissions:
contents: write
jobs:
update:
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Download QMS binaries
shell: bash
run: |
set -euo pipefail
python3 <<'PY'
import hashlib
import os
import stat
import urllib.request
import zipfile
from pathlib import Path
binaries = {
"linux": "https://lib.qms.ru/bin/linux/qms_lib.zip",
"linux_arm64": "https://lib.qms.ru/bin/linux_arm64/qms_lib.zip",
}
root = Path("custom_components/speedtest_rt_ru/bin")
checksums = []
for platform, url in binaries.items():
archive_path = Path(f"/tmp/qms_lib_{platform}.zip")
target_dir = root / platform
target_path = target_dir / "qms_lib"
target_dir.mkdir(parents=True, exist_ok=True)
urllib.request.urlretrieve(url, archive_path)
with zipfile.ZipFile(archive_path, "r") as archive:
members = [
member for member in archive.infolist()
if not member.is_dir()
]
selected = next(
(
member for member in members
if Path(member.filename).name == "qms_lib"
),
members[0] if len(members) == 1 else None,
)
if selected is None:
raise FileNotFoundError(
f"No qms_lib executable found in {url}"
)
with archive.open(selected) as source, target_path.open("wb") as target:
target.write(source.read())
mode = target_path.stat().st_mode
target_path.chmod(mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
digest = hashlib.sha256(target_path.read_bytes()).hexdigest()
checksums.append(f"{digest} {platform}/qms_lib")
(root / "SHA256SUMS").write_text("\n".join(checksums) + "\n")
PY
- name: Check for binary updates
id: binary_updates
shell: bash
run: |
set -euo pipefail
if git diff --quiet -- custom_components/speedtest_rt_ru/bin; then
echo "changed=false" >> "$GITHUB_OUTPUT"
else
echo "changed=true" >> "$GITHUB_OUTPUT"
fi
- name: Bump manifest version
if: steps.binary_updates.outputs.changed == 'true'
id: version
shell: bash
run: |
set -euo pipefail
python3 <<'PY'
import json
import os
import subprocess
from pathlib import Path
manifest_path = Path("custom_components/speedtest_rt_ru/manifest.json")
manifest = json.loads(manifest_path.read_text())
current_version = manifest["version"]
parts = current_version.split(".")
if len(parts) != 3 or not all(part.isdigit() for part in parts):
raise ValueError(
f"Expected semantic version like 1.2.3, got {current_version}"
)
major, minor, patch = (int(part) for part in parts)
existing_tags = set(
subprocess.check_output(
["git", "tag", "--list"],
text=True,
).splitlines()
)
while True:
patch += 1
new_version = f"{major}.{minor}.{patch}"
tag = f"v{new_version}"
if tag not in existing_tags:
break
manifest["version"] = new_version
manifest_path.write_text(
json.dumps(manifest, indent=2, ensure_ascii=False) + "\n"
)
with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as output:
output.write(f"version={new_version}\n")
output.write(f"tag={tag}\n")
PY
- name: Commit update
if: steps.binary_updates.outputs.changed == 'true'
shell: bash
run: |
set -euo pipefail
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add custom_components/speedtest_rt_ru/bin custom_components/speedtest_rt_ru/manifest.json
git commit -m "Update RT.RU QMS binaries to ${{ steps.version.outputs.version }}"
git push origin "HEAD:${GITHUB_REF_NAME}"
- name: Create tag
if: steps.binary_updates.outputs.changed == 'true'
shell: bash
run: |
set -euo pipefail
git tag "${{ steps.version.outputs.tag }}"
git push origin "${{ steps.version.outputs.tag }}"
- name: Create release notes
if: steps.binary_updates.outputs.changed == 'true'
shell: bash
run: |
set -euo pipefail
{
echo "Updates the bundled RT.RU QMS binaries used by the Home Assistant integration."
echo
echo "Manifest version: ${{ steps.version.outputs.version }}"
echo
echo "SHA256 checksums:"
echo '```'
cat custom_components/speedtest_rt_ru/bin/SHA256SUMS
echo '```'
} > /tmp/qms-release-notes.md
- name: Create GitHub release
if: steps.binary_updates.outputs.changed == 'true'
env:
GH_TOKEN: ${{ github.token }}
shell: bash
run: |
set -euo pipefail
gh release create "${{ steps.version.outputs.tag }}" \
--title "${{ steps.version.outputs.tag }}" \
--notes-file /tmp/qms-release-notes.md
- name: No update found
if: steps.binary_updates.outputs.changed != 'true'
run: echo "QMS binaries are already up to date."