Enhance dockerfiles (#523)

Co-authored-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax
2021-12-25 22:38:58 +01:00
committed by GitHub
parent 65455b04f7
commit a7e9a40fed
11 changed files with 164 additions and 173 deletions

View File

@@ -1,6 +1,8 @@
# syntax=docker/dockerfile:1.2
FROM squidfunk/mkdocs-material:8.1.0 AS base
ARG MKDOCS_VERSION="8.1.0"
FROM squidfunk/mkdocs-material:${MKDOCS_VERSION} AS base
RUN apk add --no-cache \
git \
git-fast-import \
@@ -18,7 +20,7 @@ RUN apk add --no-cache \
FROM base AS generate
RUN --mount=type=bind,target=. \
mkdocs build --strict --site-dir /tmp/site
mkdocs build --strict --site-dir /out
FROM scratch AS release
COPY --from=generate /tmp/site/ /
COPY --from=generate /out /

View File

@@ -1,7 +1,8 @@
# syntax=docker/dockerfile:1.2
# syntax=docker/dockerfile:1.3-labs
ARG GO_VERSION
ARG PROTOC_VERSION
ARG GLIBC_VERSION=2.33-r0
ARG PROTOC_VERSION="3.17.3"
ARG GLIBC_VERSION="2.33-r0"
FROM golang:${GO_VERSION}-alpine AS base
ARG GLIBC_VERSION
@@ -17,24 +18,37 @@ RUN curl -sSL "https://github.com/protocolbuffers/protobuf/releases/download/v${
&& rm "protoc.zip"
WORKDIR /src
FROM base AS gomod
FROM base AS vendored
RUN --mount=type=bind,target=.,rw \
--mount=type=cache,target=/go/pkg/mod \
go mod tidy && go mod download && go install -v $(sed -n -e 's|^\s*_\s*"\(.*\)".*$|\1| p' tools.go)
go mod tidy && go mod download
FROM gomod AS generate
FROM vendored AS tools
RUN --mount=type=bind,target=.,rw \
--mount=type=cache,target=/go/pkg/mod \
go generate ./... && mkdir /out && cp -Rf pb /out
go install -v $(sed -n -e 's|^\s*_\s*"\(.*\)".*$|\1| p' tools.go)
FROM tools AS generate
RUN --mount=type=bind,target=.,rw \
--mount=type=cache,target=/go/pkg/mod <<EOT
set -e
go generate ./...
mkdir /out
cp -Rf pb /out
EOT
FROM scratch AS update
COPY --from=generate /out /
FROM generate AS validate
RUN --mount=type=bind,target=.,rw \
git add -A && cp -rf /out/* .; \
if [ -n "$(git status --porcelain -- pb)" ]; then \
echo >&2 'ERROR: Generate result differs. Please update with "docker buildx bake gen-update"'; \
git status --porcelain -- pb; \
exit 1; \
fi
RUN --mount=type=bind,target=.,rw <<EOT
set -e
git add -A
cp -rf /out/* .
diff=$(git status --porcelain -- pb)
if [ -n "$diff" ]; then
echo >&2 'ERROR: Vendor result differs. Please vendor your package with "docker buildx bake gen"'
echo "$diff"
exit 1
fi
EOT

View File

@@ -1,15 +1,15 @@
# syntax=docker/dockerfile:1.2
ARG GO_VERSION
ARG GOLANGCI_LINT_VERSION="v1.37"
FROM golang:${GO_VERSION}-alpine AS base
RUN apk add --no-cache gcc linux-headers musl-dev
WORKDIR /src
FROM golangci/golangci-lint:v1.37-alpine AS golangci-lint
FROM golangci/golangci-lint:${GOLANGCI_LINT_VERSION}-alpine AS golangci-lint
FROM base AS lint
RUN --mount=type=bind,target=. \
--mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/root/.cache/golangci-lint \
--mount=type=cache,target=/root/.cache \
--mount=from=golangci-lint,source=/usr/bin/golangci-lint,target=/usr/bin/golangci-lint \
golangci-lint run --timeout 10m0s ./...

View File

@@ -1,21 +0,0 @@
# syntax=docker/dockerfile:1.2
ARG GO_VERSION
FROM golang:${GO_VERSION}-alpine AS base
RUN apk add --no-cache gcc linux-headers musl-dev
WORKDIR /src
FROM base AS gomod
RUN --mount=type=bind,target=.,rw \
--mount=type=cache,target=/go/pkg/mod \
go mod tidy && go mod download
FROM gomod AS test
RUN --mount=type=bind,target=. \
--mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
go test -v -coverprofile=/tmp/coverage.txt -covermode=atomic -race ./... && \
go tool cover -func=/tmp/coverage.txt
FROM scratch AS test-coverage
COPY --from=test /tmp/coverage.txt /coverage.txt

View File

@@ -1,5 +1,7 @@
# syntax=docker/dockerfile:1.2
# syntax=docker/dockerfile:1.3-labs
ARG GO_VERSION
ARG GOMOD_OUTDATED_VERSION="v0.8.0"
FROM golang:${GO_VERSION}-alpine AS base
RUN apk add --no-cache git linux-headers musl-dev
@@ -7,25 +9,33 @@ WORKDIR /src
FROM base AS vendored
RUN --mount=type=bind,target=.,rw \
--mount=type=cache,target=/go/pkg/mod \
go mod tidy && go mod download && \
mkdir /out && cp go.mod go.sum /out
--mount=type=cache,target=/go/pkg/mod <<EOT
set -e
go mod tidy
go mod download
mkdir /out
cp go.mod go.sum /out
EOT
FROM scratch AS update
COPY --from=vendored /out /
FROM vendored AS validate
RUN --mount=type=bind,target=.,rw \
git add -A && cp -rf /out/* .; \
if [ -n "$(git status --porcelain -- go.mod go.sum)" ]; then \
echo >&2 'ERROR: Vendor result differs. Please vendor your package with "docker buildx bake vendor-update"'; \
git status --porcelain -- go.mod go.sum; \
exit 1; \
fi
RUN --mount=type=bind,target=.,rw <<EOT
set -e
git add -A
cp -rf /out/* .
diff=$(git status --porcelain -- go.mod go.sum)
if [ -n "$diff" ]; then
echo >&2 'ERROR: Vendor result differs. Please vendor your package with "docker buildx bake vendor"'
echo "$diff"
exit 1
fi
EOT
FROM psampaz/go-mod-outdated:v0.8.0 AS go-mod-outdated
FROM psampaz/go-mod-outdated:${GOMOD_OUTDATED_VERSION} AS go-mod-outdated
FROM base AS outdated
RUN --mount=type=bind,target=.,ro \
RUN --mount=type=bind,target=. \
--mount=type=cache,target=/go/pkg/mod \
--mount=from=go-mod-outdated,source=/home/go-mod-outdated,target=/usr/bin/go-mod-outdated \
go list -mod=readonly -u -m -json all | go-mod-outdated -update -direct