| 1 |
# syntax=docker/dockerfile:1 |
| 2 |
|
| 3 |
# Copyright 2021 cli-docs-tool authors |
| 4 |
# |
| 5 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
# you may not use this file except in compliance with the License. |
| 7 |
# You may obtain a copy of the License at |
| 8 |
# |
| 9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
# |
| 11 |
# Unless required by applicable law or agreed to in writing, software |
| 12 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
# See the License for the specific language governing permissions and |
| 15 |
# limitations under the License. |
| 16 |
|
| 17 |
ARG GO_VERSION="1.24" |
| 18 |
ARG XX_VERSION="1.9.0" |
| 19 |
ARG GOLANGCI_LINT_VERSION="v2.7.1" |
| 20 |
ARG ADDLICENSE_VERSION="v1.2.0" |
| 21 |
|
| 22 |
ARG LICENSE_ARGS="-c cli-docs-tool -l apache" |
| 23 |
ARG LICENSE_FILES=".*\(Dockerfile\|\.go\|\.hcl\|\.sh\)" |
| 24 |
|
| 25 |
FROM golangci/golangci-lint:${GOLANGCI_LINT_VERSION}-alpine AS golangci-lint |
| 26 |
FROM --platform=$BUILDPLATFORM tonistiigi/xx:${XX_VERSION} AS xx |
| 27 |
|
| 28 |
FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-alpine AS base |
| 29 |
RUN apk add --no-cache cpio findutils git linux-headers |
| 30 |
ENV CGO_ENABLED=0 |
| 31 |
WORKDIR /src |
| 32 |
COPY --link --from=xx / / |
| 33 |
|
| 34 |
FROM base AS addlicense |
| 35 |
ARG ADDLICENSE_VERSION |
| 36 |
ARG TARGETPLATFORM |
| 37 |
RUN --mount=target=/root/.cache,type=cache \ |
| 38 |
--mount=type=cache,target=/go/pkg/mod <<EOT |
| 39 |
set -ex |
| 40 |
xx-go install "github.com/google/addlicense@${ADDLICENSE_VERSION}" |
| 41 |
mkdir /out |
| 42 |
if ! xx-info is-cross; then |
| 43 |
mv /go/bin/addlicense /out |
| 44 |
else |
| 45 |
mv /go/bin/*/addlicense* /out |
| 46 |
fi |
| 47 |
EOT |
| 48 |
|
| 49 |
FROM base AS vendored |
| 50 |
RUN --mount=type=bind,target=.,rw \ |
| 51 |
--mount=type=cache,target=/go/pkg/mod \ |
| 52 |
go mod tidy && go mod download && \ |
| 53 |
mkdir /out && cp go.mod go.sum /out |
| 54 |
|
| 55 |
FROM scratch AS vendor-update |
| 56 |
COPY --from=vendored /out / |
| 57 |
|
| 58 |
FROM vendored AS vendor-validate |
| 59 |
RUN --mount=type=bind,target=.,rw <<EOT |
| 60 |
set -e |
| 61 |
git add -A |
| 62 |
cp -rf /out/* . |
| 63 |
diff=$(git status --porcelain -- go.mod go.sum) |
| 64 |
if [ -n "$diff" ]; then |
| 65 |
echo >&2 'ERROR: Vendor result differs. Please vendor your package with "docker buildx bake vendor"' |
| 66 |
echo "$diff" |
| 67 |
exit 1 |
| 68 |
fi |
| 69 |
EOT |
| 70 |
|
| 71 |
FROM base AS lint |
| 72 |
RUN --mount=type=bind,target=. \ |
| 73 |
--mount=type=cache,target=/root/.cache \ |
| 74 |
--mount=from=golangci-lint,source=/usr/bin/golangci-lint,target=/usr/bin/golangci-lint \ |
| 75 |
golangci-lint run ./... |
| 76 |
|
| 77 |
FROM base AS license-set |
| 78 |
ARG LICENSE_ARGS |
| 79 |
ARG LICENSE_FILES |
| 80 |
RUN --mount=type=bind,target=.,rw \ |
| 81 |
--mount=from=addlicense,source=/out/addlicense,target=/usr/bin/addlicense \ |
| 82 |
find . -regex "${LICENSE_FILES}" | xargs addlicense ${LICENSE_ARGS} \ |
| 83 |
&& mkdir /out \ |
| 84 |
&& find . -regex "${LICENSE_FILES}" | cpio -pdm /out |
| 85 |
|
| 86 |
FROM scratch AS license-update |
| 87 |
COPY --from=set /out / |
| 88 |
|
| 89 |
FROM base AS license-validate |
| 90 |
ARG LICENSE_ARGS |
| 91 |
ARG LICENSE_FILES |
| 92 |
RUN --mount=type=bind,target=. \ |
| 93 |
--mount=from=addlicense,source=/out/addlicense,target=/usr/bin/addlicense \ |
| 94 |
find . -regex "${LICENSE_FILES}" | xargs addlicense -check ${LICENSE_ARGS} |
| 95 |
|
| 96 |
FROM vendored AS test |
| 97 |
RUN --mount=type=bind,target=. \ |
| 98 |
--mount=type=cache,target=/root/.cache \ |
| 99 |
--mount=type=cache,target=/go/pkg/mod \ |
| 100 |
go test -v -coverprofile=/tmp/coverage.txt -covermode=atomic ./... |
| 101 |
|
| 102 |
FROM scratch AS test-coverage |
| 103 |
COPY --from=test /tmp/coverage.txt /coverage.txt |