|
| 1 | +name: Release Package |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + tags: |
| 6 | + - "v*.*.*" |
| 7 | + workflow_dispatch: |
| 8 | + inputs: |
| 9 | + tag: |
| 10 | + description: Existing release tag to validate |
| 11 | + required: true |
| 12 | + type: string |
| 13 | + |
| 14 | +permissions: |
| 15 | + contents: read |
| 16 | + |
| 17 | +concurrency: |
| 18 | + group: release-${{ github.ref }} |
| 19 | + cancel-in-progress: false |
| 20 | + |
| 21 | +jobs: |
| 22 | + package: |
| 23 | + name: Build and validate package |
| 24 | + runs-on: ubuntu-latest |
| 25 | + timeout-minutes: 30 |
| 26 | + |
| 27 | + steps: |
| 28 | + - name: Checkout repository |
| 29 | + uses: actions/checkout@v6 |
| 30 | + |
| 31 | + - name: Set up Python |
| 32 | + uses: actions/setup-python@v6 |
| 33 | + with: |
| 34 | + python-version: "3.12" |
| 35 | + cache: pip |
| 36 | + cache-dependency-path: pyproject.toml |
| 37 | + |
| 38 | + - name: Install release tooling |
| 39 | + run: python -m pip install --upgrade pip build twine |
| 40 | + |
| 41 | + - name: Resolve release tag |
| 42 | + id: release |
| 43 | + shell: bash |
| 44 | + run: | |
| 45 | + if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then |
| 46 | + echo "tag=${{ inputs.tag }}" >> "$GITHUB_OUTPUT" |
| 47 | + else |
| 48 | + echo "tag=${GITHUB_REF_NAME}" >> "$GITHUB_OUTPUT" |
| 49 | + fi |
| 50 | +
|
| 51 | + - name: Verify tag and source versions |
| 52 | + env: |
| 53 | + RELEASE_TAG: ${{ steps.release.outputs.tag }} |
| 54 | + run: | |
| 55 | + python - <<'PY' |
| 56 | + import ast |
| 57 | + import os |
| 58 | + import tomllib |
| 59 | + from pathlib import Path |
| 60 | +
|
| 61 | + metadata = tomllib.loads( |
| 62 | + Path("pyproject.toml").read_text(encoding="utf-8") |
| 63 | + ) |
| 64 | + package_version = metadata["project"]["version"] |
| 65 | +
|
| 66 | + module = ast.parse( |
| 67 | + Path( |
| 68 | + "src/edge_traffic_vision/__init__.py" |
| 69 | + ).read_text(encoding="utf-8") |
| 70 | + ) |
| 71 | +
|
| 72 | + version_assignments = [ |
| 73 | + node |
| 74 | + for node in module.body |
| 75 | + if isinstance(node, ast.Assign) |
| 76 | + and any( |
| 77 | + isinstance(target, ast.Name) |
| 78 | + and target.id == "__version__" |
| 79 | + for target in node.targets |
| 80 | + ) |
| 81 | + ] |
| 82 | +
|
| 83 | + if len(version_assignments) != 1: |
| 84 | + raise SystemExit( |
| 85 | + "Expected exactly one __version__ assignment" |
| 86 | + ) |
| 87 | +
|
| 88 | + source_version = ast.literal_eval( |
| 89 | + version_assignments[0].value |
| 90 | + ) |
| 91 | +
|
| 92 | + expected_tag = f"v{package_version}" |
| 93 | + release_tag = os.environ["RELEASE_TAG"] |
| 94 | +
|
| 95 | + if release_tag != expected_tag: |
| 96 | + raise SystemExit( |
| 97 | + f"Tag mismatch: expected {expected_tag}, " |
| 98 | + f"received {release_tag}" |
| 99 | + ) |
| 100 | +
|
| 101 | + if source_version != package_version: |
| 102 | + raise SystemExit( |
| 103 | + f"Source version {source_version} does not match " |
| 104 | + f"metadata version {package_version}" |
| 105 | + ) |
| 106 | +
|
| 107 | + print(f"Version contract verified: {release_tag}") |
| 108 | + PY |
| 109 | +
|
| 110 | + - name: Build distributions |
| 111 | + run: python -m build |
| 112 | + |
| 113 | + - name: Validate distributions |
| 114 | + run: python -m twine check dist/* |
| 115 | + |
| 116 | + - name: Install built wheel |
| 117 | + run: python -m pip install --no-deps --force-reinstall dist/*.whl |
| 118 | + |
| 119 | + - name: Verify installed package version |
| 120 | + env: |
| 121 | + RELEASE_TAG: ${{ steps.release.outputs.tag }} |
| 122 | + run: | |
| 123 | + python - <<'PY' |
| 124 | + import os |
| 125 | +
|
| 126 | + import edge_traffic_vision |
| 127 | +
|
| 128 | + expected = os.environ["RELEASE_TAG"].removeprefix("v") |
| 129 | + actual = edge_traffic_vision.__version__ |
| 130 | +
|
| 131 | + if actual != expected: |
| 132 | + raise SystemExit( |
| 133 | + f"Installed version {actual} does not match {expected}" |
| 134 | + ) |
| 135 | +
|
| 136 | + print(f"Installed package version verified: {actual}") |
| 137 | + PY |
| 138 | +
|
| 139 | + - name: Upload release distributions |
| 140 | + uses: actions/upload-artifact@v4 |
| 141 | + with: |
| 142 | + name: edge-traffic-vision-${{ steps.release.outputs.tag }} |
| 143 | + path: dist/* |
| 144 | + if-no-files-found: error |
| 145 | + retention-days: 14 |
0 commit comments