github.com/khulnasoft/cli@v0.0.0-20240402070845-01bcad7beefa/Dockerfile (about) 1 # syntax=docker/dockerfile:1 2 3 ARG BASE_VARIANT=alpine 4 ARG ALPINE_VERSION=3.18 5 ARG BASE_DEBIAN_DISTRO=bookworm 6 7 ARG GO_VERSION=1.21.8 8 ARG XX_VERSION=1.4.0 9 ARG GOVERSIONINFO_VERSION=v1.3.0 10 ARG GOTESTSUM_VERSION=v1.10.0 11 ARG BUILDX_VERSION=0.12.1 12 ARG COMPOSE_VERSION=v2.24.3 13 14 FROM --platform=$BUILDPLATFORM tonistiigi/xx:${XX_VERSION} AS xx 15 16 FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-alpine${ALPINE_VERSION} AS build-base-alpine 17 ENV GOTOOLCHAIN=local 18 COPY --link --from=xx / / 19 RUN apk add --no-cache bash clang lld llvm file git 20 WORKDIR /go/src/github.com/khulnasoft/cli 21 22 FROM build-base-alpine AS build-alpine 23 ARG TARGETPLATFORM 24 # gcc is installed for libgcc only 25 RUN xx-apk add --no-cache musl-dev gcc 26 27 FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-${BASE_DEBIAN_DISTRO} AS build-base-debian 28 ENV GOTOOLCHAIN=local 29 COPY --link --from=xx / / 30 RUN apt-get update && apt-get install --no-install-recommends -y bash clang lld llvm file 31 WORKDIR /go/src/github.com/khulnasoft/cli 32 33 FROM build-base-debian AS build-debian 34 ARG TARGETPLATFORM 35 RUN xx-apt-get install --no-install-recommends -y libc6-dev libgcc-12-dev pkgconf 36 37 FROM build-base-${BASE_VARIANT} AS goversioninfo 38 ARG GOVERSIONINFO_VERSION 39 RUN --mount=type=cache,target=/root/.cache/go-build \ 40 --mount=type=cache,target=/go/pkg/mod \ 41 GOBIN=/out GO111MODULE=on CGO_ENABLED=0 go install "github.com/josephspurrier/goversioninfo/cmd/goversioninfo@${GOVERSIONINFO_VERSION}" 42 43 FROM build-base-${BASE_VARIANT} AS gotestsum 44 ARG GOTESTSUM_VERSION 45 RUN --mount=type=cache,target=/root/.cache/go-build \ 46 --mount=type=cache,target=/go/pkg/mod \ 47 GOBIN=/out GO111MODULE=on CGO_ENABLED=0 go install "gotest.tools/gotestsum@${GOTESTSUM_VERSION}" \ 48 && /out/gotestsum --version 49 50 FROM build-${BASE_VARIANT} AS build 51 # GO_LINKMODE defines if static or dynamic binary should be produced 52 ARG GO_LINKMODE=static 53 # GO_BUILDTAGS defines additional build tags 54 ARG GO_BUILDTAGS 55 # GO_STRIP strips debugging symbols if set 56 ARG GO_STRIP 57 # CGO_ENABLED manually sets if cgo is used 58 ARG CGO_ENABLED 59 # VERSION sets the version for the produced binary 60 ARG VERSION 61 # PACKAGER_NAME sets the company that produced the windows binary 62 ARG PACKAGER_NAME 63 COPY --link --from=goversioninfo /out/goversioninfo /usr/bin/goversioninfo 64 RUN --mount=type=bind,target=.,ro \ 65 --mount=type=cache,target=/root/.cache \ 66 --mount=from=dockercore/golang-cross:xx-sdk-extras,target=/xx-sdk,src=/xx-sdk \ 67 --mount=type=tmpfs,target=cli/winresources \ 68 # override the default behavior of go with xx-go 69 xx-go --wrap && \ 70 # export GOCACHE=$(go env GOCACHE)/$(xx-info)$([ -f /etc/alpine-release ] && echo "alpine") && \ 71 TARGET=/out ./scripts/build/binary && \ 72 xx-verify $([ "$GO_LINKMODE" = "static" ] && echo "--static") /out/docker 73 74 FROM build-${BASE_VARIANT} AS test 75 COPY --link --from=gotestsum /out/gotestsum /usr/bin/gotestsum 76 ENV GO111MODULE=auto 77 RUN --mount=type=bind,target=.,rw \ 78 --mount=type=cache,target=/root/.cache \ 79 --mount=type=cache,target=/go/pkg/mod \ 80 gotestsum -- -coverprofile=/tmp/coverage.txt $(go list ./... | grep -vE '/vendor/|/e2e/') 81 82 FROM scratch AS test-coverage 83 COPY --from=test /tmp/coverage.txt /coverage.txt 84 85 FROM build-${BASE_VARIANT} AS build-plugins 86 ARG GO_LINKMODE=static 87 ARG GO_BUILDTAGS 88 ARG GO_STRIP 89 ARG CGO_ENABLED 90 ARG VERSION 91 RUN --mount=ro --mount=type=cache,target=/root/.cache \ 92 --mount=from=dockercore/golang-cross:xx-sdk-extras,target=/xx-sdk,src=/xx-sdk \ 93 xx-go --wrap && \ 94 TARGET=/out ./scripts/build/plugins e2e/cli-plugins/plugins/* 95 96 FROM build-base-alpine AS e2e-base-alpine 97 RUN apk add --no-cache build-base curl openssl openssh-client 98 99 FROM build-base-debian AS e2e-base-debian 100 RUN apt-get update && apt-get install -y build-essential curl openssl openssh-client 101 102 FROM docker/buildx-bin:${BUILDX_VERSION} AS buildx 103 FROM docker/compose-bin:${COMPOSE_VERSION} AS compose 104 105 FROM e2e-base-${BASE_VARIANT} AS e2e 106 ARG NOTARY_VERSION=v0.6.1 107 ADD --chmod=0755 https://github.com/theupdateframework/notary/releases/download/${NOTARY_VERSION}/notary-Linux-amd64 /usr/local/bin/notary 108 COPY --link e2e/testdata/notary/root-ca.cert /usr/share/ca-certificates/notary.cert 109 RUN echo 'notary.cert' >> /etc/ca-certificates.conf && update-ca-certificates 110 COPY --link --from=gotestsum /out/gotestsum /usr/bin/gotestsum 111 COPY --link --from=build /out ./build/ 112 COPY --link --from=build-plugins /out ./build/ 113 COPY --link --from=buildx /buildx /usr/libexec/docker/cli-plugins/docker-buildx 114 COPY --link --from=compose /docker-compose /usr/libexec/docker/cli-plugins/docker-compose 115 COPY --link . . 116 ENV DOCKER_BUILDKIT=1 117 ENV PATH=/go/src/github.com/khulnasoft/cli/build:$PATH 118 CMD ./scripts/test/e2e/entry 119 120 FROM build-base-${BASE_VARIANT} AS dev 121 COPY --link . . 122 123 FROM scratch AS plugins 124 COPY --from=build-plugins /out . 125 126 FROM scratch AS bin-image 127 COPY --from=build /out/docker /docker 128 129 FROM scratch AS binary 130 COPY --from=build /out .