Refactored docker image builder script
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
parent
8129280b08
commit
19e44b60f0
14
Dockerfile.builder
Normal file
14
Dockerfile.builder
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
# Builder
|
||||||
|
FROM golang:alpine
|
||||||
|
|
||||||
|
ARG BIN_OUTPUT=/go/bin
|
||||||
|
ARG GO_SERVER=cmd/server/main.go
|
||||||
|
ARG GO_WORKER=cmd/worker/main.go
|
||||||
|
|
||||||
|
WORKDIR /go/src/app
|
||||||
|
COPY src ./
|
||||||
|
|
||||||
|
RUN go mod download && \
|
||||||
|
export CGO_ENABLED=0 ; export GOOS=linux ; export GOARCH=amd64 && \
|
||||||
|
go build -ldflags="-w -s" -o "$BIN_OUTPUT/server" $GO_SERVER && \
|
||||||
|
go build -ldflags="-w -s" -o "$BIN_OUTPUT/worker" $GO_WORKER
|
@ -1,30 +0,0 @@
|
|||||||
# Builder
|
|
||||||
FROM golang:alpine AS builder
|
|
||||||
|
|
||||||
ARG BIN_OUTPUT=/go/bin/server
|
|
||||||
ARG GO_MAIN=cmd/server/main.go
|
|
||||||
|
|
||||||
WORKDIR /go/src/app
|
|
||||||
COPY src ./
|
|
||||||
|
|
||||||
RUN go mod download && \
|
|
||||||
export CGO_ENABLED=0 ; export GOOS=linux ; export GOARCH=amd64 && \
|
|
||||||
go build -ldflags="-w -s" -o $BIN_OUTPUT $GO_MAIN
|
|
||||||
|
|
||||||
|
|
||||||
# Destination image - server
|
|
||||||
FROM gcr.io/distroless/base-debian10
|
|
||||||
|
|
||||||
ARG BIN_OUTPUT=/go/bin/server
|
|
||||||
|
|
||||||
LABEL author="Piotr Biernat"
|
|
||||||
LABEL service="basket-svc"
|
|
||||||
LABEL vendor="Egommerce"
|
|
||||||
LABEL version="1.0"
|
|
||||||
|
|
||||||
WORKDIR /
|
|
||||||
COPY --from=builder $BIN_OUTPUT /
|
|
||||||
COPY .env.dist /.env
|
|
||||||
|
|
||||||
EXPOSE 80
|
|
||||||
ENTRYPOINT ["/server"]
|
|
22
Dockerfile.target
Normal file
22
Dockerfile.target
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
# Builder
|
||||||
|
ARG BUILDER_IMAGE="git.pbiernat.dev/egommerce/basket-builder:latest"
|
||||||
|
FROM ${BUILDER_IMAGE} AS builder
|
||||||
|
|
||||||
|
# Destination image - server
|
||||||
|
FROM gcr.io/distroless/base-debian10
|
||||||
|
|
||||||
|
ARG BIN_OUTPUT
|
||||||
|
ARG SVC_NAME
|
||||||
|
ARG SVC_VER
|
||||||
|
|
||||||
|
LABEL author="Piotr Biernat"
|
||||||
|
LABEL vendor="egommerce"
|
||||||
|
LABEL service=${SVC_NAME}
|
||||||
|
LABEL version=${SVC_VER}
|
||||||
|
|
||||||
|
WORKDIR /
|
||||||
|
COPY --from=builder $BIN_OUTPUT /app
|
||||||
|
COPY .env.dist /.env
|
||||||
|
|
||||||
|
EXPOSE 80
|
||||||
|
ENTRYPOINT ["/app"]
|
@ -1,29 +0,0 @@
|
|||||||
# Builder
|
|
||||||
FROM golang:alpine AS builder
|
|
||||||
|
|
||||||
ARG BIN_OUTPUT=/go/bin/worker
|
|
||||||
ARG GO_WORKER=cmd/worker/main.go
|
|
||||||
|
|
||||||
WORKDIR /go/src/app
|
|
||||||
COPY src ./
|
|
||||||
|
|
||||||
RUN go mod download && \
|
|
||||||
export CGO_ENABLED=0 ; export GOOS=linux ; export GOARCH=amd64 && \
|
|
||||||
go build -ldflags="-w -s" -o $BIN_OUTPUT $GO_WORKER
|
|
||||||
|
|
||||||
|
|
||||||
# Destination image - worker
|
|
||||||
FROM gcr.io/distroless/base-debian10
|
|
||||||
|
|
||||||
ARG BIN_OUTPUT=/go/bin/worker
|
|
||||||
|
|
||||||
LABEL author="Piotr Biernat"
|
|
||||||
LABEL service="basket-worker"
|
|
||||||
LABEL vendor="Egommerce"
|
|
||||||
LABEL version="1.0"
|
|
||||||
|
|
||||||
WORKDIR /
|
|
||||||
COPY --from=builder $BIN_OUTPUT /
|
|
||||||
COPY .env.dist /.env
|
|
||||||
|
|
||||||
ENTRYPOINT ["/worker"]
|
|
@ -1,31 +1,37 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
# RUN IN REPO ROOT DIR !!
|
# RUN IN REPO ROOT DIR !!
|
||||||
|
|
||||||
export IMAGE_BASE="git.pbiernat.dev/egommerce/basket"
|
export IMAGE_PREFIX="git.pbiernat.dev/egommerce/basket"
|
||||||
export SERVER_IMAGE="$IMAGE_BASE-svc"
|
export BUILDER_IMAGE="$IMAGE_PREFIX-builder:tmp"
|
||||||
export WORKER_IMAGE="$IMAGE_BASE-worker"
|
export SERVER_IMAGE="$IMAGE_PREFIX-svc"
|
||||||
|
export WORKER_IMAGE="$IMAGE_PREFIX-worker"
|
||||||
|
|
||||||
TARGET=${1:-latest}
|
TARGET=${1:-latest}
|
||||||
KIND=${2:-all}
|
|
||||||
|
|
||||||
if [ $KIND = "svc" ] || [ $KIND = "all" ]; then
|
docker build --no-cache -t "$BUILDER_IMAGE" -f Dockerfile.builder . >/dev/null && echo "Successfully tagged $BUILDER_IMAGE"
|
||||||
echo "Building: $SERVER_IMAGE:$TARGET"
|
|
||||||
if [ $TARGET = "dev" ]
|
|
||||||
then
|
|
||||||
docker build --rm --no-cache -t "$SERVER_IMAGE:$TARGET" -f Dockerfile.server . # >/dev/null 2>&1
|
|
||||||
else
|
|
||||||
docker build --rm --cache-from "$SERVER_IMAGE:$TARGET" -t "$SERVER_IMAGE:$TARGET" -f Dockerfile.server . >/dev/null 2>&1
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ $KIND = "worker" ] || [ $KIND = "all" ]; then
|
echo "Building target $IMAGE_PREFIX images..."
|
||||||
echo "Building: $WORKER_IMAGE:$TARGET"
|
if [ $TARGET = "latest" ]
|
||||||
if [ $TARGET = "dev" ]
|
then
|
||||||
then
|
# SERVER
|
||||||
docker build --rm --no-cache -t "$WORKER_IMAGE:$TARGET" -f Dockerfile.worker . # >/dev/null 2>&1
|
docker build --build-arg SVC_NAME=basket-svc --build-arg SVC_VER="1.0" --build-arg BIN_OUTPUT=/go/bin/server \
|
||||||
else
|
--rm --build-arg BUILDER_IMAGE --cache-from "$SERVER_IMAGE:$TARGET" \
|
||||||
docker build --rm --cache-from "$WORKER_IMAGE:$TARGET" -t "$WORKER_IMAGE:$TARGET" -f Dockerfile.worker . >/dev/null 2>&1
|
-t "$SERVER_IMAGE:$TARGET" -f Dockerfile.target . >/dev/null 2>&1
|
||||||
fi
|
|
||||||
|
# WORKER
|
||||||
|
docker build --build-arg SVC_NAME=basket-worker --build-arg SVC_VER="1.0" --build-arg BIN_OUTPUT=/go/bin/worker \
|
||||||
|
--rm --build-arg BUILDER_IMAGE --cache-from "$WORKER_IMAGE:$TARGET" \
|
||||||
|
-t "$WORKER_IMAGE:$TARGET" -f Dockerfile.target . >/dev/null 2>&1
|
||||||
|
else
|
||||||
|
# SERVER
|
||||||
|
docker build --build-arg SVC_NAME=basket-svc --build-arg SVC_VER="dev" --build-arg BIN_OUTPUT=/go/bin/server \
|
||||||
|
--rm --build-arg BUILDER_IMAGE --no-cache -t "$SERVER_IMAGE:$TARGET" -f Dockerfile.target . \
|
||||||
|
>/dev/null && echo "Successfully tagged $SERVER_IMAGE:$TARGET"
|
||||||
|
|
||||||
|
# WORKER
|
||||||
|
docker build --build-arg SVC_NAME=basket-worker --build-arg SVC_VER="dev" --build-arg BIN_OUTPUT=/go/bin/worker \
|
||||||
|
--rm --build-arg BUILDER_IMAGE --no-cache -t "$WORKER_IMAGE:$TARGET" -f Dockerfile.target . \
|
||||||
|
>/dev/null && echo "Successfully tagged $WORKER_IMAGE:$TARGET"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "Done."
|
echo "Done."
|
||||||
|
Loading…
Reference in New Issue
Block a user