@@ -12,7 +12,7 @@ FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-alpine${ALPINE_VERSION} AS b
1212COPY --from=xx / /
1313RUN apk add --no-cache curl
1414RUN sh -c "$(curl --location https://taskfile.dev/install.sh)" -- -d -b /usr/local/bin
15- RUN apk add --no-cache git ca-certificates openssh-client
15+ RUN apk add --no-cache git ca-certificates openssh-client zip
1616
1717FROM build-base AS build
1818ARG TARGETPLATFORM
@@ -27,20 +27,35 @@ RUN --mount=type=ssh \
2727COPY . ./
2828
2929FROM build AS binary
30- ENV CGO_ENABLED=0
30+ ARG TARGETOS
31+ ARG TARGETARCH
3132ARG BIN_NAME
33+ ARG NO_ARCHIVE
34+ ENV CGO_ENABLED=0
3235RUN --mount=type=cache,target=/root/.cache \
3336 --mount=type=cache,target=/go/pkg/mod \
3437 GIT_VERSION=$(git describe --tags | cut -c 2-) && \
38+ PKG_NAME=$(go mod graph | head -n 1 | cut -d ' ' -f 1) && \
3539 xx-go build \
3640 -o dist/${BIN_NAME} \
3741 -ldflags="-w -s \
38- -X {{. PKG_NAME}} /internal/constants.Version=$GIT_VERSION" \
42+ -X $ PKG_NAME/internal/constants.Version=$GIT_VERSION" \
3943 ./cmd/${BIN_NAME} && \
40- xx-verify dist/${BIN_NAME}
44+ xx-verify dist/${BIN_NAME} && \
45+ if [ -z "${NO_ARCHIVE}" ]; then \
46+ # on windows add the .exe extension and zip the binary \
47+ if [ "${TARGETOS}" = "windows" ]; then \
48+ mv dist/${BIN_NAME} dist/${BIN_NAME}.exe && \
49+ (cd dist && zip ${BIN_NAME}-${TARGETOS}-${TARGETARCH}.zip ${BIN_NAME}.exe && rm -f ${BIN_NAME}.exe); \
50+ fi && \
51+ # if target os is not windows, tar and gzip the binary \
52+ if [ "${TARGETOS}" != "windows" ]; then \
53+ tar -C dist -czf dist/${BIN_NAME}-${TARGETOS}-${TARGETARCH}.tar.gz ${BIN_NAME} && rm -f dist/${BIN_NAME}; \
54+ fi \
55+ fi
4156
4257FROM scratch AS export-bin
4358ARG BIN_NAME
4459ARG TARGETOS
4560ARG TARGETARCH
46- COPY --from=binary /go/src/dist/${BIN_NAME} /${BIN_NAME}-${TARGETOS}-${TARGETARCH}
61+ COPY --from=binary /go/src/dist/* /
0 commit comments