Skip to content

Commit 72e3dca

Browse files
committed
docs: refresh Go example
- `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]>
1 parent 1ee061d commit 72e3dca

File tree

3 files changed

+58
-8
lines changed

3 files changed

+58
-8
lines changed

examples/go/Dockerfile

+20-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-alpine${ALPINE_VERSION} AS b
1212
COPY --from=xx / /
1313
RUN apk add --no-cache curl
1414
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
1616

1717
FROM build-base AS build
1818
ARG TARGETPLATFORM
@@ -27,20 +27,35 @@ RUN --mount=type=ssh \
2727
COPY . ./
2828

2929
FROM build AS binary
30-
ENV CGO_ENABLED=0
30+
ARG TARGETOS
31+
ARG TARGETARCH
3132
ARG BIN_NAME
33+
ARG NO_ARCHIVE
34+
ENV CGO_ENABLED=0
3235
RUN --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

4257
FROM scratch AS export-bin
4358
ARG BIN_NAME
4459
ARG TARGETOS
4560
ARG TARGETARCH
46-
COPY --from=binary /go/src/dist/${BIN_NAME} /${BIN_NAME}-${TARGETOS}-${TARGETARCH}
61+
COPY --from=binary /go/src/dist/* /

examples/go/README.md

+20-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Go packages and cache are shared with `golangci-lint` to speed up the process.
1212

1313
### Build
1414

15-
`build` action will build the codebase using `docker buildx` and output as (multi-platform) binaries.
15+
`build` action will build the codebase using `docker buildx` and output as binary.
1616

1717
By convention if the binary name is `tool`, it will try to build `cmd/tool/` and the output will be inside `dist` directory.
1818

@@ -26,11 +26,29 @@ images:
2626
build:
2727
opts:
2828
bin_name: <bin name>
29-
platforms: <platforms to build against>
3029
```
3130
3231
That way you will be able to run `docker runx build` without to specify anything else.
3332

33+
### Multi platform builds
34+
35+
`build:all` allows to create multi-platform builds using `docker buildx`.
36+
37+
The generated binaries will be compressed and stored in the `dist` directory.
38+
39+
To make it more convenient, define the following in `.docker/runx.yaml`:
40+
41+
```yaml
42+
ref: eunomie/go
43+
images:
44+
eunomie/go:
45+
actions:
46+
build:all:
47+
opts:
48+
bin_name: <bin name>
49+
platforms: <comma separated list of platforms>
50+
```
51+
3452
### Mocks
3553

3654
`mocks` action will generate mocks for the codebase using `mockery`.

examples/go/runx.yaml

+18-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,23 @@ actions:
2020
golangci-lint run --timeout 5m
2121
2222
- id: build
23+
type: build
24+
dockerfile: Dockerfile
25+
opts:
26+
- name: bin_name
27+
desc: The name of the binary that also defines the build target as cmd/<bin_name>
28+
prompt: Please enter then name of the binary
29+
required: true
30+
cmd: >
31+
-f {{.Dockerfile}}
32+
--build-arg BIN_NAME={{opt "bin_name"}}
33+
--build-arg NO_ARCHIVE=true
34+
--platform local
35+
--target export-bin
36+
--output type=local,dest=dist/
37+
.
38+
39+
- id: build:all
2340
type: build
2441
dockerfile: Dockerfile
2542
opts:
@@ -35,7 +52,7 @@ actions:
3552
--build-arg BIN_NAME={{opt "bin_name"}}
3653
{{if opt "platforms"}}--platform {{opt "platforms"}}{{end}}
3754
--target export-bin
38-
--output type=local,dest=dist/
55+
--output type=local,dest=dist/,platform-split=false
3956
.
4057
4158
- id: mocks

0 commit comments

Comments
 (0)