# Use the automatic platform ARG `BUILDPLATFORM` from Buildx to stick the builder on the native platform,
# so that we do not use emulation mechanism to build, which is VERY SLOW.
# Please refer to `https://docs.docker.com/engine/reference/builder/#from` for more info.
FROM --platform=$BUILDPLATFORM gcr.io/pingcap-public/third-party/baseimage/golang:1.25.12 AS builder

ARG GITHUB_TOKEN=ghp_LxxxxxF
RUN git config --global url."https://${GITHUB_TOKEN}@github.com/".insteadOf "https://github.com/"
WORKDIR /build

COPY . ./
# Activate the automatic platform ARG `TARGETOS` and `TARGETARCH` from Buildx,
# so that we can build different binaries for different OSes and archs.
# Please refer to `https://docs.docker.com/engine/reference/builder/#automatic-platform-args-in-the-global-scope` for more info.
ARG TARGETOS TARGETARCH
RUN cd dataflow-agent-ng && GOOS=$TARGETOS GOARCH=$TARGETARCH make build-dataflow-agent

# dataflow-agent is built with CGO_ENABLED=0 (statically linked), so plain alpine
# without glibc is sufficient at runtime.
FROM gcr.io/pingcap-public/pingcap/alpine:3.22.1
# The internal base image is a point-in-time snapshot; pull the latest security
# patches for OS packages (openssl, musl, busybox, zlib, ...) at build time.
RUN apk upgrade --no-cache
COPY --from=builder /build/dataflow-agent-ng/dist/dataflow-agent /dataflow-agent

CMD ["/dataflow-agent"]
