From 606109d62946b88f0106d26eea3fbf3bafe7fd73 Mon Sep 17 00:00:00 2001 From: maxDorninger <97409287+maxDorninger@users.noreply.github.com> Date: Sat, 29 Nov 2025 16:57:23 +0100 Subject: [PATCH] stop saving of docker build cache when run by PRs from forks and build x86 and arm64 image in parallel --- .github/workflows/build-push-backend.yml | 118 +++++++++++++++--- .../workflows/build-push-metadata_relay.yml | 117 ++++++++++++++--- 2 files changed, 198 insertions(+), 37 deletions(-) diff --git a/.github/workflows/build-push-backend.yml b/.github/workflows/build-push-backend.yml index 75e1034..bfd27d2 100644 --- a/.github/workflows/build-push-backend.yml +++ b/.github/workflows/build-push-backend.yml @@ -36,7 +36,6 @@ jobs: lint-backend: name: Lint Python Code runs-on: ubuntu-latest - steps: - uses: actions/checkout@v4 - uses: astral-sh/ruff-action@v3 @@ -61,10 +60,15 @@ jobs: run: npm run lint working-directory: ./web - - build-and-push: + build: needs: [lint-frontend, lint-backend] runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + platform: + - linux/amd64 + - linux/arm64 permissions: contents: read packages: write @@ -77,6 +81,10 @@ jobs: id: repo_name run: echo "name=$(echo '${{ github.event.repository.name }}' | tr '[:upper:]' '[:lower:]')" >> $GITHUB_OUTPUT + - name: Prepare platform slug + id: platform_slug + run: echo "slug=${{ matrix.platform }}" | tr '/' '-' >> $GITHUB_OUTPUT + - name: Set up QEMU uses: docker/setup-qemu-action@v3 @@ -104,7 +112,85 @@ jobs: fi echo "version=$VERSION" >> $GITHUB_OUTPUT - - name: Extract metadata (tags, labels) for Docker + - name: Extract metadata (labels only) + id: meta + uses: docker/metadata-action@v5 + with: + images: ghcr.io/${{ github.repository_owner }}/${{ steps.repo_name.outputs.name }}/mediamanager + + - name: Build and push by digest + id: build + uses: docker/build-push-action@v6 + with: + context: . + file: ./Dockerfile + platforms: ${{ matrix.platform }} + labels: ${{ steps.meta.outputs.labels }} + outputs: type=image,name=ghcr.io/${{ github.repository_owner }}/${{ steps.repo_name.outputs.name }}/mediamanager,push-by-digest=true,name-canonical=true,push=${{ github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository }} + build-args: | + VERSION=${{ steps.version.outputs.version }} + BASE_PATH= + cache-from: type=registry,ref=ghcr.io/${{ github.repository_owner }}/${{ steps.repo_name.outputs.name }}/mediamanager:buildcache-${{ steps.platform_slug.outputs.slug }} + cache-to: ${{ (github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository) && format('type=registry,ref=ghcr.io/{0}/{1}/mediamanager:buildcache-{2},mode=max', github.repository_owner, steps.repo_name.outputs.name, steps.platform_slug.outputs.slug) || '' }} + no-cache: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.repository }} + + - name: Export digest + run: | + mkdir -p /tmp/digests + digest="${{ steps.build.outputs.digest }}" + touch "/tmp/digests/${digest#sha256:}" + + - name: Upload digest + uses: actions/upload-artifact@v4 + with: + name: digests-backend-${{ strategy.job-index }} + path: /tmp/digests/* + if-no-files-found: error + retention-days: 1 + + merge: + needs: build + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository + steps: + - name: Download digests + uses: actions/download-artifact@v4 + with: + pattern: digests-backend-* + path: /tmp/digests + merge-multiple: true + + - name: Set repository name to lowercase + id: repo_name + run: echo "name=$(echo '${{ github.event.repository.name }}' | tr '[:upper:]' '[:lower:]')" >> $GITHUB_OUTPUT + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Log in to GitHub Container Registry + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract version + id: version + run: | + if [[ "${{ github.event_name }}" == "release" ]]; then + VERSION="${{ github.event.release.tag_name }}" + elif [[ "${{ github.ref_type }}" == "tag" ]]; then + VERSION="${{ github.ref_name }}" + else + DATE_STAMP=$(date -u +'%Y.%m.%d') + VERSION="dev-${DATE_STAMP}-${{ github.run_number }}" + fi + echo "version=$VERSION" >> $GITHUB_OUTPUT + + - name: Extract metadata (tags) id: meta uses: docker/metadata-action@v5 with: @@ -119,18 +205,12 @@ jobs: type=semver,pattern={{major}} type=sha - - name: Build and push Docker image - uses: docker/build-push-action@v6 - with: - context: . - file: ./Dockerfile - platforms: linux/amd64,linux/arm64 - push: ${{ github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository }} - tags: ${{ steps.meta.outputs.tags }} - labels: ${{ steps.meta.outputs.labels }} - build-args: | - VERSION=${{ steps.version.outputs.version }} - BASE_PATH= - cache-from: type=registry,ref=ghcr.io/${{ github.repository_owner }}/${{ steps.repo_name.outputs.name }}/mediamanager:buildcache - cache-to: type=registry,ref=ghcr.io/${{ github.repository_owner }}/${{ steps.repo_name.outputs.name }}/mediamanager:buildcache,mode=max - no-cache: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.repository }} \ No newline at end of file + - name: Create manifest list and push + working-directory: /tmp/digests + run: | + docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \ + $(printf 'ghcr.io/${{ github.repository_owner }}/${{ steps.repo_name.outputs.name }}/mediamanager@sha256:%s ' *) + + - name: Inspect image + run: | + docker buildx imagetools inspect ghcr.io/${{ github.repository_owner }}/${{ steps.repo_name.outputs.name }}/mediamanager:${{ steps.version.outputs.version }} \ No newline at end of file diff --git a/.github/workflows/build-push-metadata_relay.yml b/.github/workflows/build-push-metadata_relay.yml index 3571d71..aefe7f9 100644 --- a/.github/workflows/build-push-metadata_relay.yml +++ b/.github/workflows/build-push-metadata_relay.yml @@ -19,16 +19,21 @@ jobs: lint-code: name: Lint Python Code runs-on: ubuntu-latest - steps: - uses: actions/checkout@v4 - uses: astral-sh/ruff-action@v3 with: src: "./metadata_relay" - build-and-push: + build: needs: lint-code runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + platform: + - linux/amd64 + - linux/arm64 permissions: contents: read packages: write @@ -41,6 +46,10 @@ jobs: id: repo_name run: echo "name=$(echo '${{ github.event.repository.name }}' | tr '[:upper:]' '[:lower:]')" >> $GITHUB_OUTPUT + - name: Prepare platform slug + id: platform_slug + run: echo "slug=${{ matrix.platform }}" | tr '/' '-' >> $GITHUB_OUTPUT + - name: Set up QEMU uses: docker/setup-qemu-action@v3 @@ -68,11 +77,88 @@ jobs: fi echo "version=$VERSION" >> $GITHUB_OUTPUT - - name: Extract metadata (tags, labels) for Docker + - name: Extract metadata (labels only) id: meta uses: docker/metadata-action@v5 with: - images: ghcr.io/${{ github.repository_owner }}/${{ github.event.repository.name }}/metadata_relay + images: ghcr.io/${{ github.repository_owner }}/${{ steps.repo_name.outputs.name }}/metadata_relay + + - name: Build and push by digest + id: build + uses: docker/build-push-action@v6 + with: + context: ./metadata_relay + file: ./metadata_relay/Dockerfile + platforms: ${{ matrix.platform }} + labels: ${{ steps.meta.outputs.labels }} + outputs: type=image,name=ghcr.io/${{ github.repository_owner }}/${{ steps.repo_name.outputs.name }}/metadata_relay,push-by-digest=true,name-canonical=true,push=${{ github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository }} + cache-from: type=registry,ref=ghcr.io/${{ github.repository_owner }}/${{ steps.repo_name.outputs.name }}/mediamanager:buildcache-${{ steps.platform_slug.outputs.slug }} + cache-to: ${{ (github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository) && format('type=registry,ref=ghcr.io/{0}/{1}/mediamanager:buildcache-{2},mode=max', github.repository_owner, steps.repo_name.outputs.name, steps.platform_slug.outputs.slug) || '' }} + no-cache: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.repository }} + build-args: | + VERSION=${{ steps.version.outputs.version }} + + - name: Export digest + run: | + mkdir -p /tmp/digests + digest="${{ steps.build.outputs.digest }}" + touch "/tmp/digests/${digest#sha256:}" + + - name: Upload digest + uses: actions/upload-artifact@v4 + with: + name: digests-relay-${{ strategy.job-index }} + path: /tmp/digests/* + if-no-files-found: error + retention-days: 1 + + merge: + needs: build + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository + steps: + - name: Download digests + uses: actions/download-artifact@v4 + with: + pattern: digests-relay-* + path: /tmp/digests + merge-multiple: true + + - name: Set repository name to lowercase + id: repo_name + run: echo "name=$(echo '${{ github.event.repository.name }}' | tr '[:upper:]' '[:lower:]')" >> $GITHUB_OUTPUT + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Log in to GitHub Container Registry + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract version + id: version + run: | + if [[ "${{ github.event_name }}" == "release" ]]; then + VERSION="${{ github.event.release.tag_name }}" + elif [[ "${{ github.ref_type }}" == "tag" ]]; then + VERSION="${{ github.ref_name }}" + else + DATE_STAMP=$(date -u +'%Y.%m.%d') + VERSION="dev-${DATE_STAMP}-${{ github.run_number }}" + fi + echo "version=$VERSION" >> $GITHUB_OUTPUT + + - name: Extract metadata (tags) + id: meta + uses: docker/metadata-action@v5 + with: + images: ghcr.io/${{ github.repository_owner }}/${{ steps.repo_name.outputs.name }}/metadata_relay tags: | type=raw,value=latest,enable=${{ github.event_name == 'release' }} type=raw,value=${{ steps.version.outputs.version }},enable=${{ github.event_name != 'release' }} @@ -83,17 +169,12 @@ jobs: type=semver,pattern={{major}} type=sha - - name: Build and push Docker image - uses: docker/build-push-action@v6 - with: - context: ./metadata_relay - file: ./metadata_relay/Dockerfile - platforms: linux/amd64,linux/arm64 - push: ${{ github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository }} - tags: ${{ steps.meta.outputs.tags }} - labels: ${{ steps.meta.outputs.labels }} - cache-from: type=registry,ref=ghcr.io/${{ github.repository_owner }}/${{ steps.repo_name.outputs.name }}/mediamanager:buildcache - cache-to: type=registry,ref=ghcr.io/${{ github.repository_owner }}/${{ steps.repo_name.outputs.name }}/mediamanager:buildcache,mode=max - no-cache: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.repository }} - build-args: | - VERSION=${{ steps.meta.outputs.version }} + - name: Create manifest list and push + working-directory: /tmp/digests + run: | + docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \ + $(printf 'ghcr.io/${{ github.repository_owner }}/${{ steps.repo_name.outputs.name }}/metadata_relay@sha256:%s ' *) + + - name: Inspect image + run: | + docker buildx imagetools inspect ghcr.io/${{ github.repository_owner }}/${{ steps.repo_name.outputs.name }}/metadata_relay:${{ steps.version.outputs.version }} \ No newline at end of file