|
| 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.16" |
| 18 | +ARG GOLANGCI_LINT_VERSION="v1.37" |
| 19 | +ARG ADDLICENSE_VERSION="v1.0.0" |
| 20 | + |
| 21 | +ARG LICENSE_ARGS="-c cli-docs-tool -l apache" |
| 22 | +ARG LICENSE_FILES=".*\(Dockerfile\|\.go\|\.hcl\|\.sh\)" |
| 23 | + |
| 24 | +FROM golangci/golangci-lint:${GOLANGCI_LINT_VERSION}-alpine AS golangci-lint |
| 25 | +FROM ghcr.io/google/addlicense:${ADDLICENSE_VERSION} AS addlicense |
| 26 | + |
| 27 | +FROM golang:${GO_VERSION}-alpine AS base |
| 28 | +RUN apk add --no-cache cpio findutils git linux-headers |
| 29 | +ENV CGO_ENABLED=0 |
| 30 | +WORKDIR /src |
| 31 | + |
| 32 | +FROM base AS vendored |
| 33 | +RUN --mount=type=bind,target=.,rw \ |
| 34 | + --mount=type=cache,target=/go/pkg/mod \ |
| 35 | + go mod tidy && go mod download && \ |
| 36 | + mkdir /out && cp go.mod go.sum /out |
| 37 | + |
| 38 | +FROM scratch AS vendor-update |
| 39 | +COPY --from=vendored /out / |
| 40 | + |
| 41 | +FROM vendored AS vendor-validate |
| 42 | +RUN --mount=type=bind,target=.,rw <<EOT |
| 43 | +set -e |
| 44 | +git add -A |
| 45 | +cp -rf /out/* . |
| 46 | +diff=$(git status --porcelain -- go.mod go.sum) |
| 47 | +if [ -n "$diff" ]; then |
| 48 | + echo >&2 'ERROR: Vendor result differs. Please vendor your package with "docker buildx bake vendor"' |
| 49 | + echo "$diff" |
| 50 | + exit 1 |
| 51 | +fi |
| 52 | +EOT |
| 53 | + |
| 54 | +FROM base AS lint |
| 55 | +RUN --mount=type=bind,target=. \ |
| 56 | + --mount=type=cache,target=/root/.cache \ |
| 57 | + --mount=from=golangci-lint,source=/usr/bin/golangci-lint,target=/usr/bin/golangci-lint \ |
| 58 | + golangci-lint run --timeout 10m0s ./... |
| 59 | + |
| 60 | +FROM base AS license-set |
| 61 | +ARG LICENSE_ARGS |
| 62 | +ARG LICENSE_FILES |
| 63 | +RUN --mount=type=bind,target=.,rw \ |
| 64 | + --mount=from=addlicense,source=/app/addlicense,target=/usr/bin/addlicense \ |
| 65 | + find . -regex "${LICENSE_FILES}" | xargs addlicense ${LICENSE_ARGS} \ |
| 66 | + && mkdir /out \ |
| 67 | + && find . -regex "${LICENSE_FILES}" | cpio -pdm /out |
| 68 | + |
| 69 | +FROM scratch AS license-update |
| 70 | +COPY --from=set /out / |
| 71 | + |
| 72 | +FROM base AS license-validate |
| 73 | +ARG LICENSE_ARGS |
| 74 | +ARG LICENSE_FILES |
| 75 | +RUN --mount=type=bind,target=. \ |
| 76 | + --mount=from=addlicense,source=/app/addlicense,target=/usr/bin/addlicense \ |
| 77 | + find . -regex "${LICENSE_FILES}" | xargs addlicense -check ${LICENSE_ARGS} |
| 78 | + |
| 79 | +FROM vendored AS test |
| 80 | +RUN --mount=type=bind,target=. \ |
| 81 | + --mount=type=cache,target=/root/.cache \ |
| 82 | + --mount=type=cache,target=/go/pkg/mod \ |
| 83 | + go test -v -coverprofile=/tmp/coverage.txt -covermode=atomic ./... |
| 84 | + |
| 85 | +FROM scratch AS test-coverage |
| 86 | +COPY --from=test /tmp/coverage.txt /coverage.txt |
0 commit comments