diff --git a/.circleci/config.yml b/.circleci/config.yml index 4ccedf9..981787a 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -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: @@ -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: diff --git a/Dockerfile b/Dockerfile index 011f79a..cd81c18 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/Makefile b/Makefile index b9a4854..dd5d813 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -VERSION=v0.0.3 +VERSION=v0.0.4 REPOSITORY=hashicorpdemoapp/public-api .PHONY: auth @@ -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