|
| 1 | +# Written using |
| 2 | +# https://docs.docker.com/build/ci/github-actions/multi-platform/ |
| 3 | +# and |
| 4 | +# https://github.com/sredevopsorg/multi-arch-docker-github-workflow/blob/main/.github/workflows/multi-build.yaml |
1 | 5 | name: CI |
2 | 6 |
|
3 | 7 | on: |
|
8 | 12 | - '*' |
9 | 13 | pull_request: |
10 | 14 |
|
| 15 | +env: |
| 16 | + # The name of the Docker image to be built and pushed to GHCR |
| 17 | + # The image name is derived from the GitHub repository name and the GitHub Container Registry (GHCR) URL. |
| 18 | + # The image name will be in the format: ghcr.io/<owner>/<repo> |
| 19 | + GHCR_IMAGE: ghcr.io/${{ github.repository }} |
| 20 | + |
11 | 21 | jobs: |
12 | 22 |
|
13 | | - build_container: |
14 | | - runs-on: ubuntu-latest |
| 23 | + build: |
| 24 | + |
| 25 | + strategy: |
| 26 | + fail-fast: false |
| 27 | + matrix: |
| 28 | + include: |
| 29 | + - arch: amd64 |
| 30 | + runner: ubuntu-latest |
| 31 | + - arch: arm64 |
| 32 | + runner: ubuntu-24.04-arm |
| 33 | + runs-on: ${{ matrix.runner }} |
15 | 34 |
|
16 | 35 | steps: |
17 | | - - name: Checkout |
18 | | - uses: actions/checkout@v6 |
19 | | - with: |
20 | | - # Need this to get version number from last tag |
21 | | - fetch-depth: 0 |
| 36 | + - name: Lowercase the image name |
| 37 | + run: echo "GHCR_IMAGE=${GHCR_IMAGE,,}" >> $GITHUB_ENV |
22 | 38 |
|
23 | 39 | - name: Set up Docker Buildx |
24 | | - id: buildx |
25 | 40 | uses: docker/setup-buildx-action@v3 |
26 | 41 |
|
27 | | - - name: Log in to GitHub Docker Registry |
28 | | - if: github.event_name != 'pull_request' |
29 | | - uses: docker/login-action@v3 |
| 42 | + - name: Extract metadata for publishing image |
| 43 | + id: meta |
| 44 | + uses: docker/metadata-action@v5 |
30 | 45 | with: |
31 | | - registry: ghcr.io |
32 | | - username: ${{ github.actor }} |
33 | | - password: ${{ secrets.GITHUB_TOKEN }} |
| 46 | + images: ${{ env.GHCR_IMAGE }} |
34 | 47 |
|
35 | 48 | - name: Build and export to Docker local cache |
36 | 49 | uses: docker/build-push-action@v6 |
37 | | - env: |
38 | | - DOCKER_BUILD_RECORD_UPLOAD: false |
39 | 50 | with: |
40 | | - context: . |
41 | 51 | # Need load and tags so we can test it below |
42 | 52 | load: true |
43 | 53 | tags: tag_for_testing |
44 | 54 |
|
45 | 55 | - name: Test cli works in cached runtime image |
46 | 56 | run: docker run --rm tag_for_testing uvx pycowsay 'hello ubuntu-devcontainer!' |
47 | 57 |
|
| 58 | + - name: Log in to GitHub Docker Registry |
| 59 | + if: github.ref_type == 'tag' |
| 60 | + uses: docker/login-action@v3 |
| 61 | + with: |
| 62 | + registry: ghcr.io |
| 63 | + username: ${{ github.actor }} |
| 64 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 65 | + |
| 66 | + - name: Build and push by digest |
| 67 | + if: github.ref_type == 'tag' |
| 68 | + id: build |
| 69 | + uses: docker/build-push-action@v6 |
| 70 | + with: |
| 71 | + labels: ${{ steps.meta.outputs.labels }} |
| 72 | + annotations: ${{ steps.meta.outputs.annotations }} |
| 73 | + outputs: type=image,name=${{ env.GHCR_IMAGE }},push-by-digest=true,name-canonical=true,push=true,oci-mediatypes=true |
| 74 | + |
| 75 | + - name: Export digest |
| 76 | + if: github.ref_type == 'tag' |
| 77 | + run: | |
| 78 | + mkdir -p ${{ runner.temp }}/digests |
| 79 | + digest="${{ steps.build.outputs.digest }}" |
| 80 | + touch "${{ runner.temp }}/digests/${digest#sha256:}" |
| 81 | +
|
| 82 | + - name: Upload digest |
| 83 | + if: github.ref_type == 'tag' |
| 84 | + uses: actions/upload-artifact@v4 |
| 85 | + with: |
| 86 | + name: digests-linux-${{ matrix.arch }} |
| 87 | + path: ${{ runner.temp }}/digests/* |
| 88 | + if-no-files-found: error |
| 89 | + |
| 90 | + merge: |
| 91 | + runs-on: ubuntu-latest |
| 92 | + needs: |
| 93 | + - build |
| 94 | + if: github.ref_type == 'tag' |
| 95 | + |
| 96 | + steps: |
| 97 | + - name: Lowercase the image name |
| 98 | + run: echo "GHCR_IMAGE=${GHCR_IMAGE,,}" >> $GITHUB_ENV |
| 99 | + |
| 100 | + - name: Download digests |
| 101 | + uses: actions/download-artifact@v4 |
| 102 | + with: |
| 103 | + path: ${{ runner.temp }}/digests |
| 104 | + pattern: digests-* |
| 105 | + merge-multiple: true |
| 106 | + |
| 107 | + - name: Set up Docker Buildx |
| 108 | + uses: docker/setup-buildx-action@v3 |
| 109 | + |
48 | 110 | - name: Create tags for publishing image |
49 | 111 | id: meta |
50 | 112 | uses: docker/metadata-action@v5 |
51 | 113 | with: |
52 | | - images: ghcr.io/${{ github.repository }} |
| 114 | + images: ${{ env.GHCR_IMAGE }} |
53 | 115 | tags: | |
54 | 116 | type=ref,event=tag |
55 | 117 | type=raw,value=latest |
56 | 118 | type=raw,value=noble |
57 | | - |
58 | | - - name: Push cached image to container registry |
59 | | - if: github.ref_type == 'tag' |
60 | | - uses: docker/build-push-action@v6 |
61 | | - env: |
62 | | - DOCKER_BUILD_RECORD_UPLOAD: false |
63 | | - # This does not build the image again, it will find the image in the |
64 | | - # Docker cache and publish it |
| 119 | +
|
| 120 | + - name: Log in to GitHub Docker Registry |
| 121 | + uses: docker/login-action@v3 |
65 | 122 | with: |
66 | | - context: . |
67 | | - push: true |
68 | | - tags: ${{ steps.meta.outputs.tags }} |
69 | | - labels: ${{ steps.meta.outputs.labels }} |
| 123 | + registry: ghcr.io |
| 124 | + username: ${{ github.actor }} |
| 125 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 126 | + |
| 127 | + - name: Create manifest list and push |
| 128 | + working-directory: ${{ runner.temp }}/digests |
| 129 | + run: | |
| 130 | + docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \ |
| 131 | + $(printf '${{ env.GHCR_IMAGE }}@sha256:%s ' *) |
| 132 | +
|
| 133 | + - name: Inspect image |
| 134 | + run: | |
| 135 | + docker buildx imagetools inspect ${{ env.GHCR_IMAGE }}:${{ steps.meta.outputs.version }} |
0 commit comments