Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 31 additions & 14 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,36 @@
# limitations under the License.

ARG GO_VERSION="1.23"
ARG XX_VERSION="1.6.1"
ARG GOLANGCI_LINT_VERSION="v1.62"
ARG ADDLICENSE_VERSION="v1.0.0"
ARG ADDLICENSE_VERSION="v1.1.1"

ARG LICENSE_ARGS="-c cli-docs-tool -l apache"
ARG LICENSE_FILES=".*\(Dockerfile\|\.go\|\.hcl\|\.sh\)"

FROM golangci/golangci-lint:${GOLANGCI_LINT_VERSION}-alpine AS golangci-lint
FROM ghcr.io/google/addlicense:${ADDLICENSE_VERSION} AS addlicense
FROM --platform=$BUILDPLATFORM tonistiigi/xx:${XX_VERSION} AS xx

FROM golang:${GO_VERSION}-alpine AS base
FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-alpine AS base
RUN apk add --no-cache cpio findutils git linux-headers
ENV CGO_ENABLED=0
WORKDIR /src
COPY --link --from=xx / /

FROM base AS addlicense
ARG ADDLICENSE_VERSION
ARG TARGETPLATFORM
RUN --mount=target=/root/.cache,type=cache \
--mount=type=cache,target=/go/pkg/mod <<EOT
set -ex
xx-go install "github.com/google/addlicense@${ADDLICENSE_VERSION}"
mkdir /out
if ! xx-info is-cross; then
mv /go/bin/addlicense /out
else
mv /go/bin/*/addlicense* /out
fi
EOT

FROM base AS vendored
RUN --mount=type=bind,target=.,rw \
Expand All @@ -40,15 +57,15 @@ COPY --from=vendored /out /

FROM vendored AS vendor-validate
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
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 base AS lint
Expand All @@ -61,7 +78,7 @@ FROM base AS license-set
ARG LICENSE_ARGS
ARG LICENSE_FILES
RUN --mount=type=bind,target=.,rw \
--mount=from=addlicense,source=/app/addlicense,target=/usr/bin/addlicense \
--mount=from=addlicense,source=/out/addlicense,target=/usr/bin/addlicense \
find . -regex "${LICENSE_FILES}" | xargs addlicense ${LICENSE_ARGS} \
&& mkdir /out \
&& find . -regex "${LICENSE_FILES}" | cpio -pdm /out
Expand All @@ -73,7 +90,7 @@ FROM base AS license-validate
ARG LICENSE_ARGS
ARG LICENSE_FILES
RUN --mount=type=bind,target=. \
--mount=from=addlicense,source=/app/addlicense,target=/usr/bin/addlicense \
--mount=from=addlicense,source=/out/addlicense,target=/usr/bin/addlicense \
find . -regex "${LICENSE_FILES}" | xargs addlicense -check ${LICENSE_ARGS}

FROM vendored AS test
Expand Down
Loading