docker 使用GitHub工作流从不同文件构建amd64和arm64

oewdyzsn  于 2023-04-05  发布在  Docker
关注(0)|答案(1)|浏览(245)

为多平台设置GitHub工作流的正确方法是什么(amd 64 + arm 64)Docker镜像构建--我有两个不同的docker文件,每个平台一个(参见MapLibre/Martin项目),我试图用一个矩阵来配置平台和文件参数,但由于某种原因,它发布了带有linux/arm64元数据的linux/amd64镜像,另一个镜像有unknown/unknown

jobs:
  docker:
    name: Build ${{ matrix.platform }} docker image
    strategy:
      fail-fast: true
      matrix:
        include:
          - platform: linux/amd64
            file: Dockerfile
          - platform: linux/arm64
            file: arm64.Dockerfile
    runs-on: ubuntu-latest
    steps:

      - name: Checkout sources
        uses: actions/checkout@v3

      - name: Docker meta
        id: docker_meta
        uses: docker/metadata-action@v4
        with:
          images: ghcr.io/maplibre/martin

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2.1.0
        with:
          platforms: linux/arm64,linux/amd64

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2.5.0
        with:
          install: true
          platforms: ${{ matrix.platform }}

      - name: Build the Docker image
        id: docker_build
        uses: docker/build-push-action@v4
        with:
          file: ${{ matrix.file }}
          push: false
          load: true
          tags: ${{ steps.docker_meta.outputs.tags }}
          labels: ${{ steps.docker_meta.outputs.labels }}
          platforms: ${{ matrix.platform }}

      - name: Login to GitHub Docker registry
        uses: docker/login-action@v2
        if: ${{ github.actor != 'dependabot[bot]' && !github.event.pull_request.head.repo.fork }}
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Push the Docker image
        if: ${{ github.actor != 'dependabot[bot]' && github.event_name != 'pull_request' }}
        uses: docker/build-push-action@v4
        with:
          file: ${{ matrix.file }}
          push: true
          load: false
          tags: ${{ steps.docker_meta.outputs.tags }}
          labels: ${{ steps.docker_meta.outputs.labels }}
          platforms: ${{ matrix.platform }}
iezvtpos

iezvtpos1#

您不需要使用strategy: matrix: ...来进行多平台构建。如果您可以将两个Dockerfile集成到一个Dockerfile中,则会更容易。将--platform=$BUILDPLATFORM添加到Dockerfile的第一行FROM ...

FROM --platform=$BUILDPLATFORM rust:alpine as builder

你应该尝试在同一组基础镜像上为你的arm和amd版本所用,如果你不能,那么它会变得更复杂。
在构建步骤中,使用一系列平台:

- name: Build Docker images
      uses: docker/build-push-action@v4
      with:
        platforms:
           - linux/amd64
           - linux/arm64

你会得到这样的结果:

jobs:
  docker:
    name: Build docker images
    runs-on: ubuntu-latest
    steps:

      - name: Checkout sources
        uses: actions/checkout@v3

      - name: Docker meta
        id: docker_meta
        uses: docker/metadata-action@v4
        with:
          images: ghcr.io/maplibre/martin

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2.5.0

      - name: Login to GitHub Docker registry
        uses: docker/login-action@v2
        if: ${{ github.actor != 'dependabot[bot]' && !github.event.pull_request.head.repo.fork }}
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Push the Docker image
        if: ${{ github.actor != 'dependabot[bot]' && github.event_name != 'pull_request' }}
        uses: docker/build-push-action@v4
        with:
          push: true
          tags: ${{ steps.docker_meta.outputs.tags }}
          labels: ${{ steps.docker_meta.outputs.labels }}
          platforms: 
             - linux/arm64
             - linux/amd64

相关问题