Skip to content

Commit

Permalink
Use docker buildx to create multi-arch image (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
AverageMarcus authored Aug 10, 2020
1 parent 05cd593 commit 985fec7
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 11 deletions.
63 changes: 57 additions & 6 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,61 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Build and push Docker images
uses: docker/build-push-action@v1.1.0

- name: Prepare
id: prepare
run: |
DOCKER_IMAGE=averagemarcus/kube-image-prefetch
DOCKER_PLATFORMS=linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64,linux/386,linux/ppc64le,linux/s390x
VERSION=latest
if [[ $GITHUB_REF == refs/tags/* ]]; then
VERSION=${GITHUB_REF#refs/tags/v}
fi
TAGS="--tag ${DOCKER_IMAGE}:${VERSION}"
if [[ $VERSION =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
TAGS="$TAGS --tag ${DOCKER_IMAGE}:latest"
fi
echo ::set-output name=tags::${TAGS}
echo ::set-output name=platforms::${DOCKER_PLATFORMS}
- name: Set up Docker Buildx
uses: crazy-max/ghaction-docker-buildx@v3

- name: Cache Docker layers
uses: actions/cache@v2
id: cache
with:
username: averagemarcus
password: ${{ secrets.DOCKER_TOKEN }}
repository: averagemarcus/kube-image-prefetch
tag_with_ref: true
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Docker Buildx (build)
run: |
docker buildx build \
--cache-from "type=local,src=/tmp/.buildx-cache" \
--cache-to "type=local,dest=/tmp/.buildx-cache" \
--platform ${{ steps.prepare.outputs.platforms }} \
--output "type=image,push=false" \
${{ steps.prepare.outputs.tags }} \
.
- name: Docker Login
env:
DOCKER_USERNAME: averagemarcus
DOCKER_PASSWORD: ${{ secrets.DOCKER_TOKEN }}
run: |
echo "${DOCKER_PASSWORD}" | docker login --username "${DOCKER_USERNAME}" --password-stdin
- name: Docker Buildx (push)
run: |
docker buildx build \
--cache-from "type=local,src=/tmp/.buildx-cache" \
--cache-to "type=local,dest=/tmp/.buildx-cache" \
--platform ${{ steps.prepare.outputs.platforms }} \
--output "type=image,push=true" \
${{ steps.prepare.outputs.tags }} \
.
8 changes: 5 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
FROM golang:1.14-alpine AS builder
FROM --platform=${BUILDPLATFORM:-linux/amd64} tonistiigi/xx:golang AS xgo
FROM --platform=${BUILDPLATFORM:-linux/amd64} golang:1.14-alpine AS builder
COPY --from=xgo / /
WORKDIR /app/
ADD . .
RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags="-w -s" -o kube-image-prefetch main.go
RUN CGO_ENABLED=0 go build -ldflags="-w -s" -o kube-image-prefetch main.go

FROM scratch
FROM --platform=${TARGETPLATFORM:-linux/amd64} scratch
WORKDIR /app/
COPY --from=builder /app/kube-image-prefetch /app/kube-image-prefetch
ENTRYPOINT ["/app/kube-image-prefetch"]
15 changes: 13 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

IMAGE ?= averagemarcus/kube-image-prefetch:latest

export DOCKER_CLI_EXPERIMENTAL=enabled

.PHONY: test # Run all tests, linting and format checks
test: lint check-format run-tests

Expand Down Expand Up @@ -31,11 +33,20 @@ build: lint check-format fetch-deps

.PHONY: docker-build # Build the docker image
docker-build:
@docker build -t $(IMAGE) .
@docker buildx create --use --name=crossplat --node=crossplat && \
docker buildx build \
--output "type=docker,push=false" \
--tag $(IMAGE) \
.

.PHONY: docker-publish # Push the docker image to the remote registry
docker-publish:
@docker push $(IMAGE)
@docker buildx create --use --name=crossplat --node=crossplat && \
docker buildx build \
--platform linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64,linux/ppc64le,linux/s390x \
--output "type=image,push=true" \
--tag $(IMAGE) \
.

.PHONY: run # Run the application
run:
Expand Down

0 comments on commit 985fec7

Please sign in to comment.