|
| 1 | +name: Sync manifest version from tag |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + tags: |
| 6 | + - 'v*.*.*' |
| 7 | + |
| 8 | +concurrency: |
| 9 | + group: sync-manifest-version |
| 10 | + cancel-in-progress: true |
| 11 | + |
| 12 | +permissions: |
| 13 | + contents: write |
| 14 | + |
| 15 | +jobs: |
| 16 | + sync: |
| 17 | + name: Sync manifest.json version to release tag |
| 18 | + runs-on: ubuntu-latest |
| 19 | + steps: |
| 20 | + - name: Checkout main |
| 21 | + uses: actions/checkout@v4 |
| 22 | + with: |
| 23 | + ref: main |
| 24 | + fetch-depth: 0 |
| 25 | + token: ${{ secrets.PUSH_TOKEN || secrets.GITHUB_TOKEN }} |
| 26 | + |
| 27 | + - name: Verify tag commit is in main history |
| 28 | + env: |
| 29 | + TAG_NAME: ${{ github.ref_name }} |
| 30 | + run: | |
| 31 | + TAG_COMMIT=$(git rev-list -n 1 "refs/tags/${TAG_NAME}") |
| 32 | + if ! git merge-base --is-ancestor "$TAG_COMMIT" HEAD; then |
| 33 | + echo "Tag ${TAG_NAME} (${TAG_COMMIT}) is not an ancestor of main; refusing to sync manifest." |
| 34 | + exit 1 |
| 35 | + fi |
| 36 | +
|
| 37 | + - name: Set up Python |
| 38 | + uses: actions/setup-python@v5 |
| 39 | + with: |
| 40 | + python-version: '3.13' |
| 41 | + |
| 42 | + - name: Install packaging |
| 43 | + run: python -m pip install --quiet packaging |
| 44 | + |
| 45 | + - name: Update manifest.json version |
| 46 | + env: |
| 47 | + TAG_NAME: ${{ github.ref_name }} |
| 48 | + run: | |
| 49 | + VERSION="${TAG_NAME#v}" |
| 50 | + MANIFEST="custom_components/unifi_network_rules/manifest.json" |
| 51 | +
|
| 52 | + python3 - "$MANIFEST" "$VERSION" <<'PY' |
| 53 | + import json, re, sys |
| 54 | + from packaging.version import InvalidVersion, Version |
| 55 | +
|
| 56 | + path, new = sys.argv[1], sys.argv[2] |
| 57 | + with open(path) as f: |
| 58 | + text = f.read() |
| 59 | + current = json.loads(text)["version"] |
| 60 | +
|
| 61 | + try: |
| 62 | + if Version(new) <= Version(current): |
| 63 | + print(f"manifest.json version {current} is already >= tag {new}; skipping") |
| 64 | + sys.exit(0) |
| 65 | + except InvalidVersion as err: |
| 66 | + print(f"Unable to compare versions ({err}); refusing to update") |
| 67 | + sys.exit(1) |
| 68 | +
|
| 69 | + updated, n = re.subn( |
| 70 | + r'("version"\s*:\s*")[^"]+(")', |
| 71 | + lambda m: m.group(1) + new + m.group(2), |
| 72 | + text, |
| 73 | + count=1, |
| 74 | + ) |
| 75 | + if n != 1: |
| 76 | + print("Could not locate version field in manifest.json") |
| 77 | + sys.exit(1) |
| 78 | + with open(path, "w") as f: |
| 79 | + f.write(updated) |
| 80 | + print(f"manifest.json: {current} -> {new}") |
| 81 | + PY |
| 82 | +
|
| 83 | + - name: Commit and push if changed |
| 84 | + env: |
| 85 | + TAG_NAME: ${{ github.ref_name }} |
| 86 | + run: | |
| 87 | + git config user.name "github-actions[bot]" |
| 88 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 89 | +
|
| 90 | + git add custom_components/unifi_network_rules/manifest.json |
| 91 | +
|
| 92 | + if git diff --cached --quiet; then |
| 93 | + echo "No version changes to commit" |
| 94 | + exit 0 |
| 95 | + fi |
| 96 | +
|
| 97 | + git commit -m "chore(release): sync manifest.json to ${TAG_NAME} [skip ci]" |
| 98 | + git push |
0 commit comments