Skip to content

Commit 109681a

Browse files
committed
Use docker-buildx to build multi-platform images
1 parent a09bc70 commit 109681a

File tree

3 files changed

+55
-24
lines changed

3 files changed

+55
-24
lines changed

.github/workflows/docker-publish.yml

+43-20
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ jobs:
2323
runs-on: ubuntu-latest
2424

2525
steps:
26-
- uses: actions/checkout@v2
26+
- name: Checkout code
27+
uses: actions/checkout@v2
2728

28-
- name: Run tests
29-
run: make
29+
- name: Test build
30+
run: make build
3031

3132
# Push image to GitHub Packages.
3233
# See also https://docs.docker.com/docker-hub/builds/
@@ -42,18 +43,8 @@ jobs:
4243
packages: write
4344

4445
steps:
45-
- uses: actions/checkout@v2
46-
47-
- name: Build app
48-
run: make build
49-
50-
- name: Build image
51-
run: docker build . --file Dockerfile --tag $IMAGE_NAME --label "runnumber=${GITHUB_RUN_ID}"
52-
53-
- name: Log into registry
54-
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
55-
56-
- name: Push image
46+
- name: Set up environment
47+
id: setup
5748
run: |
5849
IMAGE_ID=ghcr.io/${{ github.repository_owner }}/$IMAGE_NAME
5950
@@ -69,8 +60,40 @@ jobs:
6960
# Use Docker `latest` tag convention
7061
[ "$VERSION" == "master" ] && VERSION=latest
7162
72-
echo IMAGE_ID=$IMAGE_ID
73-
echo VERSION=$VERSION
74-
75-
docker tag $IMAGE_NAME $IMAGE_ID:$VERSION
76-
docker push $IMAGE_ID:$VERSION
63+
TAGS="${IMAGE_ID}:${VERSION}"
64+
65+
echo ::set-output name=tags::${TAGS}
66+
67+
- name: Checkout code
68+
uses: actions/checkout@v2
69+
70+
- name: Set up QEMU
71+
id: qemu
72+
uses: docker/setup-qemu-action@v1
73+
with:
74+
platforms: arm64
75+
76+
- name: Set up Docker Buildx
77+
id: buildx
78+
uses: docker/setup-buildx-action@v1
79+
with:
80+
install: true
81+
82+
- name: List available platforms
83+
run: echo ${{ steps.buildx.outputs.platforms }}
84+
85+
- name: Login to GitHub Container Registry
86+
uses: docker/login-action@v1
87+
with:
88+
registry: ghcr.io
89+
username: ${{ github.repository_owner }}
90+
password: ${{ secrets.GITHUB_TOKEN }}
91+
92+
- name: Build and push
93+
uses: docker/build-push-action@v2
94+
with:
95+
context: .
96+
file: ./Dockerfile
97+
platforms: linux/amd64,linux/arm64
98+
tags: ${{ steps.setup.outputs.tags }}
99+
push: true

Dockerfile

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1+
FROM golang:1.17 AS builder
2+
3+
WORKDIR /go/src/github.com/rhobs/prometheus-example-app
4+
COPY . /go/src/github.com/rhobs/prometheus-example-app
5+
6+
RUN make build
7+
18
FROM quay.io/prometheus/busybox:latest
29

3-
ADD prometheus-example-app /bin/prometheus-example-app
10+
COPY --from=builder /go/src/github.com/rhobs/prometheus-example-app/prometheus-example-app \
11+
/bin/prometheus-example-app
412

513
ENTRYPOINT ["/bin/prometheus-example-app"]

Makefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ LDFLAGS="-X main.appVersion=$(VERSION)"
66

77
.PHONY : all build image
88

9-
all: build image
9+
all: build
1010

1111
build:
1212
CGO_ENABLED=0 go build -ldflags=$(LDFLAGS) -o prometheus-example-app --installsuffix cgo main.go
1313

14-
image: build
15-
docker build -t ghcr.io/rhobs/$(IMAGE_NAME):$(VERSION) .
14+
image:
15+
docker build -t "ghcr.io/rhobs/$(IMAGE_NAME):$(VERSION)" .

0 commit comments

Comments
 (0)