|
| 1 | +name: docker-release |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + release: |
| 6 | + types: |
| 7 | + - published |
| 8 | + workflow_dispatch: |
| 9 | + |
| 10 | +env: |
| 11 | + REGISTRY: ghcr.io |
| 12 | + IMAGE_NAME: ${{ github.repository }} |
| 13 | + |
| 14 | +permissions: |
| 15 | + contents: read |
| 16 | + |
| 17 | +concurrency: |
| 18 | + group: docker-release-${{ github.ref }} |
| 19 | + cancel-in-progress: true |
| 20 | + |
| 21 | +jobs: |
| 22 | + test: |
| 23 | + runs-on: ubuntu-latest |
| 24 | + steps: |
| 25 | + - name: Checkout |
| 26 | + uses: actions/checkout@v6 |
| 27 | + |
| 28 | + - name: Set up Python |
| 29 | + uses: actions/setup-python@v6 |
| 30 | + with: |
| 31 | + python-version-file: pyproject.toml |
| 32 | + |
| 33 | + - name: Install uv |
| 34 | + uses: astral-sh/setup-uv@v7 |
| 35 | + with: |
| 36 | + enable-cache: true |
| 37 | + |
| 38 | + - name: Install project |
| 39 | + run: uv sync --locked --extra dev |
| 40 | + |
| 41 | + - name: Run tests |
| 42 | + run: uv run pytest -q |
| 43 | + |
| 44 | + docker-validate: |
| 45 | + needs: test |
| 46 | + if: github.event_name == 'pull_request' || github.event_name == 'workflow_dispatch' |
| 47 | + runs-on: ubuntu-latest |
| 48 | + steps: |
| 49 | + - name: Checkout |
| 50 | + uses: actions/checkout@v6 |
| 51 | + |
| 52 | + - name: Set up Docker Buildx |
| 53 | + uses: docker/setup-buildx-action@v4 |
| 54 | + |
| 55 | + - name: Build Docker image |
| 56 | + uses: docker/build-push-action@v7 |
| 57 | + with: |
| 58 | + context: . |
| 59 | + file: ./Dockerfile |
| 60 | + platforms: linux/amd64 |
| 61 | + push: false |
| 62 | + cache-from: type=gha |
| 63 | + cache-to: type=gha,mode=max |
| 64 | + |
| 65 | + docker-release: |
| 66 | + needs: test |
| 67 | + if: github.event_name == 'release' |
| 68 | + runs-on: ubuntu-latest |
| 69 | + permissions: |
| 70 | + contents: read |
| 71 | + packages: write |
| 72 | + attestations: write |
| 73 | + id-token: write |
| 74 | + steps: |
| 75 | + - name: Checkout |
| 76 | + uses: actions/checkout@v6 |
| 77 | + |
| 78 | + - name: Set up Python |
| 79 | + uses: actions/setup-python@v6 |
| 80 | + with: |
| 81 | + python-version-file: pyproject.toml |
| 82 | + |
| 83 | + - name: Validate release tag matches package version |
| 84 | + shell: python |
| 85 | + run: | |
| 86 | + import re |
| 87 | + import tomllib |
| 88 | + from pathlib import Path |
| 89 | +
|
| 90 | + tag = "${{ github.event.release.tag_name }}" |
| 91 | + pyproject = tomllib.loads(Path("pyproject.toml").read_text(encoding="utf-8")) |
| 92 | + project_version = str(pyproject["project"]["version"]).strip() |
| 93 | +
|
| 94 | + init_text = Path("src/roborock_local_server/__init__.py").read_text(encoding="utf-8") |
| 95 | + match = re.search(r'__version__\s*=\s*"([^"]+)"', init_text) |
| 96 | + if match is None: |
| 97 | + raise SystemExit("Could not find __version__ in src/roborock_local_server/__init__.py") |
| 98 | +
|
| 99 | + module_version = match.group(1).strip() |
| 100 | + expected_tag = f"v{project_version}" |
| 101 | + if module_version != project_version: |
| 102 | + raise SystemExit( |
| 103 | + f"Version mismatch: pyproject.toml={project_version}, __init__.py={module_version}" |
| 104 | + ) |
| 105 | + if tag != expected_tag: |
| 106 | + raise SystemExit(f"Git tag {tag} does not match package version {expected_tag}") |
| 107 | +
|
| 108 | + - name: Log in to GHCR |
| 109 | + uses: docker/login-action@v3 |
| 110 | + with: |
| 111 | + registry: ${{ env.REGISTRY }} |
| 112 | + username: ${{ github.actor }} |
| 113 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 114 | + |
| 115 | + - name: Set up QEMU |
| 116 | + uses: docker/setup-qemu-action@v3 |
| 117 | + |
| 118 | + - name: Set up Docker Buildx |
| 119 | + uses: docker/setup-buildx-action@v4 |
| 120 | + |
| 121 | + - name: Extract metadata |
| 122 | + id: meta |
| 123 | + uses: docker/metadata-action@v5 |
| 124 | + with: |
| 125 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 126 | + flavor: latest=false |
| 127 | + tags: | |
| 128 | + type=semver,pattern={{version}},value=${{ github.event.release.tag_name }} |
| 129 | + type=semver,pattern={{major}}.{{minor}},value=${{ github.event.release.tag_name }} |
| 130 | + type=semver,pattern={{major}},value=${{ github.event.release.tag_name }} |
| 131 | + type=raw,value=latest,enable=${{ startsWith(github.event.release.tag_name, 'v') && !contains(github.event.release.tag_name, '-') }} |
| 132 | + type=sha,prefix=sha- |
| 133 | + labels: | |
| 134 | + org.opencontainers.image.title=roborock-local-server |
| 135 | + org.opencontainers.image.description=Private Roborock HTTPS and MQTT stack you run on your own system. |
| 136 | +
|
| 137 | + - name: Build and push Docker image |
| 138 | + id: push |
| 139 | + uses: docker/build-push-action@v7 |
| 140 | + with: |
| 141 | + context: . |
| 142 | + file: ./Dockerfile |
| 143 | + platforms: linux/amd64,linux/arm64 |
| 144 | + push: true |
| 145 | + tags: ${{ steps.meta.outputs.tags }} |
| 146 | + labels: ${{ steps.meta.outputs.labels }} |
| 147 | + cache-from: type=gha |
| 148 | + cache-to: type=gha,mode=max |
| 149 | + |
| 150 | + - name: Generate artifact attestation |
| 151 | + uses: actions/attest@v4 |
| 152 | + with: |
| 153 | + subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 154 | + subject-digest: ${{ steps.push.outputs.digest }} |
| 155 | + push-to-registry: true |
0 commit comments