| 1 |
# syntax=docker/dockerfile:1 |
| 2 |
|
| 3 |
|
| 4 |
# Copyright 2020 Docker Compose CLI authors |
| 5 |
|
| 6 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
# you may not use this file except in compliance with the License. |
| 8 |
# You may obtain a copy of the License at |
| 9 |
|
| 10 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
|
| 12 |
# Unless required by applicable law or agreed to in writing, software |
| 13 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
# See the License for the specific language governing permissions and |
| 16 |
# limitations under the License. |
| 17 |
|
| 18 |
ARG GO_VERSION=1.24.9 |
| 19 |
ARG XX_VERSION=1.6.1 |
| 20 |
ARG GOLANGCI_LINT_VERSION=v2.0.2 |
| 21 |
ARG ADDLICENSE_VERSION=v1.0.0 |
| 22 |
|
| 23 |
ARG BUILD_TAGS="e2e" |
| 24 |
ARG DOCS_FORMATS="md,yaml" |
| 25 |
ARG LICENSE_FILES=".*\(Dockerfile\|Makefile\|\.go\|\.hcl\|\.sh\)" |
| 26 |
|
| 27 |
# xx is a helper for cross-compilation |
| 28 |
FROM --platform=${BUILDPLATFORM} tonistiigi/xx:${XX_VERSION} AS xx |
| 29 |
|
| 30 |
# osxcross contains the MacOSX cross toolchain for xx |
| 31 |
FROM crazymax/osxcross:11.3-alpine AS osxcross |
| 32 |
|
| 33 |
FROM golangci/golangci-lint:${GOLANGCI_LINT_VERSION}-alpine AS golangci-lint |
| 34 |
FROM ghcr.io/google/addlicense:${ADDLICENSE_VERSION} AS addlicense |
| 35 |
|
| 36 |
FROM --platform=${BUILDPLATFORM} golang:${GO_VERSION}-alpine AS base |
| 37 |
COPY --from=xx / / |
| 38 |
RUN apk add --no-cache \ |
| 39 |
clang \ |
| 40 |
docker \ |
| 41 |
file \ |
| 42 |
findutils \ |
| 43 |
git \ |
| 44 |
make \ |
| 45 |
protoc \ |
| 46 |
protobuf-dev |
| 47 |
WORKDIR /src |
| 48 |
ENV CGO_ENABLED=0 |
| 49 |
|
| 50 |
FROM base AS build-base |
| 51 |
COPY go.* . |
| 52 |
RUN --mount=type=cache,target=/go/pkg/mod \ |
| 53 |
--mount=type=cache,target=/root/.cache/go-build \ |
| 54 |
go mod download |
| 55 |
|
| 56 |
FROM build-base AS vendored |
| 57 |
RUN --mount=type=bind,target=.,rw \ |
| 58 |
--mount=type=cache,target=/go/pkg/mod \ |
| 59 |
go mod tidy && mkdir /out && cp go.mod go.sum /out |
| 60 |
|
| 61 |
FROM scratch AS vendor-update |
| 62 |
COPY --from=vendored /out / |
| 63 |
|
| 64 |
FROM vendored AS vendor-validate |
| 65 |
RUN --mount=type=bind,target=.,rw <<EOT |
| 66 |
set -e |
| 67 |
git add -A |
| 68 |
cp -rf /out/* . |
| 69 |
diff=$(git status --porcelain -- go.mod go.sum) |
| 70 |
if [ -n "$diff" ]; then |
| 71 |
echo >&2 'ERROR: Vendor result differs. Please vendor your package with "make go-mod-tidy"' |
| 72 |
echo "$diff" |
| 73 |
exit 1 |
| 74 |
fi |
| 75 |
EOT |
| 76 |
|
| 77 |
FROM build-base AS build |
| 78 |
ARG BUILD_TAGS |
| 79 |
ARG BUILD_FLAGS |
| 80 |
ARG TARGETPLATFORM |
| 81 |
RUN --mount=type=bind,target=. \ |
| 82 |
--mount=type=cache,target=/root/.cache \ |
| 83 |
--mount=type=cache,target=/go/pkg/mod \ |
| 84 |
--mount=type=bind,from=osxcross,src=/osxsdk,target=/xx-sdk \ |
| 85 |
xx-go --wrap && \ |
| 86 |
if [ "$(xx-info os)" == "darwin" ]; then export CGO_ENABLED=1; fi && \ |
| 87 |
make build GO_BUILDTAGS="$BUILD_TAGS" DESTDIR=/out && \ |
| 88 |
xx-verify --static /out/docker-compose |
| 89 |
|
| 90 |
FROM build-base AS lint |
| 91 |
ARG BUILD_TAGS |
| 92 |
ENV GOLANGCI_LINT_CACHE=/cache/golangci-lint |
| 93 |
RUN --mount=type=bind,target=. \ |
| 94 |
--mount=type=cache,target=/root/.cache \ |
| 95 |
--mount=type=cache,target=/go/pkg/mod \ |
| 96 |
--mount=type=cache,target=/cache/golangci-lint \ |
| 97 |
--mount=from=golangci-lint,source=/usr/bin/golangci-lint,target=/usr/bin/golangci-lint \ |
| 98 |
golangci-lint cache status && \ |
| 99 |
golangci-lint run --build-tags "$BUILD_TAGS" ./... |
| 100 |
|
| 101 |
FROM build-base AS test |
| 102 |
ARG CGO_ENABLED=0 |
| 103 |
ARG BUILD_TAGS |
| 104 |
RUN --mount=type=bind,target=. \ |
| 105 |
--mount=type=cache,target=/root/.cache \ |
| 106 |
--mount=type=cache,target=/go/pkg/mod \ |
| 107 |
rm -rf /tmp/coverage && \ |
| 108 |
mkdir -p /tmp/coverage && \ |
| 109 |
rm -rf /tmp/report && \ |
| 110 |
mkdir -p /tmp/report && \ |
| 111 |
go run gotest.tools/gotestsum@latest --format testname --junitfile "/tmp/report/report.xml" -- -tags "$BUILD_TAGS" -v -cover -covermode=atomic $(go list $(TAGS) ./... | grep -vE 'e2e') -args -test.gocoverdir="/tmp/coverage" && \ |
| 112 |
go tool covdata percent -i=/tmp/coverage |
| 113 |
|
| 114 |
FROM scratch AS test-coverage |
| 115 |
COPY --from=test --link /tmp/coverage / |
| 116 |
COPY --from=test --link /tmp/report / |
| 117 |
|
| 118 |
FROM base AS license-set |
| 119 |
ARG LICENSE_FILES |
| 120 |
RUN --mount=type=bind,target=.,rw \ |
| 121 |
--mount=from=addlicense,source=/app/addlicense,target=/usr/bin/addlicense \ |
| 122 |
find . -regex "${LICENSE_FILES}" | xargs addlicense -c 'Docker Compose CLI' -l apache && \ |
| 123 |
mkdir /out && \ |
| 124 |
find . -regex "${LICENSE_FILES}" | cpio -pdm /out |
| 125 |
|
| 126 |
FROM scratch AS license-update |
| 127 |
COPY --from=set /out / |
| 128 |
|
| 129 |
FROM base AS license-validate |
| 130 |
ARG LICENSE_FILES |
| 131 |
RUN --mount=type=bind,target=. \ |
| 132 |
--mount=from=addlicense,source=/app/addlicense,target=/usr/bin/addlicense \ |
| 133 |
find . -regex "${LICENSE_FILES}" | xargs addlicense -check -c 'Docker Compose CLI' -l apache -ignore validate -ignore testdata -ignore resolvepath -v |
| 134 |
|
| 135 |
FROM base AS docsgen |
| 136 |
WORKDIR /src |
| 137 |
RUN --mount=target=. \ |
| 138 |
--mount=target=/root/.cache,type=cache \ |
| 139 |
--mount=type=cache,target=/go/pkg/mod \ |
| 140 |
go build -o /out/docsgen ./docs/yaml/main/generate.go |
| 141 |
|
| 142 |
FROM --platform=${BUILDPLATFORM} alpine AS docs-build |
| 143 |
RUN apk add --no-cache rsync git |
| 144 |
WORKDIR /src |
| 145 |
COPY --from=docsgen /out/docsgen /usr/bin |
| 146 |
ARG DOCS_FORMATS |
| 147 |
RUN --mount=target=/context \ |
| 148 |
--mount=target=.,type=tmpfs <<EOT |
| 149 |
set -e |
| 150 |
rsync -a /context/. . |
| 151 |
docsgen --formats "$DOCS_FORMATS" --source "docs/reference" |
| 152 |
mkdir /out |
| 153 |
cp -r docs/reference /out |
| 154 |
EOT |
| 155 |
|
| 156 |
FROM scratch AS docs-update |
| 157 |
COPY --from=docs-build /out /out |
| 158 |
|
| 159 |
FROM docs-build AS docs-validate |
| 160 |
RUN --mount=target=/context \ |
| 161 |
--mount=target=.,type=tmpfs <<EOT |
| 162 |
set -e |
| 163 |
rsync -a /context/. . |
| 164 |
git add -A |
| 165 |
rm -rf docs/reference/* |
| 166 |
cp -rf /out/* ./docs/ |
| 167 |
if [ -n "$(git status --porcelain -- docs/reference)" ]; then |
| 168 |
echo >&2 'ERROR: Docs result differs. Please update with "make docs"' |
| 169 |
git status --porcelain -- docs/reference |
| 170 |
exit 1 |
| 171 |
fi |
| 172 |
EOT |
| 173 |
|
| 174 |
FROM scratch AS binary-unix |
| 175 |
COPY --link --from=build /out/docker-compose / |
| 176 |
FROM binary-unix AS binary-darwin |
| 177 |
FROM binary-unix AS binary-linux |
| 178 |
FROM scratch AS binary-windows |
| 179 |
COPY --link --from=build /out/docker-compose /docker-compose.exe |
| 180 |
FROM binary-$TARGETOS AS binary |
| 181 |
# enable scanning for this stage |
| 182 |
ARG BUILDKIT_SBOM_SCAN_STAGE=true |
| 183 |
|
| 184 |
FROM --platform=$BUILDPLATFORM alpine AS releaser |
| 185 |
WORKDIR /work |
| 186 |
ARG TARGETOS |
| 187 |
ARG TARGETARCH |
| 188 |
ARG TARGETVARIANT |
| 189 |
RUN --mount=from=binary \ |
| 190 |
mkdir -p /out && \ |
| 191 |
# TODO: should just use standard arch |
| 192 |
TARGETARCH=$([ "$TARGETARCH" = "amd64" ] && echo "x86_64" || echo "$TARGETARCH"); \ |
| 193 |
TARGETARCH=$([ "$TARGETARCH" = "arm64" ] && echo "aarch64" || echo "$TARGETARCH"); \ |
| 194 |
cp docker-compose* "/out/docker-compose-${TARGETOS}-${TARGETARCH}${TARGETVARIANT}$(ls docker-compose* | sed -e 's/^docker-compose//')" |
| 195 |
|
| 196 |
FROM scratch AS release |
| 197 |
COPY --from=releaser /out/ / |