|
13 | 13 | - main
|
14 | 14 |
|
15 | 15 | env:
|
16 |
| - USER: ${{secrets.DOCKER_USER}} |
17 |
| - IMAGE_NAME: docker-test |
| 16 | + IMAGE_NAME: multi-arch-go |
18 | 17 |
|
19 | 18 | jobs:
|
20 | 19 | docker_build:
|
21 | 20 | runs-on: ${{ matrix.os }}
|
22 | 21 | strategy:
|
| 22 | + fail-fast: false |
23 | 23 | matrix:
|
24 | 24 | include:
|
25 | 25 | - os: ${{vars.BASE_OS}}
|
26 |
| - arch: amd64 |
| 26 | + platform: linux/amd64 |
27 | 27 | - os: ${{vars.BASE_OS}}-arm
|
28 |
| - arch: arm64 |
| 28 | + platform: linux/arm64 |
29 | 29 | steps:
|
30 |
| - - name: Checkout |
31 |
| - uses: actions/checkout@v4 |
| 30 | + - name: Prepare |
| 31 | + run: | |
| 32 | + platform=${{ matrix.platform }} |
| 33 | + echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV |
| 34 | + - name: Extract Docker image metadata |
| 35 | + id: meta |
| 36 | + uses: docker/metadata-action@v5 |
| 37 | + with: |
| 38 | + images: ${{ vars.DOCKER_USER }}/${{env.IMAGE_NAME}} |
32 | 39 | - name: Log in to Docker Hub
|
33 | 40 | uses: docker/login-action@v3
|
34 | 41 | with:
|
35 |
| - username: ${{env.USER}} |
| 42 | + username: ${{vars.DOCKER_USER}} |
36 | 43 | password: ${{secrets.DOCKER_PAT}}
|
37 |
| - - name: Build and push |
| 44 | + - name: Set up Docker Buildx |
| 45 | + uses: docker/setup-buildx-action@v3 |
| 46 | + - name: Build and push by digest |
| 47 | + id: build |
| 48 | + uses: docker/build-push-action@v6 |
| 49 | + with: |
| 50 | + platforms: ${{ matrix.platform }} |
| 51 | + labels: ${{ steps.meta.outputs.labels }} |
| 52 | + tags: ${{vars.DOCKER_USER}}/${{env.IMAGE_NAME}} |
| 53 | + outputs: type=image,push-by-digest=true,name-canonical=true,push=true |
| 54 | + - name: Export digest |
38 | 55 | run: |
|
39 |
| - docker build --tag ${{env.USER}}/${{env.IMAGE_NAME}}:${{matrix.arch}} . |
40 |
| - docker push ${{env.USER}}/${{env.IMAGE_NAME}}:${{matrix.arch}} |
| 56 | + mkdir -p ${{ runner.temp }}/digests |
| 57 | + digest="${{ steps.build.outputs.digest }}" |
| 58 | + touch "${{ runner.temp }}/digests/${digest#sha256:}" |
| 59 | + - name: Upload digest |
| 60 | + uses: actions/upload-artifact@v4 |
| 61 | + with: |
| 62 | + name: digests-${{ env.PLATFORM_PAIR }} |
| 63 | + path: ${{ runner.temp }}/digests/* |
| 64 | + if-no-files-found: error |
| 65 | + retention-days: 1 |
| 66 | + |
41 | 67 |
|
42 |
| - docker_manifest: |
| 68 | + docker_merge: |
43 | 69 | needs: docker_build
|
44 | 70 | runs-on: ${{vars.BASE_OS}}-arm
|
45 | 71 | steps:
|
| 72 | + - name: Download digests |
| 73 | + uses: actions/download-artifact@v4 |
| 74 | + with: |
| 75 | + path: ${{ runner.temp }}/digests |
| 76 | + pattern: digests-* |
| 77 | + merge-multiple: true |
46 | 78 | - name: Log in to Docker Hub
|
47 | 79 | uses: docker/login-action@v3
|
48 | 80 | with:
|
49 |
| - username: ${{env.USER}} |
| 81 | + username: ${{vars.DOCKER_USER}} |
50 | 82 | password: ${{secrets.DOCKER_PAT}}
|
51 |
| - - name: Create and push manifest |
| 83 | + - name: Docker meta |
| 84 | + id: meta |
| 85 | + uses: docker/metadata-action@v5 |
| 86 | + with: |
| 87 | + images: ${{vars.DOCKER_USER}}/${{env.IMAGE_NAME}} |
| 88 | + tags: | |
| 89 | + type=raw,value=latest,enable={{is_default_branch}} |
| 90 | + type=ref,event=branch |
| 91 | + type=ref,event=pr |
| 92 | + type=semver,pattern={{version}} |
| 93 | + type=semver,pattern={{major}}.{{minor}} |
| 94 | +
|
| 95 | + - name: Create manifest list and push |
| 96 | + working-directory: ${{ runner.temp }}/digests |
| 97 | + run: | |
| 98 | + docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \ |
| 99 | + $(printf '${{vars.DOCKER_USER}}/${{env.IMAGE_NAME}}@sha256:%s ' *) |
| 100 | +
|
| 101 | + - name: Inspect image |
52 | 102 | run: |
|
53 |
| - docker manifest create ${{env.USER}}/${{env.IMAGE_NAME}} ${{env.USER}}/${{env.IMAGE_NAME}}:amd64 ${{env.USER}}/${{env.IMAGE_NAME}}:arm64 |
54 |
| - docker manifest push ${{env.USER}}/${{env.IMAGE_NAME}} |
| 103 | + docker buildx imagetools inspect ${{vars.DOCKER_USER}}/${{env.IMAGE_NAME}}:${{ steps.meta.outputs.version }} |
| 104 | + outputs: |
| 105 | + image-tag: ${{ steps.meta.outputs.version }} |
55 | 106 |
|
56 | 107 | docker_test:
|
57 | 108 | if: ${{github.event.inputs.run_tests == 'true' || github.event.inputs.run_tests == null}}
|
58 |
| - needs: docker_manifest |
| 109 | + needs: docker_merge |
59 | 110 | runs-on: ${{ matrix.os }}
|
60 | 111 | strategy:
|
61 | 112 | matrix:
|
|
69 | 120 | shell: bash
|
70 | 121 | run: |
|
71 | 122 | # Run our container
|
72 |
| - docker run -d --name ${{env.IMAGE_NAME}} --rm ${{env.USER}}/${{env.IMAGE_NAME}}:latest |
| 123 | + docker run -d --name ${{env.IMAGE_NAME}} --rm ${{vars.DOCKER_USER}}/${{env.IMAGE_NAME}}:${{ needs.docker_merge.outputs.image-tag }} |
73 | 124 | sleep 5 # Wait for the container to start
|
74 | 125 |
|
75 | 126 | # Check the output
|
|
0 commit comments