Skip to content

Instantly share code, notes, and snippets.

@skandragon
Created May 10, 2022 08:48
Show Gist options
  • Save skandragon/b5625a885fb5de8fccfa0f6798355e17 to your computer and use it in GitHub Desktop.
Save skandragon/b5625a885fb5de8fccfa0f6798355e17 to your computer and use it in GitHub Desktop.
#
# Copyright 2021-2022 OpsMx, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License")
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# Install the latest versions of our mods. This is done as a separate step
# so it will pull from an image cache if possible, unless there are changes.
#
FROM golang:1.18-alpine AS buildmod
RUN apk update && apk upgrade && apk add gcc musl-dev && rm -rf /var/cache/apk/*
RUN mkdir /build
WORKDIR /build
COPY go.mod .
COPY go.sum .
RUN go mod download
#
# Compile the code.
#
FROM buildmod AS build-binaries
COPY . .
ARG TARGETOS
ARG TARGETARCH
RUN mkdir /out
RUN CGO_ENABLED=1 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -ldflags="-s -w" -o /out/clouddriver cmd/clouddriver/*.go
#
# Establish a base OS image used by all the applications.
#
FROM alpine:3 AS base-image
RUN apk update && apk upgrade && apk add ca-certificates curl jq && rm -rf /var/cache/apk/*
RUN update-ca-certificates
RUN mkdir /local /local/ca-certificates && rm -rf /usr/local/share/ca-certificates && ln -s /local/ca-certificates /usr/local/share/ca-certificates
COPY docker/run.sh /app/run.sh
ENTRYPOINT ["/bin/sh", "/app/run.sh"]
#
# Build the clouddriver image. This should be a --target on docker build.
#
FROM base-image AS clouddriver-image
WORKDIR /app
COPY --from=build-binaries /out/clouddriver /app
ARG GIT_BRANCH
ENV GIT_BRANCH=${GIT_BRANCH}
ARG GIT_HASH
ENV GIT_HASH=${GIT_HASH}
EXPOSE 7002
CMD ["/app/clouddriver"]
#
# Copyright 2021-2022 OpsMx, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License")
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
TARGETS=test local
PLATFORM=linux/amd64,linux/arm64
BUILDX=docker buildx build --pull --platform ${PLATFORM}
IMAGE_PREFIX=docker.flame.org/library/
#
# Build targets. Adding to these will cause magic to occur.
#
# These are the targets for Docker images, used both for the multi-arch and
# single (local) Docker builds.
# Dockerfiles should have a target that ends in -image, e.g. clouddriver-image.
IMAGE_TARGETS = clouddriver
#
# Below here lies magic...
#
all_deps := $(shell find * -name '*.go' | grep -v _test)
now := $(shell date -u +%Y%m%dT%H%M%S)
#
# Default target.
#
.PHONY: all
all: ${TARGETS}
#
# make a buildtime directory to hold the build timestamp files
buildtime:
[ ! -d buildtime ] && mkdir buildtime
#
# Build locally, mostly for development speed.
#
.PHONY: local
local: $(addprefix bin/,$(BINARIES))
bin/%:: ${all_deps}
@[ -d bin ] || mkdir bin
go build -ldflags="-s -w" -o $@ app/$(@F)/*.go
#
# Multi-architecture image builds
#
.PHONY: images
images: buildtime $(addsuffix .ts, $(addprefix buildtime/,$(IMAGE_TARGETS)))
buildtime/%.ts:: ${all_deps} docker/Dockerfile.multi
@$(eval GIT_BRANCH=$(shell git branch --show-current))
@$(eval GIT_HASH=$(shell git rev-parse ${GIT_BRANCH}))
${BUILDX} \
--tag ${IMAGE_PREFIX}go-$(patsubst %.ts,%,$(@F)):latest \
--tag ${IMAGE_PREFIX}go-$(patsubst %.ts,%,$(@F)):v${now} \
--target $(patsubst %.ts,%,$(@F))-image \
--build-arg GIT_HASH=${GIT_HASH} \
--build-arg GIT_BRANCH=${GIT_BRANCH} \
-f docker/Dockerfile.multi \
--push .
@touch $@
#
# Test targets
#
.PHONY: test
test:
go test -race ./...
#
# Clean the world.
#
.PHONY: clean
clean:
rm -f buildtime/*.ts
rm -f bin/*
.PHONY: really-clean
really-clean: clean
#!/bin/sh
update-ca-certificates
exec $*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment