Skip to content

Commit

Permalink
docs: refresh Go example
Browse files Browse the repository at this point in the history
- `build` action now only build for the local platform
- `build:all` action builds for multi-platform and compress the binaries

Signed-off-by: Yves Brissaud <[email protected]>
  • Loading branch information
eunomie committed Oct 10, 2024
1 parent 1ee061d commit ddbe02c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
25 changes: 20 additions & 5 deletions examples/go/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-alpine${ALPINE_VERSION} AS b
COPY --from=xx / /
RUN apk add --no-cache curl
RUN sh -c "$(curl --location https://taskfile.dev/install.sh)" -- -d -b /usr/local/bin
RUN apk add --no-cache git ca-certificates openssh-client
RUN apk add --no-cache git ca-certificates openssh-client zip

FROM build-base AS build
ARG TARGETPLATFORM
Expand All @@ -27,20 +27,35 @@ RUN --mount=type=ssh \
COPY . ./

FROM build AS binary
ENV CGO_ENABLED=0
ARG TARGETOS
ARG TARGETARCH
ARG BIN_NAME
ARG NO_ARCHIVE
ENV CGO_ENABLED=0
RUN --mount=type=cache,target=/root/.cache \
--mount=type=cache,target=/go/pkg/mod \
GIT_VERSION=$(git describe --tags | cut -c 2-) && \
PKG_NAME=$(go mod graph | head -n 1 | cut -d ' ' -f 1) && \
xx-go build \
-o dist/${BIN_NAME} \
-ldflags="-w -s \
-X {{.PKG_NAME}}/internal/constants.Version=$GIT_VERSION" \
-X $PKG_NAME/internal/constants.Version=$GIT_VERSION" \
./cmd/${BIN_NAME} && \
xx-verify dist/${BIN_NAME}
xx-verify dist/${BIN_NAME} && \
if [ -z "${NO_ARCHIVE}" ]; then \
# on windows add the .exe extension and zip the binary \
if [ "${TARGETOS}" = "windows" ]; then \
mv dist/${BIN_NAME} dist/${BIN_NAME}.exe && \
(cd dist && zip ${BIN_NAME}-${TARGETOS}-${TARGETARCH}.zip ${BIN_NAME}.exe && rm -f ${BIN_NAME}.exe); \
fi && \
# if target os is not windows, tar and gzip the binary \
if [ "${TARGETOS}" != "windows" ]; then \
tar -C dist -czf dist/${BIN_NAME}-${TARGETOS}-${TARGETARCH}.tar.gz ${BIN_NAME} && rm -f dist/${BIN_NAME}; \
fi \
fi

FROM scratch AS export-bin
ARG BIN_NAME
ARG TARGETOS
ARG TARGETARCH
COPY --from=binary /go/src/dist/${BIN_NAME} /${BIN_NAME}-${TARGETOS}-${TARGETARCH}
COPY --from=binary /go/src/dist/* /
19 changes: 18 additions & 1 deletion examples/go/runx.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ actions:
golangci-lint run --timeout 5m
- id: build
type: build
dockerfile: Dockerfile
opts:
- name: bin_name
desc: The name of the binary that also defines the build target as cmd/<bin_name>
prompt: Please enter then name of the binary
required: true
cmd: >
-f {{.Dockerfile}}
--build-arg BIN_NAME={{opt "bin_name"}}
--build-arg NO_ARCHIVE=true
--platform local
--target export-bin
--output type=local,dest=dist/
.
- id: build:all
type: build
dockerfile: Dockerfile
opts:
Expand All @@ -35,7 +52,7 @@ actions:
--build-arg BIN_NAME={{opt "bin_name"}}
{{if opt "platforms"}}--platform {{opt "platforms"}}{{end}}
--target export-bin
--output type=local,dest=dist/
--output type=local,dest=dist/,platform-split=false
.
- id: mocks
Expand Down

0 comments on commit ddbe02c

Please sign in to comment.