diff --git a/.github/workflows/build-docker-image.yml b/.github/workflows/build-docker-image.yml index 89f9385..d4178b1 100644 --- a/.github/workflows/build-docker-image.yml +++ b/.github/workflows/build-docker-image.yml @@ -3,8 +3,8 @@ name: Docker Image Builder on: workflow_call: inputs: - platform: - description: 'The platform to build the image for' + platforms: + description: 'The platforms to build the image for' required: true type: string path: @@ -22,8 +22,8 @@ on: required: false default: '' type: string - images: - description: 'The images to pass to the Docker metadata action' + image: + description: 'The image to pass to the Docker metadata action' required: true type: string tags: @@ -45,9 +45,13 @@ on: required: false jobs: - build-image: + build: name: Build Docker image runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + platform: ${{ fromJson( inputs.platforms ) }} env: docker-token: ${{ secrets.DOCKERHUB_TOKEN }} steps: @@ -57,22 +61,20 @@ jobs: id: meta uses: docker/metadata-action@v5 with: - images: ${{ inputs.images }} - tags: ${{ inputs.tags }} - flavor: ${{ inputs.flavor }} + images: ${{ inputs.image }} - name: Get first tag id: first-tag run: | echo -e ${{ toJson(steps.meta.outputs.tags) }} | { mapfile -t; echo "tag=${MAPFILE[0]}" >> $GITHUB_OUTPUT; echo "First tag: ${MAPFILE[0]}"; } - name: Login to Docker Hub uses: docker/login-action@v3 - if: env.docker-token != '' + if: "!contains( inputs.image, 'ghcr.io' )" with: username: ${{ vars.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} - name: Login to GHCR uses: docker/login-action@v3 - if: contains( inputs.images, 'ghcr.io' ) + if: "contains( inputs.image, 'ghcr.io' )" with: registry: ghcr.io username: ${{ github.actor }} @@ -86,14 +88,69 @@ jobs: [worker.oci] max-parallelism = 2 - name: Build and push + id: build uses: docker/build-push-action@v5 with: build-args: ${{ inputs.build-args }} file: ${{ inputs.file }} context: ${{ inputs.path }} - push: true - tags: ${{ steps.meta.outputs.tags }} + #push: true + #tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} - platforms: ${{ inputs.platform }} + platforms: ${{ matrix.platform }} + outputs: type=image,push-by-digest=true,push=true,name-canonical=true,name=${{ inputs.image }} + - 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@v3 + with: + name: digests + path: /tmp/digests/* + if-no-files-found: error + retention-days: 1 outputs: tag: ${{ steps.first-tag.outputs.tag }} + merge: + name: Merge manifests + runs-on: ubuntu-latest + needs: + - build + steps: + - name: Download digests + uses: actions/download-artifact@v3 + with: + name: digests + path: /tmp/digests + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + - name: Docker meta + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ inputs.image }} + tags: ${{ inputs.tags }} + flavor: ${{ inputs.flavor }} + - name: Login to Docker Hub + uses: docker/login-action@v3 + if: "!contains( inputs.image, 'ghcr.io' )" + with: + username: ${{ vars.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + - name: Login to GHCR + uses: docker/login-action@v3 + if: "contains( inputs.image, 'ghcr.io' )" + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + - 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 '${{ inputs.image }}@sha256:%s ' *) + - name: Inspect image + run: | + docker buildx imagetools inspect ${{ inputs.image }}:${{ steps.meta.outputs.version }} diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index c1a7f9e..66123fd 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -24,19 +24,31 @@ jobs: outputs: matrix: ${{ steps.set-matrix.outputs.matrix }} - build-base-image: - name: Build base image + build-base-image-docker-hub: + name: Build base image for Docker hub needs: enumerate-configs uses: ./.github/workflows/build-docker-image.yml - strategy: - fail-fast: false - matrix: - platform: ${{ fromJson(vars.DOCKER_PLATFORMS) }} + if: ${{ github.event_name != 'pull_request' }} + with: + platforms: ${{ vars.DOCKER_PLATFORMS }} + image: ngreatorex/homie-streamdeck + tags: | + type=ref,event=branch + type=ref,event=pr + type=semver,pattern={{version}} + type=semver,pattern={{major}}.{{minor}} + type=semver,pattern={{major}} + type=sha + secrets: inherit + + build-base-image-ghcr: + name: Build base image for GitHub Container Registry + needs: enumerate-configs + uses: ./.github/workflows/build-docker-image.yml + if: ${{ github.event_name == 'pull_request' }} with: - platform: ${{ matrix.platform }} - images: | - name=ngreatorex/homie-streamdeck,enable=${{ github.event_name != 'pull_request' }} - name=ghcr.io/${{ github.actor }}/homie-streamdeck,enable=${{ github.event_name == 'pull_request' }} + platforms: ${{ vars.DOCKER_PLATFORMS }} + image: ghcr.io/${{ github.actor }}/homie-streamdeck tags: | type=ref,event=branch type=ref,event=pr @@ -51,18 +63,17 @@ jobs: #if: github.event_name != 'pull_request' needs: - enumerate-configs - - build-base-image + - build-base-image-docker-hub + - build-base-image-ghcr strategy: fail-fast: false matrix: - platform: ${{ fromJson(vars.DOCKER_PLATFORMS) }} config-name: ${{ fromJson(needs.enumerate-configs.outputs.matrix) }} uses: ./.github/workflows/build-docker-image.yml with: build-args: | - FROM_IMAGE=${{ needs.build-base-image.outputs.tag }} - images: | - name=ghcr.io/${{ github.actor }}/homie-streamdeck-configured + FROM_IMAGE=${{ needs.build-base-image-docker-hub.outputs.tag || needs.build-base-image-ghcr.outputs.tag }} + image: ghcr.io/${{ github.actor }}/homie-streamdeck-configured flavor: | suffix=_${{ matrix.config-name }} tags: | @@ -72,6 +83,6 @@ jobs: type=semver,pattern={{major}}.{{minor}} type=semver,pattern={{major}} type=sha - platform: ${{ matrix.platform }} + platforms: ${{ vars.DOCKER_PLATFORMS }} path: config/ file: config/Dockerfile.${{ matrix.config-name }}