Skip to content

Commit

Permalink
Add Multi-Arch Builds (#5)
Browse files Browse the repository at this point in the history
* dockerfile and makefile changes

* updated to circleci

* install buildx and revert to 1.13 circleci build image

* revert to hashicorp docker hub repos
  • Loading branch information
jcolemorrison authored Jun 29, 2021
1 parent c3bb72f commit 06b98f2
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 15 deletions.
44 changes: 36 additions & 8 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ jobs:
name: test application
command: go test -v ./...
- run:
name: build application (linux)
command: CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o bin/public-api
name: build application (linux amd64)
command: CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o bin/amd64/public-api
- run:
name: build application (linux arm64)
command: CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o bin/arm64/public-api
- persist_to_workspace:
root: /go/src/github.com/hashicorp-demoapp
paths:
Expand All @@ -39,27 +42,52 @@ jobs:
command: cd functional_test && shipyard test



publish-docker-release:
docker:
- image: circleci/golang:1.13
environment:
GO111MODULE: "on"
DOCKER_BUILDKIT: 1
BUILDX_PLATFORMS: linux/amd64,linux/arm64
working_directory: /go/src/github.com/hashicorp-demoapp/public-api
steps:
- setup_remote_docker
- attach_workspace:
at: /go/src/github.com/hashicorp-demoapp
- run:
name: install docker buildx
command: |
BUILDX_BINARY_URL="https://github.com/docker/buildx/releases/download/v0.4.2/buildx-v0.4.2.linux-amd64"
curl --output docker-buildx \
--silent --show-error --location --fail --retry 3 \
"$BUILDX_BINARY_URL"
mkdir -p ~/.docker/cli-plugins
mv docker-buildx ~/.docker/cli-plugins/
chmod a+x ~/.docker/cli-plugins/docker-buildx
docker buildx install
# Run binfmt
docker run --rm --privileged tonistiigi/binfmt:latest --install "$BUILDX_PLATFORMS"
docker context create multi_arch_build
# Create Builder
docker buildx create --use multi_arch_build
- run:
name: docker login
command: docker login -u ${DOCKER_USER} -p ${DOCKER_PASS}
- run:
name: docker build
command: |
docker build -t hashicorpdemoapp/public-api:${CIRCLE_TAG} .
- run:
name: docker push
name: docker buildx build and push
command: |
docker push hashicorpdemoapp/public-api:${CIRCLE_TAG}
docker buildx build --platform linux/amd64,linux/arm64 \
-t hashicorpdemoapp/public-api:${CIRCLE_TAG} \
--push \
.
publish-github-release:
docker:
Expand Down
17 changes: 15 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
FROM alpine:latest
FROM alpine:latest as base

RUN addgroup api && \
adduser -D -G api api

RUN mkdir /app
COPY ./bin/public-api /app/public-api

# Setup AMD64
FROM base as image-amd64
COPY ./bin/amd64/public-api /app/public-api
RUN chmod +x /app/public-api

# Setup ARM64
FROM base as image-arm64
COPY ./bin/arm64/public-api /app/public-api
RUN chmod +x /app/public-api

FROM image-${TARGETARCH}

RUN echo "Running on $BUILDPLATFORM platform, building for $TARGETPLATFORM"

USER api

Expand Down
20 changes: 15 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION=v0.0.3
VERSION=v0.0.4
REPOSITORY=hashicorpdemoapp/public-api

.PHONY: auth
Expand All @@ -11,15 +11,25 @@ generate:
run:
go run main.go

build_linux:
CGO_ENABLED=0 GOOS=linux go build -o ./bin/public-api main.go
build_linux_amd64:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ./bin/amd64/public-api main.go

build_docker: build_linux
build_linux_arm64:
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o ./bin/arm64/public-api main.go

build_docker: build_linux_amd64 build_linux_arm64
docker build -t ${REPOSITORY}:${VERSION} .

build_docker_dev: build_linux
build_docker_dev: build_linux_amd64 build_linux_arm64
docker build -t ${REPOSITORY}:dev .

buildx_docker: build_linux_amd64 build_linux_arm64
docker buildx create --use --name multi_arch_build || true
docker buildx build --platform linux/amd64,linux/arm64 \
-t ${REPOSITORY}:${VERSION} \
--push \
.

run_functional_tests: build_docker_dev
# First copy the config to a volume, this is needed for CircleCI
docker create -v data.volume:/config --name dummy alpine /bin/true
Expand Down

0 comments on commit 06b98f2

Please sign in to comment.