|
| 1 | +name: Publish FrameOS cross-toolchain images |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + paths: |
| 8 | + - backend/bin/cross |
| 9 | + - backend/tools/cross-toolchain.Dockerfile |
| 10 | + - .github/workflows/frameos-cross-toolchain.yml |
| 11 | + workflow_dispatch: |
| 12 | + inputs: |
| 13 | + image_tag: |
| 14 | + description: "Toolchain image tag to publish" |
| 15 | + required: false |
| 16 | + default: latest |
| 17 | + type: string |
| 18 | + |
| 19 | +permissions: |
| 20 | + contents: write |
| 21 | + |
| 22 | +jobs: |
| 23 | + determine-matrix: |
| 24 | + runs-on: ubuntu-24.04 |
| 25 | + outputs: |
| 26 | + targets: ${{ steps.set-matrix.outputs.targets }} |
| 27 | + steps: |
| 28 | + - uses: actions/checkout@v4 |
| 29 | + - id: set-matrix |
| 30 | + run: | |
| 31 | + MATRIX=$(python3 backend/bin/cross matrix) |
| 32 | + echo "targets=$MATRIX" >> "$GITHUB_OUTPUT" |
| 33 | +
|
| 34 | + publish: |
| 35 | + needs: determine-matrix |
| 36 | + runs-on: ubuntu-24.04 |
| 37 | + steps: |
| 38 | + - uses: actions/checkout@v4 |
| 39 | + - uses: docker/setup-qemu-action@v3 |
| 40 | + - uses: docker/setup-buildx-action@v3 |
| 41 | + - name: Login to Docker Hub |
| 42 | + uses: docker/login-action@v3 |
| 43 | + with: |
| 44 | + username: ${{ secrets.DOCKERHUB_USERNAME }} |
| 45 | + password: ${{ secrets.DOCKERHUB_TOKEN }} |
| 46 | + - name: Build and push cross-toolchain images |
| 47 | + env: |
| 48 | + IMAGE_TAG: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.image_tag || 'latest' }} |
| 49 | + MATRIX: ${{ needs.determine-matrix.outputs.targets }} |
| 50 | + run: | |
| 51 | + set -euo pipefail |
| 52 | + python3 - <<'PY' |
| 53 | + from __future__ import annotations |
| 54 | +
|
| 55 | + import json |
| 56 | + import os |
| 57 | + import re |
| 58 | + import subprocess |
| 59 | + from pathlib import Path |
| 60 | +
|
| 61 | + def sanitize(value: str) -> str: |
| 62 | + return re.sub(r"[^A-Za-z0-9_.-]+", "_", value) |
| 63 | +
|
| 64 | + matrix = json.loads(os.environ["MATRIX"]) |
| 65 | + image_repo = "frameos/frameos-cross-toolchain" |
| 66 | + image_tag = os.environ.get("IMAGE_TAG", "latest").strip() or "latest" |
| 67 | + digest_data = {"images": {}} |
| 68 | +
|
| 69 | + for index, target in enumerate(matrix["include"]): |
| 70 | + base_image = target["image"] |
| 71 | + safe_base = sanitize(base_image.replace("/", "_")) |
| 72 | + safe_platform = sanitize(target["platform"].replace("/", "_")) |
| 73 | + image = f"{image_repo}:{safe_base}-{safe_platform}-{image_tag}" |
| 74 | + metadata_file = Path(f"/tmp/toolchain-metadata-{index}.json") |
| 75 | +
|
| 76 | + subprocess.run( |
| 77 | + [ |
| 78 | + "docker", |
| 79 | + "buildx", |
| 80 | + "build", |
| 81 | + "--platform", |
| 82 | + target["platform"], |
| 83 | + "--build-arg", |
| 84 | + f"BASE_IMAGE={base_image}", |
| 85 | + "--tag", |
| 86 | + image, |
| 87 | + "--push", |
| 88 | + "--metadata-file", |
| 89 | + str(metadata_file), |
| 90 | + "-f", |
| 91 | + "backend/tools/cross-toolchain.Dockerfile", |
| 92 | + ".", |
| 93 | + ], |
| 94 | + check=True, |
| 95 | + ) |
| 96 | +
|
| 97 | + metadata = json.loads(metadata_file.read_text(encoding="utf-8")) |
| 98 | + digest = (metadata.get("containerimage") or {}).get("digest") |
| 99 | + if not digest: |
| 100 | + raise RuntimeError(f"Missing container image digest for {image}") |
| 101 | +
|
| 102 | + digest_data["images"][image] = { |
| 103 | + "digest": digest, |
| 104 | + "platform": target["platform"], |
| 105 | + "base_image": base_image, |
| 106 | + "tag": image_tag, |
| 107 | + } |
| 108 | +
|
| 109 | + Path("cross-toolchain-images.json").write_text( |
| 110 | + json.dumps(digest_data, indent=2, sort_keys=True) + "\n", |
| 111 | + encoding="utf-8", |
| 112 | + ) |
| 113 | + PY |
| 114 | + - name: Commit updated cross-toolchain digests |
| 115 | + if: github.event_name == 'push' |
| 116 | + run: | |
| 117 | + set -euo pipefail |
| 118 | + git add cross-toolchain-images.json |
| 119 | + if git diff --cached --quiet; then |
| 120 | + echo "No cross-toolchain digest updates to commit." |
| 121 | + exit 0 |
| 122 | + fi |
| 123 | + git config user.name "github-actions[bot]" |
| 124 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 125 | + git commit -m "chore: refresh cross-toolchain image digests [skip ci]" |
| 126 | + git push |
0 commit comments