fix(Makefile, Dockerfile): optimization of the container image building process

This commit is contained in:
Markus Pesch 2020-01-09 21:50:27 +01:00
parent f0bba4a202
commit a507bef108
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982
3 changed files with 135 additions and 37 deletions

1
.dockerignore Normal file
View File

@ -0,0 +1 @@
bin

View File

@ -1,3 +1,20 @@
FROM busybox:latest # ARGs
COPY bin/linux/amd64/flucky /usr/bin/flucky # ==================================
ENTRYPOINT [ "/usr/bin/flucky" ] ARG BASE_IMAGE
ARG BUILD_IMAGE
ARG EXECUTABLE
ARG EXECUTABLE_TARGET
ARG GOPROXY
ARG GOPRIVATE
ARG VERSION
# BUILD
# ==================================
FROM ${BUILD_IMAGE} AS build
COPY . /workspace
RUN make ${EXECUTABLE_TARGET} VERSION=${VERSION} GOPROXY=${GOPROXY} GOPRIVATE=${GOPRIVATE}
# TARGET
# ==================================
FROM ${BASE_IMAGE}
COPY --from=build /workspace/${EXECUTABLE_TARGET} /usr/bin/${EXECUTABLE}

148
Makefile
View File

@ -1,24 +1,16 @@
# VERSION/RELEASE # VERSION/RELEASE
# If no version is specified as a parameter of make, the last git hash # If no version is specified as a parameter of make, the last git hash
# value is taken. # value is taken.
VERSION:=$(shell git describe --abbrev=0)+$(shell date +'%Y%m%d%H%I%S') VERSION?=$(shell git describe --abbrev=0)+$(shell date +'%Y%m%d%H%I%S')
RELEASE?=1 RELEASE?=1
# CONTAINER_IMAGE_VERSION # GOPROXY settings
# Defines the version of the container image which has included the executable # If no GOPROXY environment variable available, the pre-defined GOPROXY from go
# binary. This is somethimes different to the variable VERSION, because the # env to download and validate go modules is used. Exclude downloading and
# container image version should be latest instead contains the latest git tag # validation of all private modules which are pre-defined in GOPRIVATE. If no
# and hash sum. # GOPRIVATE variable defined, the variable of go env is used.
CONTAINER_IMAGE_VERSION:=latest GOPROXY?=$(shell go env GOPROXY)
GOPRIVATE?=$(shell go env GOPRIVATE)
# CONTAINER_RUNTIME/BUILD_IMAGE
# The CONTAINER_RUNTIME variable will be used to specified the path to a
# container runtime. This is needed to start and run a container image defined
# by the BUILD_IMAGE variable. The BUILD_IMAGE container serve as build
# environment to execute the different make steps inside. Therefore, the bulid
# environment requires all necessary dependancies to build this project.
CONTAINER_RUNTIME?=$(shell which docker)
BUILD_IMAGE:=volkerraschek/build-image:latest
# EXECUTABLE # EXECUTABLE
# Executable binary which should be compiled for different architecures # Executable binary which should be compiled for different architecures
@ -69,11 +61,38 @@ EXECUTABLES:=\
EXECUTABLE_TARGETS:= \ EXECUTABLE_TARGETS:= \
${UNIX_EXECUTABLE_TARGETS} ${UNIX_EXECUTABLE_TARGETS}
# CONTAINER_RUNTIME / BUILD_IMAGE
# The CONTAINER_RUNTIME variable will be used to specified the path to a
# container runtime. This is needed to start and run a container image defined
# by the BUILD_IMAGE variable.
CONTAINER_RUNTIME?=$(shell which docker)
# BUILD_IMAGE
# The BUILD_IMAGE container serve as build environment to execute the different
# make steps inside. Therefore, the bulid environment requires all necessary
# dependancies to build this project.
BUILD_IMAGE:=docker.io/volkerraschek/build-image:latest
# BASE_IMAGE
# Defines the name of the container base image on which should be built the new
# CONTAINER_IMAGE.
BASE_IMAGE=docker.io/library/alpine:3.11.2
# REGISTRY / CONTAINER_IMAGE
# Specification of the container registry. This is needed to build and push the
# container image on a container registry. Additionally, the CONTAINER_IMAGE
# variable is composed based on the REGISTRY_NAMESPACE, the CONTAINER_IMAGE_NAME
# and the CONTAINER_IMAGE_VERSION
REGISTRY_MIRROR:=docker.io
REGISTRY_NAMESPACE:=volkerraschek
CONTAINER_IMAGE_NAME:=${EXECUTABLE}
CONTAINER_IMAGE_VERSION:=latest
CONTAINER_IMAGE=${REGISTRY_NAMESPACE}/${CONTAINER_IMAGE_NAME}/${CONTAINER_IMAGE_VERSION:v%=%}
# BINARIES # BINARIES
# ============================================================================== # ==============================================================================
# current os # current os
${EXECUTABLE}: bindata ${EXECUTABLE}: bin/tmp/${EXECUTABLE}
CGO_ENABLED=0 go build -ldflags "-X main.version=${VERSION}" -o ${@}
# build all binaries # build all binaries
PHONY:=all PHONY:=all
@ -81,32 +100,84 @@ all: ${EXECUTABLE_TARGETS}
# darwin os # darwin os
bin/darwin/386/${EXECUTABLE}: bindata bin/darwin/386/${EXECUTABLE}: bindata
CGO_ENABLED=0 GOARCH=386 GOOS=darwin go build -ldflags "-X main.version=${VERSION}" -o ${@} CGO_ENABLED=0 \
GOOS=darwin \
GOARCH=386 \
GOPROXY=${GOPROXY} \
GOPRIVATE=${GOPRIVATE} \
go build -ldflags "-X main.version=${VERSION:v%=%}" -o ${@}
bin/darwin/amd64/${EXECUTABLE}: bindata bin/darwin/amd64/${EXECUTABLE}: bindata
CGO_ENABLED=0 GOARCH=amd64 GOOS=darwin go build -ldflags "-X main.version=${VERSION}" -o ${@} CGO_ENABLED=0 \
GOOS=darwin \
GOARCH=amd64 \
GOPROXY=${GOPROXY} \
GOPRIVATE=${GOPRIVATE} \
go build -ldflags "-X main.version=${VERSION:v%=%}" -o ${@}
# freebsd os # freebsd os
bin/freebsd/amd64/${EXECUTABLE}: bindata bin/freebsd/amd64/${EXECUTABLE}: bindata
CGO_ENABLED=0 GOARCH=amd64 GOOS=freebsd go build -ldflags "-X main.version=${VERSION}" -o ${@} CGO_ENABLED=0 \
GOOS=freebsd \
GOARCH=amd64 \
GOPROXY=${GOPROXY} \
GOPRIVATE=${GOPRIVATE} \
go build -ldflags "-X main.version=${VERSION:v%=%}" -o ${@}
# linux os # linux os
bin/linux/386/${EXECUTABLE}: bindata bin/linux/386/${EXECUTABLE}: bindata
CGO_ENABLED=0 GOARCH=386 GOOS=linux go build -ldflags "-X main.version=${VERSION}" -o ${@} CGO_ENABLED=0 \
GOOS=linux \
GOARCH=386 \
GOPROXY=${GOPROXY} \
GOPRIVATE=${GOPRIVATE} \
go build -ldflags "-X main.version=${VERSION:v%=%}" -o ${@}
bin/linux/amd64/${EXECUTABLE}: bindata bin/linux/amd64/${EXECUTABLE}: bindata
CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build -ldflags "-X main.version=${VERSION}" -o ${@} CGO_ENABLED=0 \
GOOS=linux \
GOARCH=amd64 \
GOPROXY=${GOPROXY} \
GOPRIVATE=${GOPRIVATE} \
go build -ldflags "-X main.version=${VERSION:v%=%}" -o ${@}
bin/linux/arm/5/${EXECUTABLE}: bindata bin/linux/arm/5/${EXECUTABLE}: bindata
CGO_ENABLED=0 GOARM=5 GOARCH=arm GOOS=linux go build -ldflags "-X main.version=${VERSION}" -o ${@} CGO_ENABLED=0 \
GOOS=linux \
GOARCH=arm \
GOARM=5 \
GOPROXY=${GOPROXY} \
GOPRIVATE=${GOPRIVATE} \
go build -ldflags "-X main.version=${VERSION:v%=%}" -o ${@}
bin/linux/arm/7/${EXECUTABLE}: bindata bin/linux/arm/7/${EXECUTABLE}: bindata
CGO_ENABLED=0 GOARM=7 GOARCH=arm GOOS=linux go build -ldflags "-X main.version=${VERSION}" -o ${@} CGO_ENABLED=0 \
GOOS=linux \
GOARCH=arm \
GOARM=7 \
GOPROXY=${GOPROXY} \
GOPRIVATE=${GOPRIVATE} \
go build -ldflags "-X main.version=${VERSION:v%=%}" -o ${@}
bin/tmp/${EXECUTABLE}: bindata
CGO_ENABLED=0 \
GOPROXY=${GOPROXY} \
GOPRIVATE=${GOPRIVATE} \
go build -ldflags "-X main.version=${VERSION:v%=%}" -o ${@}
# GO-BINDATA # GO-BINDATA
# ============================================================================== # ==============================================================================
bindata: BINDATA_TARGETS := \
pkg/storage/db/bindataSQL.go \
test/goldenfiles/bindata.go
PHONY+=bindata
bindata: ${BINDATA_TARGETS}
pkg/storage/db/bindataSQL.go:
go-bindata -pkg db -o ./pkg/storage/db/bindataSQL.go ./pkg/storage/db/sql/*** ./pkg/storage/db/sql/psql/schema/*** go-bindata -pkg db -o ./pkg/storage/db/bindataSQL.go ./pkg/storage/db/sql/*** ./pkg/storage/db/sql/psql/schema/***
test/goldenfiles/bindata.go:
go-bindata -pkg goldenfiles -ignore ".*\.go" -o ./test/goldenfiles/bindata.go ./test/goldenfiles/*** go-bindata -pkg goldenfiles -ignore ".*\.go" -o ./test/goldenfiles/bindata.go ./test/goldenfiles/***
# TEST # TEST
@ -125,7 +196,7 @@ test-update-all: ${EXECUTABLE}
--exec-file "$<:/usr/bin/${EXECUTABLE}" \ --exec-file "$<:/usr/bin/${EXECUTABLE}" \
--dir "systemd:/usr/lib/systemd/system" \ --dir "systemd:/usr/lib/systemd/system" \
--license "Apache 2.0" \ --license "Apache 2.0" \
--version ${VERSION} \ --version ${VERSION:v%=%} \
--release ${RELEASE} \ --release ${RELEASE} \
--out ${@} \ --out ${@} \
${EXECUTABLE} ${EXECUTABLE}
@ -134,21 +205,30 @@ test-update-all: ${EXECUTABLE}
# ============================================================================== # ==============================================================================
PHONY+=clean PHONY+=clean
clean: clean:
rm ${EXECUTABLE} || true rm --force ${BINDATA_TARGETS} || true
rm ${EXECUTABLE}.* || true rm --force --recursive bin/ || true
rm --recursive --force bin/ || true
# CONTAINER IMAGE STEPS # CONTAINER IMAGE STEPS
# ============================================================================== # ==============================================================================
PHONY+=container-image/build/amd64 PHONY+=container-image/build/amd64
container-image/build/amd64: bin/linux/amd64/${EXECUTABLE} container-image/build/amd64:
${CONTAINER_RUNTIME} build \ ${CONTAINER_RUNTIME} build \
--tag volkerraschek/${EXECUTABLE}:${IMAGE_VERSION} \ --build-arg BASE_IMAGE=${BASE_IMAGE} \
--build-arg BUILD_IMAGE=${BUILD_IMAGE} \
--build-arg EXECUTABLE=${EXECUTABLE} \
--build-arg EXECUTABLE_TARGET=bin/linux/amd64/${EXECUTABLE} \
--build-arg GOPROXY=${GOPROXY} \
--build-arg GOPRIVATE=${GOPRIVATE} \
--build-arg VERSION=${VERSION} \
--file Dockerfile \
--no-cache \
--tag ${REGISTRY_MIRROR}/${CONTAINER_IMAGE} \
--tag ${CONTAINER_IMAGE} \
. .
PHONY+=container-image/push/amd64 PHONY+=container-image/push/amd64
container-image/push/amd64: container-image/build/amd64 container-image/push/amd64: container-image/build/amd64
${CONTAINER_RUNTIME} push volkerraschek/${EXECUTABLE}:${IMAGE_VERSION} ${CONTAINER_RUNTIME} push ${REGISTRY_MIRROR}/${CONTAINER_IMAGE}
# CONTAINER STEPS - BINARY # CONTAINER STEPS - BINARY
# ============================================================================== # ==============================================================================
@ -197,7 +277,7 @@ container-run:
--volume ${PWD}:/workspace \ --volume ${PWD}:/workspace \
${BUILD_IMAGE} \ ${BUILD_IMAGE} \
make ${COMMAND} \ make ${COMMAND} \
VERSION=${VERSION} \ VERSION=${VERSION:v%=%} \
RELEASE=${RELEASE} RELEASE=${RELEASE}
# REMOTE # REMOTE