@@ -12,7 +12,7 @@ FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-alpine${ALPINE_VERSION} AS b
12
12
COPY --from=xx / /
13
13
RUN apk add --no-cache curl
14
14
RUN 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
16
16
17
17
FROM build-base AS build
18
18
ARG TARGETPLATFORM
@@ -27,20 +27,35 @@ RUN --mount=type=ssh \
27
27
COPY . ./
28
28
29
29
FROM build AS binary
30
- ENV CGO_ENABLED=0
30
+ ARG TARGETOS
31
+ ARG TARGETARCH
31
32
ARG BIN_NAME
33
+ ARG NO_ARCHIVE
34
+ ENV CGO_ENABLED=0
32
35
RUN --mount=type=cache,target=/root/.cache \
33
36
--mount=type=cache,target=/go/pkg/mod \
34
37
GIT_VERSION=$(git describe --tags | cut -c 2-) && \
38
+ PKG_NAME=$(go mod graph | head -n 1 | cut -d ' ' -f 1) && \
35
39
xx-go build \
36
40
-o dist/${BIN_NAME} \
37
41
-ldflags="-w -s \
38
- -X {{. PKG_NAME}} /internal/constants.Version=$GIT_VERSION" \
42
+ -X $ PKG_NAME/internal/constants.Version=$GIT_VERSION" \
39
43
./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
41
56
42
57
FROM scratch AS export-bin
43
58
ARG BIN_NAME
44
59
ARG TARGETOS
45
60
ARG TARGETARCH
46
- COPY --from=binary /go/src/dist/${BIN_NAME} /${BIN_NAME}-${TARGETOS}-${TARGETARCH}
61
+ COPY --from=binary /go/src/dist/* /
0 commit comments