Dockerfile 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. # syntax=docker/dockerfile:1
  2. # Copyright 2020 Docker Compose CLI authors
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS,
  9. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. # See the License for the specific language governing permissions and
  11. # limitations under the License.
  12. ARG GO_VERSION=1.19.3
  13. ARG XX_VERSION=1.1.2
  14. ARG GOLANGCI_LINT_VERSION=v1.49.0
  15. ARG ADDLICENSE_VERSION=v1.0.0
  16. ARG BUILD_TAGS="e2e,kube"
  17. ARG DOCS_FORMATS="md,yaml"
  18. ARG LICENSE_FILES=".*\(Dockerfile\|Makefile\|\.go\|\.hcl\|\.sh\)"
  19. # xx is a helper for cross-compilation
  20. FROM --platform=${BUILDPLATFORM} tonistiigi/xx:${XX_VERSION} AS xx
  21. FROM golangci/golangci-lint:${GOLANGCI_LINT_VERSION}-alpine AS golangci-lint
  22. FROM ghcr.io/google/addlicense:${ADDLICENSE_VERSION} AS addlicense
  23. FROM --platform=${BUILDPLATFORM} golang:${GO_VERSION}-alpine AS base
  24. COPY --from=xx / /
  25. RUN apk add --no-cache \
  26. docker \
  27. file \
  28. git \
  29. make \
  30. protoc \
  31. protobuf-dev
  32. WORKDIR /src
  33. ENV CGO_ENABLED=0
  34. FROM base AS build-base
  35. COPY go.* .
  36. RUN --mount=type=cache,target=/go/pkg/mod \
  37. --mount=type=cache,target=/root/.cache/go-build \
  38. go mod download
  39. FROM build-base AS vendored
  40. RUN --mount=type=bind,target=.,rw \
  41. --mount=type=cache,target=/go/pkg/mod \
  42. go mod tidy && mkdir /out && cp go.mod go.sum /out
  43. FROM scratch AS vendor-update
  44. COPY --from=vendored /out /
  45. FROM vendored AS vendor-validate
  46. RUN --mount=type=bind,target=.,rw <<EOT
  47. set -e
  48. git add -A
  49. cp -rf /out/* .
  50. diff=$(git status --porcelain -- go.mod go.sum)
  51. if [ -n "$diff" ]; then
  52. echo >&2 'ERROR: Vendor result differs. Please vendor your package with "make go-mod-tidy"'
  53. echo "$diff"
  54. exit 1
  55. fi
  56. EOT
  57. FROM vendored AS modules-validate
  58. RUN apk add --no-cache bash
  59. RUN apk add --no-cache jq
  60. RUN --mount=type=bind,target=.,rw ./verify-go-modules.sh e2e
  61. FROM build-base AS build
  62. ARG BUILD_TAGS
  63. ARG TARGETPLATFORM
  64. RUN xx-go --wrap
  65. RUN --mount=type=bind,target=. \
  66. --mount=type=cache,target=/root/.cache \
  67. --mount=type=cache,target=/go/pkg/mod \
  68. make build GO_BUILDTAGS="$BUILD_TAGS" DESTDIR=/usr/bin && \
  69. xx-verify --static /usr/bin/docker-compose
  70. FROM build-base AS lint
  71. ARG BUILD_TAGS
  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 --build-tags "$BUILD_TAGS" ./...
  76. FROM build-base AS test
  77. ARG CGO_ENABLED=0
  78. ARG BUILD_TAGS
  79. RUN --mount=type=bind,target=. \
  80. --mount=type=cache,target=/root/.cache \
  81. --mount=type=cache,target=/go/pkg/mod \
  82. go test -tags "$BUILD_TAGS" -v -coverprofile=/tmp/coverage.txt -covermode=atomic $(go list $(TAGS) ./... | grep -vE 'e2e') && \
  83. go tool cover -func=/tmp/coverage.txt
  84. FROM scratch AS test-coverage
  85. COPY --from=test /tmp/coverage.txt /coverage.txt
  86. FROM base AS license-set
  87. ARG LICENSE_FILES
  88. RUN --mount=type=bind,target=.,rw \
  89. --mount=from=addlicense,source=/app/addlicense,target=/usr/bin/addlicense \
  90. find . -regex "${LICENSE_FILES}" | xargs addlicense -c 'Docker Compose CLI' -l apache && \
  91. mkdir /out && \
  92. find . -regex "${LICENSE_FILES}" | cpio -pdm /out
  93. FROM scratch AS license-update
  94. COPY --from=set /out /
  95. FROM base AS license-validate
  96. ARG LICENSE_FILES
  97. RUN --mount=type=bind,target=. \
  98. --mount=from=addlicense,source=/app/addlicense,target=/usr/bin/addlicense \
  99. find . -regex "${LICENSE_FILES}" | xargs addlicense -check -c 'Docker Compose CLI' -l apache -ignore validate -ignore testdata -ignore resolvepath -v
  100. FROM base AS docsgen
  101. WORKDIR /src
  102. RUN --mount=target=. \
  103. --mount=target=/root/.cache,type=cache \
  104. go build -o /out/docsgen ./docs/yaml/main/generate.go
  105. FROM --platform=${BUILDPLATFORM} alpine AS docs-build
  106. RUN apk add --no-cache rsync git
  107. WORKDIR /src
  108. COPY --from=docsgen /out/docsgen /usr/bin
  109. ARG DOCS_FORMATS
  110. RUN --mount=target=/context \
  111. --mount=target=.,type=tmpfs <<EOT
  112. set -e
  113. rsync -a /context/. .
  114. docsgen --formats "$DOCS_FORMATS" --source "docs/reference"
  115. mkdir /out
  116. cp -r docs/reference /out
  117. EOT
  118. FROM scratch AS docs-update
  119. COPY --from=docs-build /out /out
  120. FROM docs-build AS docs-validate
  121. RUN --mount=target=/context \
  122. --mount=target=.,type=tmpfs <<EOT
  123. set -e
  124. rsync -a /context/. .
  125. git add -A
  126. rm -rf docs/reference/*
  127. cp -rf /out/* ./docs/
  128. if [ -n "$(git status --porcelain -- docs/reference)" ]; then
  129. echo >&2 'ERROR: Docs result differs. Please update with "make docs"'
  130. git status --porcelain -- docs/reference
  131. exit 1
  132. fi
  133. EOT
  134. FROM scratch AS binary-unix
  135. COPY --link --from=build /usr/bin/docker-compose /
  136. FROM binary-unix AS binary-darwin
  137. FROM binary-unix AS binary-linux
  138. FROM scratch AS binary-windows
  139. COPY --link --from=build /usr/bin/docker-compose /docker-compose.exe
  140. FROM binary-$TARGETOS AS binary
  141. FROM --platform=$BUILDPLATFORM alpine AS releaser
  142. WORKDIR /work
  143. ARG TARGETOS
  144. ARG TARGETARCH
  145. ARG TARGETVARIANT
  146. RUN --mount=from=binary \
  147. mkdir -p /out && \
  148. # TODO: should just use standard arch
  149. TARGETARCH=$([ "$TARGETARCH" = "amd64" ] && echo "x86_64" || echo "$TARGETARCH"); \
  150. TARGETARCH=$([ "$TARGETARCH" = "arm64" ] && echo "aarch64" || echo "$TARGETARCH"); \
  151. cp docker-compose* "/out/docker-compose-${TARGETOS}-${TARGETARCH}${TARGETVARIANT}$(ls docker-compose* | sed -e 's/^docker-compose//')"
  152. FROM scratch AS release
  153. COPY --from=releaser /out/ /
  154. # docs-reference is a target used as remote context to update docs on release
  155. # with latest changes on docs.docker.com.
  156. # see open-pr job in .github/workflows/docs.yml for more details
  157. FROM scratch AS docs-reference
  158. COPY docs/reference/*.yaml .