Skip to content

Commit a644e71

Browse files
authored
Using Docker GitHub action (#3)
* Var username * new test * tag * repo names only * ${{ needs.docker_merge.outputs.tag }} * image-tag * multi-arch-go
1 parent 5af2b1d commit a644e71

File tree

2 files changed

+70
-19
lines changed

2 files changed

+70
-19
lines changed

.github/workflows/image-build.yml

Lines changed: 68 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,49 +13,100 @@ on:
1313
- main
1414

1515
env:
16-
USER: ${{secrets.DOCKER_USER}}
17-
IMAGE_NAME: docker-test
16+
IMAGE_NAME: multi-arch-go
1817

1918
jobs:
2019
docker_build:
2120
runs-on: ${{ matrix.os }}
2221
strategy:
22+
fail-fast: false
2323
matrix:
2424
include:
2525
- os: ${{vars.BASE_OS}}
26-
arch: amd64
26+
platform: linux/amd64
2727
- os: ${{vars.BASE_OS}}-arm
28-
arch: arm64
28+
platform: linux/arm64
2929
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}}
3239
- name: Log in to Docker Hub
3340
uses: docker/login-action@v3
3441
with:
35-
username: ${{env.USER}}
42+
username: ${{vars.DOCKER_USER}}
3643
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
3855
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+
4167

42-
docker_manifest:
68+
docker_merge:
4369
needs: docker_build
4470
runs-on: ${{vars.BASE_OS}}-arm
4571
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
4678
- name: Log in to Docker Hub
4779
uses: docker/login-action@v3
4880
with:
49-
username: ${{env.USER}}
81+
username: ${{vars.DOCKER_USER}}
5082
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
52102
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 }}
55106

56107
docker_test:
57108
if: ${{github.event.inputs.run_tests == 'true' || github.event.inputs.run_tests == null}}
58-
needs: docker_manifest
109+
needs: docker_merge
59110
runs-on: ${{ matrix.os }}
60111
strategy:
61112
matrix:
@@ -69,7 +120,7 @@ jobs:
69120
shell: bash
70121
run: |
71122
# 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 }}
73124
sleep 5 # Wait for the container to start
74125
75126
# Check the output

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@
3434
3. In the left sidebar, select **Secrets and variables** > **Actions**.
3535
4. Click the **New repository secret** button.
3636
5. Add the following secrets:
37-
- `DOCKER_USER`: Your Docker username.
3837
- `DOCKER_PAT`: Your Docker Personal Access Token we generated above.
3938
6. Move from the **Secrets** tab to the **Variables** tab
4039
7. Click the **New repository variable** button.
41-
8. Add the following variable:
40+
8. Add the following variable:3
41+
- `DOCKER_USER`: Your Docker username.
4242
- `BASE_OS`: `ubuntu-24.04`
4343

4444
### Run the GitHub Actions Workflow

0 commit comments

Comments
 (0)