docker / build-push-action

GitHub Action to build and push Docker images with Buildx
https://github.com/marketplace/actions/build-and-push-docker-images
Apache License 2.0
4.11k stars 526 forks source link

When using a matrix for multi-platform builds images get erased #1051

Closed andy5995 closed 4 months ago

andy5995 commented 4 months ago

Contributing guidelines

I've found a bug, and:

Description

When I specify platforms in a matrix in a GitHub workflow, and use the variable in the "platforms" field, each time a job in the matrix completes and an image is pushed, the previous one for a different architecture is erased from the repo in Docker Hub

Expected behaviour

The images shouldn't get erased, but added to the repo.

Actual behaviour

Described in description

Repository URL

https://github.com/andy5995/linuxdeploy-build-helper

Workflow run URL

https://github.com/andy5995/linuxdeploy-build-helper/actions/runs/7814118868

YAML workflow

name: linuxdeploy dependencies docker image
concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}
  cancel-in-progress: true

on:
  push:
    branches: trunk
    paths:
    - 'dependencies.dockerfile'
    - '**workflows/deps-linuxdeploy-docker.yml'
  workflow_dispatch:
  schedule:
    - cron: '30 11 17 * *'

jobs:
  build-dependencies-image:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        platform: ["linux/amd64", "linux/arm64", "linux/arm/v7"]
      fail-fast: false
    steps:
      -
        name: Checkout
        uses: actions/checkout@v4
        with:
          submodules: true

      - if: ${{ matrix.platform != 'linux/amd64' }}
        name: Set up QEMU
        uses: docker/setup-qemu-action@v3
      -
        name: Login to Docker Hub
        uses: docker/login-action@v3
        with:
          username: andy5995
          password: ${{ secrets.UNCLE_ANDY_DOCKERHUB_ACCESS_TOKEN }}
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      -
        name: Build and push
        uses: docker/build-push-action@v5
        with:
          context: .
          file: ./dependencies.dockerfile
          platforms: ${{ matrix.platform }}
          push: true
          tags: andy5995/linuxdeploy:dependencies-latest

Workflow logs

logs_79.zip

BuildKit logs

No response

Additional info

I've previously inquired about this in the Docker support forum and here on the Discussions.

Here you can see there is no drop-down box for each arch I tried building, only an image for linux/arm/v7 the last job in the matrix to complete:

image

andy5995 commented 4 months ago

I see now I didn't include all the steps https://docs.docker.com/build/ci/github-actions/multi-platform/ I'll try that and close the issue after successful completion.

andy5995 commented 4 months ago

After adding the required sections, it's working now.

I did have several failures though even after following all the docs, but I opened a ticket for it on the docs repo:

https://github.com/docker/docs/issues/19339

shink commented 1 month ago

@andy5995 Hi friend! I got the same issue. Could you please have a look at my code? Thanks so much!

I don't use the outputs param. Maybe there is something wrong here:

      - name: Build and push Docker image
        id: build-push
        uses: docker/build-push-action@v5
        with:
          context: ./docker/${{ matrix.image }}
          file: ./docker/${{ matrix.image }}/Dockerfile
          push: ${{ github.event_name != 'pull_request' }}. # only push when releasd
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          platforms: ${{ matrix.platform }}
          build-args: |
            PLATFORM=${{ matrix.platform }}
job ```yaml docker: name: build-and-push-image runs-on: ubuntu-latest needs: - prepare strategy: fail-fast: false matrix: registry: - name: Docker Hub url: docker.io owner: cosdt - name: GHCR url: ghcr.io owner: ${{ github.repository_owner }} platform: - linux/amd64 - linux/arm64 image: ${{ fromJson(needs.prepare.outputs.images) }} steps: - name: Checkout uses: actions/checkout@v4 - name: Extract metadata (tags, labels) for Docker id: meta uses: docker/metadata-action@v5 with: images: | ${{ matrix.registry.url }}/${{ matrix.registry.owner }}/${{ matrix.image }} tags: | type=schedule type=ref,event=branch type=ref,event=pr type=semver,pattern={{version}} type=semver,pattern={{major}}.{{minor}} type=semver,pattern={{major}} labels: | org.opencontainers.image.title=${{ matrix.image }} org.opencontainers.image.description=${{ matrix.image }} built by ${{ matrix.registry.url }}/${{ matrix.registry.owner }} org.opencontainers.image.vendor=${{ matrix.registry.owner }} - name: Show image metadata run: | echo "version: ${{ steps.meta.outputs.version }}" echo "tags: ${{ steps.meta.outputs.tags }}" echo "labels: ${{ steps.meta.outputs.labels }}" echo "annotations: ${{ steps.meta.outputs.annotations }}" - name: Set up QEMU uses: docker/setup-qemu-action@v3 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - name: Login to the Docker Hub if: ${{ github.event_name != 'pull_request' && matrix.registry.name == 'Docker Hub' }} uses: docker/login-action@v3 with: registry: ${{ matrix.registry.url }} username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} - name: Login to the GitHub Container if: ${{ github.event_name != 'pull_request' && matrix.registry.name == 'GHCR' }} uses: docker/login-action@v3 with: registry: ${{ matrix.registry.url }} username: ${{ github.repository_owner }} password: ${{ secrets.GITHUB_TOKEN }} - name: Build and push Docker image id: build-push uses: docker/build-push-action@v5 with: context: ./docker/${{ matrix.image }} file: ./docker/${{ matrix.image }}/Dockerfile push: ${{ github.event_name != 'pull_request' }} tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} platforms: ${{ matrix.platform }} build-args: | PLATFORM=${{ matrix.platform }} ```