build(ci): enhanced multi-platform build distribution example

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2023-05-12 11:31:13 +02:00
parent 7079dc4572
commit 6aa26f53ae
No known key found for this signature in database
GPG Key ID: 3248E46B6BB8C7F7
1 changed files with 45 additions and 43 deletions

View File

@ -58,18 +58,15 @@ In the previous example, each platform is built on the same runner which can
take a long time depending on the number of platforms and your Dockerfile. take a long time depending on the number of platforms and your Dockerfile.
To solve this issue you can use a matrix strategy to distribute the build for To solve this issue you can use a matrix strategy to distribute the build for
each platform across multiple runners and merge the resulting manifests into a each platform across multiple runners and create manifest list using the
single image using the [`buildx imagetools create` command](../../../engine/reference/commandline/buildx_imagetools_create.md). [`buildx imagetools create` command](../../../engine/reference/commandline/buildx_imagetools_create.md).
The following workflow will build the image for each platform on a dedicated The following workflow will build the image for each platform on a dedicated
runner using a matrix strategy and upload the resulting images as artifacts. runner using a matrix strategy and push by digest. Then, the `merge` job will
Then, the `push` job will download the images, load them into the local Docker create a manifest list and push it to Docker Hub.
daemon, push them to a temporary local registry so Buildx can create a manifest
list and push it to Docker Hub. It will copy the images from the local
registry to Docker Hub as well.
Using a local registry is useful, so we don't create unecessary tags for each This example also uses the [`metadata` action](https://github.com/docker/metadata-action)
platform on the remote registry. Only `user/app:latest` will be created. to set tags and labels.
{% raw %} {% raw %}
```yaml ```yaml
@ -81,9 +78,7 @@ on:
- "main" - "main"
env: env:
TMP_LOCAL_IMAGE: localhost:5000/user/app
REGISTRY_IMAGE: user/app REGISTRY_IMAGE: user/app
REGISTRY_TAG: latest
jobs: jobs:
build: build:
@ -101,12 +96,11 @@ jobs:
name: Checkout name: Checkout
uses: actions/checkout@v3 uses: actions/checkout@v3
- -
name: Prepare name: Docker meta
run: | id: meta
mkdir -p /tmp/images uses: docker/metadata-action@v4
platform=${{ matrix.platform }} with:
echo "TARFILE=${platform//\//-}.tar" >> $GITHUB_ENV images: ${{ env.REGISTRY_IMAGE }}
echo "TAG=${{ env.TMP_LOCAL_IMAGE }}:${platform//\//-}" >> $GITHUB_ENV
- -
name: Set up QEMU name: Set up QEMU
uses: docker/setup-qemu-action@v2 uses: docker/setup-qemu-action@v2
@ -114,48 +108,55 @@ jobs:
name: Set up Docker Buildx name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2 uses: docker/setup-buildx-action@v2
- -
name: Build name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
-
name: Build and push by digest
id: build
uses: docker/build-push-action@v4 uses: docker/build-push-action@v4
with: with:
context: . context: .
platforms: ${{ matrix.platform }} platforms: ${{ matrix.platform }}
tags: ${{ env.TAG }} labels: ${{ steps.meta.outputs.labels }}
outputs: type=docker,dest=/tmp/images/${{ env.TARFILE }} outputs: type=image,name=${{ env.REGISTRY_IMAGE }},push-by-digest=true,name-canonical=true,push=true
- -
name: Upload 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 uses: actions/upload-artifact@v3
with: with:
name: images name: digests
path: /tmp/images/${{ env.TARFILE }} path: /tmp/digests/*
if-no-files-found: error if-no-files-found: error
retention-days: 1 retention-days: 1
push: merge:
runs-on: ubuntu-latest runs-on: ubuntu-latest
needs: needs:
- build - build
services:
registry:
image: registry:2
ports:
- 5000:5000
steps: steps:
- -
name: Download images name: Download digests
uses: actions/download-artifact@v3 uses: actions/download-artifact@v3
with: with:
name: images name: digests
path: /tmp/images path: /tmp/digests
- -
name: Load images name: Set up Docker Buildx
run: | uses: docker/setup-buildx-action@v2
for image in /tmp/images/*.tar; do
docker load -i $image
done
- -
name: Push images to local registry name: Docker meta
run: | id: meta
docker push -a ${{ env.TMP_LOCAL_IMAGE }} uses: docker/metadata-action@v4
with:
images: ${{ env.REGISTRY_IMAGE }}
- -
name: Login to Docker Hub name: Login to Docker Hub
uses: docker/login-action@v2 uses: docker/login-action@v2
@ -164,12 +165,13 @@ jobs:
password: ${{ secrets.DOCKERHUB_TOKEN }} password: ${{ secrets.DOCKERHUB_TOKEN }}
- -
name: Create manifest list and push name: Create manifest list and push
working-directory: /tmp/digests
run: | run: |
docker buildx imagetools create -t ${{ env.REGISTRY_IMAGE }}:${{ env.REGISTRY_TAG }} \ docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
$(docker image ls --format '{{.Repository}}:{{.Tag}}' '${{ env.TMP_LOCAL_IMAGE }}' | tr '\n' ' ') $(printf '${{ env.REGISTRY_IMAGE }}@sha256:%s ' *)
- -
name: Inspect image name: Inspect image
run: | run: |
docker buildx imagetools inspect ${{ env.REGISTRY_IMAGE }}:${{ env.REGISTRY_TAG }} docker buildx imagetools inspect ${{ env.REGISTRY_IMAGE }}:${{ steps.meta.outputs.version }}
``` ```
{% endraw %} {% endraw %}