From 55b9efc5f6a583baaeb789dbf83a59b9315f4abc Mon Sep 17 00:00:00 2001 From: Stefanuk12 <42220813+Stefanuk12@users.noreply.github.com> Date: Mon, 28 Apr 2025 19:06:48 +0100 Subject: [PATCH] refactor: use matrix for build --- .github/workflows/release.yaml | 128 ++++++++++++++++++++++++++------- 1 file changed, 104 insertions(+), 24 deletions(-) diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index eb5c039..b62a5a3 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -14,7 +14,7 @@ jobs: version: ${{ steps.extract_version.outputs.value }} registry_version: ${{ steps.extract_registry_version.outputs.value }} registry_image: "ghcr.io/${{ steps.repository_owner.outputs.lowercase }}/registry" - registry_published: ${{ steps.registry_published.outputs.tag }} + registry_published: ${{ steps.registry_published.outputs.tag || 'not found' }} found: ${{ steps.ensure_not_published.outputs.FOUND }} steps: - uses: actions/checkout@v4 @@ -42,6 +42,7 @@ jobs: - name: Get Docker image publish status uses: tyriis/docker-image-tag-exists@v2.1.0 id: registry_published + continue-on-error: true with: registry: ghcr.io repository: "${{ steps.repository_owner.outputs.lowercase }}/registry" @@ -180,18 +181,37 @@ jobs: prerelease: ${{ steps.is_prerelease.outputs.value }} files: artifacts/* - publish-registry-image: - name: Build and publish Docker image - runs-on: ubuntu-latest + build-registry-image: + name: Build Docker image needs: [ prepare ] if: ${{ needs.prepare.outputs.registry_published == 'not found' }} permissions: contents: read packages: write + strategy: + fail-fast: false + matrix: + include: + - os: ubuntu-latest + platform: linux/amd64 + - os: ubuntu-24.04-arm + platform: linux/arm64 + runs-on: ${{ matrix.os }} steps: - name: Checkout repository uses: actions/checkout@v3 + - name: Prepare + run: | + platform=${{ matrix.platform }} + echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV + + - name: Docker meta + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ needs.prepare.outputs.registry_image }} + - name: Log in to the Container registry uses: docker/login-action@v3 with: @@ -199,22 +219,6 @@ jobs: username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - - name: Set Tags - id: set_tags - shell: bash - run: | - TAGS=$({ - echo "${{ needs.prepare.outputs.registry_image }}:${{ github.sha }}" - echo "${{ needs.prepare.outputs.registry_image }}:${${{ github.sha }}:0:7}" - echo "${{ needs.prepare.outputs.registry_image }}:${{ needs.prepare.outputs.registry_version }}" - if [[ "${{ needs.prepare.outputs.registry_version }}" != *"-"* ]]; then - echo "${{ needs.prepare.outputs.registry_image }}:latest" - fi - }) - echo "tags<> $GITHUB_OUTPUT - echo "$TAGS" >> $GITHUB_OUTPUT - echo "EOF" >> $GITHUB_OUTPUT - - name: Set up QEMU uses: docker/setup-qemu-action@v3 @@ -222,9 +226,85 @@ jobs: uses: docker/setup-buildx-action@v3 - name: Build and push Docker image + id: build uses: docker/build-push-action@v6 with: - context: . - platforms: linux/amd64,linux/arm64 - push: true - tags: ${{ steps.set_tags.outputs.tags }} \ No newline at end of file + platforms: ${{ matrix.platform }} + labels: ${{ steps.meta.outputs.labels }} + tags: ${{ needs.prepare.outputs.registry_image }} + outputs: type=image,push-by-digest=true,name-canonical=true,push=true + + - name: Export digest + run: | + mkdir -p ${{ runner.temp }}/digests + digest="${{ steps.build.outputs.digest }}" + touch "${{ runner.temp }}/digests/${digest#sha256:}" + + - name: Upload digest + uses: actions/upload-artifact@v4 + with: + name: digests-${{ env.PLATFORM_PAIR }} + path: ${{ runner.temp }}/digests/* + if-no-files-found: error + retention-days: 1 + + publish-registry-image: + name: Publish Docker image + runs-on: ubuntu-latest + needs: + - prepare + - build-registry-image + steps: + - name: Download digests + uses: actions/download-artifact@v4 + with: + path: ${{ runner.temp }}/digests + pattern: digests-* + merge-multiple: true + + - name: Login to Docker Hub + uses: docker/login-action@v3 + with: + registry: https://ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Docker meta + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ needs.prepare.outputs.registry_image }} + tags: | + type=ref,event=branch + type=ref,event=pr + type=semver,pattern={{version}} + type=semver,pattern={{major}}.{{minor}} + + - name: Set Tags + id: set_tags + shell: bash + run: | + GITHUB_SHORT_SHA=${GITHUB_SHA:0:7} + TAGS=$({ + echo "-t ${{ needs.prepare.outputs.registry_image }}:${{ github.sha }}"; + echo "-t ${{ needs.prepare.outputs.registry_image }}:${GITHUB_SHORT_SHA}"; + echo "-t ${{ needs.prepare.outputs.registry_image }}:${{ needs.prepare.outputs.version }}"; + if [[ "${{ needs.prepare.outputs.version }}" != *"-"* ]]; then + echo "-t ${{ needs.prepare.outputs.registry_image }}:latest"; + fi; + } | paste -sd " " -) + echo "tags=$TAGS" >> $GITHUB_OUTPUT + + - name: Create manifest list and push + working-directory: ${{ runner.temp }}/digests + run: | + docker buildx imagetools create ${{ steps.set_tags.outputs.tags }} \ + $(printf '${{ needs.prepare.outputs.registry_image }}@sha256:%s ' *) + + - name: Inspect image + run: | + docker buildx imagetools inspect ${{ needs.prepare.outputs.registry_image }}:${{ needs.prepare.outputs.registry_version }} + \ No newline at end of file