|
| 1 | +FROM alpine:3.16 AS compiler |
| 2 | +ARG EXTENSION_VERSION |
| 3 | +ARG AGENT_VERSION |
| 4 | +ARG BUILD_TAGS |
| 5 | + |
| 6 | +RUN apk add --no-cache git make musl-dev gcc |
| 7 | +COPY --from=golang:1.23.6-alpine /usr/local/go/ /usr/lib/go |
| 8 | + |
| 9 | +ENV GOROOT=/usr/lib/go |
| 10 | +ENV GOPATH=/go |
| 11 | +ENV PATH=/go/bin:$PATH |
| 12 | + |
| 13 | +RUN mkdir -p ${GOPATH}/src ${GOPATH}/bin |
| 14 | +RUN mkdir -p /tmp/dd/datadog-agent |
| 15 | + |
| 16 | +# cache dependencies |
| 17 | +COPY ./scripts/.cache/go.mod /tmp/dd/datadog-agent |
| 18 | +COPY ./scripts/.cache/go.sum /tmp/dd/datadog-agent |
| 19 | +WORKDIR /tmp/dd/datadog-agent |
| 20 | + |
| 21 | +# copy source files (/tgz gets unzip automatically by Docker) |
| 22 | +ADD ./scripts/.src/datadog-agent.tgz /tmp/dd |
| 23 | + |
| 24 | +# build the extension |
| 25 | +WORKDIR /tmp/dd/datadog-agent/cmd/serverless |
| 26 | +# add the current version number to the tags package before compilation |
| 27 | + |
| 28 | +RUN --mount=type=cache,target=/go/pkg/mod \ |
| 29 | + --mount=type=cache,target=/root/.cache/go-build \ |
| 30 | + if [ -z "$AGENT_VERSION" ]; then \ |
| 31 | + /usr/lib/go/bin/go build -ldflags="-w -extldflags '-static' \ |
| 32 | + -X github.com/DataDog/datadog-agent/pkg/serverless/tags.currentExtensionVersion=$EXTENSION_VERSION" \ |
| 33 | + -tags "${BUILD_TAGS}" -o datadog-agent; \ |
| 34 | + else \ |
| 35 | + /usr/lib/go/bin/go build -ldflags="-w -extldflags '-static' \ |
| 36 | + -X github.com/DataDog/datadog-agent/pkg/serverless/tags.currentExtensionVersion=$EXTENSION_VERSION \ |
| 37 | + -X github.com/DataDog/datadog-agent/pkg/version.agentVersionDefault=$AGENT_VERSION" \ |
| 38 | + -tags "${BUILD_TAGS}" -o datadog-agent; \ |
| 39 | + fi |
| 40 | + |
| 41 | +RUN /usr/lib/go/bin/go tool nm datadog-agent | grep -w 'github.com/DataDog/datadog-agent/pkg/version.agentVersionDefault' || \ |
| 42 | + (echo "agentVersionDefault variable doesn't exist" && exit 1) |
| 43 | + |
| 44 | +RUN strip datadog-agent |
| 45 | + |
| 46 | +# keep the smallest possible docker image |
| 47 | +FROM scratch |
| 48 | +COPY --from=compiler /tmp/dd/datadog-agent/cmd/serverless/datadog-agent / |
| 49 | +ENTRYPOINT ["/datadog-agent"] |
| 50 | + |
0 commit comments