Skip to content

Commit 06144ae

Browse files
committed
chore(build): Use goreleaser for building
Signed-off-by: Grzegorz Głąb <[email protected]>
1 parent 66b532b commit 06144ae

File tree

5 files changed

+126
-18
lines changed

5 files changed

+126
-18
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
thing-controller
2+
dist

.goreleaser.yaml

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
project_name: thing-controller
2+
env:
3+
- REPOSITORY_OWNER={{ if index .Env "REPOSITORY_OWNER" }}{{ .Env.REPOSITORY_OWNER }}{{ else }}local{{ end }}
4+
- GC_FLAGS={{ if index .Env "DEBUG" }}all=-N -l{{ else }}-c 1{{ end }}
5+
- REMOVE_DEBUG_SYMBOLS={{ if index .Env "DEBUG" }}{{ else }}-w{{ end }}
6+
builds:
7+
- env:
8+
- CGO_ENABLED=0
9+
goos:
10+
- linux
11+
- windows
12+
- darwin
13+
goarch:
14+
- amd64
15+
- arm
16+
- arm64
17+
- 386
18+
goarm:
19+
- 6
20+
- 7
21+
ldflags:
22+
- -s {{ .Env.REMOVE_DEBUG_SYMBOLS }} -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}} -X main.builtBy=goreleaser
23+
gcflags:
24+
- '{{ .Env.GC_FLAGS }}'
25+
# probably won't be used
26+
archives:
27+
- replacements:
28+
darwin: Darwin
29+
linux: Linux
30+
windows: Windows
31+
386: i386
32+
amd64: x86_64
33+
dockers:
34+
- image_templates:
35+
- "{{ .Env.REPOSITORY_OWNER }}io/{{ .ProjectName }}:v{{ .Version }}-amd64"
36+
- "ghcr.io/{{ .Env.REPOSITORY_OWNER }}/{{ .ProjectName }}:v{{ .Version }}-amd64"
37+
dockerfile: Dockerfile
38+
use: buildx
39+
build_flag_templates:
40+
- --platform=linux/amd64
41+
- --label=org.opencontainers.image.title={{ .ProjectName }}
42+
- --label=org.opencontainers.image.description={{ .ProjectName }}
43+
- --label=org.opencontainers.image.url=https://github.com/metacontroller/{{ .ProjectName }}
44+
- --label=org.opencontainers.image.source=https://github.com/metacontroller/{{ .ProjectName }}
45+
- --label=org.opencontainers.image.version=v{{ .Version }}
46+
- --label=org.opencontainers.image.created={{ .Date }}
47+
- --label=org.opencontainers.image.revision={{ .FullCommit }}
48+
- --label=org.opencontainers.image.licenses=Apache-2.0
49+
- image_templates:
50+
- "{{ .Env.REPOSITORY_OWNER }}io/{{ .ProjectName }}:v{{ .Version }}-arm64v8"
51+
- "ghcr.io/{{ .Env.REPOSITORY_OWNER }}/{{ .ProjectName }}:v{{ .Version }}-arm64v8"
52+
goarch: arm64
53+
dockerfile: Dockerfile
54+
use: buildx
55+
build_flag_templates:
56+
- --platform=linux/arm64/v8
57+
- --label=org.opencontainers.image.title={{ .ProjectName }}
58+
- --label=org.opencontainers.image.description={{ .ProjectName }}
59+
- --label=org.opencontainers.image.url=https://github.com/metacontroller/{{ .ProjectName }}
60+
- --label=org.opencontainers.image.source=https://github.com/metacontroller/{{ .ProjectName }}
61+
- --label=org.opencontainers.image.version=v{{ .Version }}
62+
- --label=org.opencontainers.image.created={{ .Date }}
63+
- --label=org.opencontainers.image.revision={{ .FullCommit }}
64+
- --label=org.opencontainers.image.licenses=Apache-2.0
65+
- image_templates:
66+
- "{{ .Env.REPOSITORY_OWNER }}io/{{ .ProjectName }}:v{{ .Version }}-armv7"
67+
- "ghcr.io/{{ .Env.REPOSITORY_OWNER }}/{{ .ProjectName }}:v{{ .Version }}-armv7"
68+
goarch: arm64
69+
dockerfile: Dockerfile
70+
use: buildx
71+
build_flag_templates:
72+
- --platform=linux/arm/v7
73+
- --label=org.opencontainers.image.title={{ .ProjectName }}
74+
- --label=org.opencontainers.image.description={{ .ProjectName }}
75+
- --label=org.opencontainers.image.url=https://github.com/metacontroller/{{ .ProjectName }}
76+
- --label=org.opencontainers.image.source=https://github.com/metacontroller/{{ .ProjectName }}
77+
- --label=org.opencontainers.image.version=v{{ .Version }}
78+
- --label=org.opencontainers.image.created={{ .Date }}
79+
- --label=org.opencontainers.image.revision={{ .FullCommit }}
80+
- --label=org.opencontainers.image.licenses=Apache-2.0
81+
docker_manifests:
82+
- name_template: ghcr.io/{{ .Env.REPOSITORY_OWNER }}/{{ .ProjectName }}:v{{ .Version }}
83+
skip_push: "false"
84+
image_templates:
85+
- "ghcr.io/{{ .Env.REPOSITORY_OWNER }}/{{ .ProjectName }}:v{{ .Version }}-amd64"
86+
- "ghcr.io/{{ .Env.REPOSITORY_OWNER }}/{{ .ProjectName }}:v{{ .Version }}-arm64v8"
87+
- "ghcr.io/{{ .Env.REPOSITORY_OWNER }}/{{ .ProjectName }}:v{{ .Version }}-armv7"
88+
- name_template: ghcr.io/{{ .Env.REPOSITORY_OWNER }}/{{ .ProjectName }}:latest
89+
skip_push: "false"
90+
image_templates:
91+
- ghcr.io/{{ .Env.REPOSITORY_OWNER }}/{{ .ProjectName }}:v{{ .Version }}-amd64
92+
- ghcr.io/{{ .Env.REPOSITORY_OWNER }}/{{ .ProjectName }}:v{{ .Version }}-arm64v8
93+
- ghcr.io/{{ .Env.REPOSITORY_OWNER }}/{{ .ProjectName }}:v{{ .Version }}-armv7
94+
checksum:
95+
name_template: 'checksums.txt'
96+
snapshot:
97+
name_template: "{{ incpatch .Version }}-next"
98+
release:
99+
mode: append
100+
footer: |
101+
## Docker Images
102+
### Github container registry
103+
- `ghcr.io/{{ .Env.REPOSITORY_OWNER }}/{{ .ProjectName }}:v{{ .Version }}`

Dockerfile

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
FROM golang:1.19 AS build
1+
FROM alpine:3.16.2@sha256:65a2763f593ae85fab3b5406dc9e80f744ec5b449f269b699b5efd37a07ad32e
2+
COPY thing-controller /usr/bin/thing-controller
3+
RUN apk update && apk add --no-cache ca-certificates
24

3-
COPY . /go/src/thing-controller
4-
WORKDIR /go/src/thing-controller
5-
RUN go mod vendor && go build -o /go/bin/thing-controller
5+
# Run container as nonroot, use the same uid and naming convention as distroless images
6+
# See https://github.com/GoogleContainerTools/distroless/blob/0d757ece34cdc83a2148cea6c697e262c333cb84/base/base.bzl#L8
7+
RUN addgroup -g 65532 -S nonroot && adduser -D -u 65532 -g nonroot -S nonroot -G nonroot
8+
USER nonroot:nonroot
69

7-
FROM debian:stretch-slim
8-
9-
COPY --from=build /go/bin/thing-controller /usr/bin/thing-controller
10+
CMD ["/usr/bin/thing-controller"]

Makefile

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
PWD := ${CURDIR}
2-
ADDITIONAL_BUILD_ARGUMENTS?=""
3-
4-
PKG := metacontroller
5-
API_GROUPS := metacontroller/v1alpha1
2+
TAG?= dev
63

74
CODE_GENERATOR_VERSION="v0.24.3"
85

@@ -13,3 +10,12 @@ generate_crds:
1310
@echo "+ Generating crds"
1411
@go install sigs.k8s.io/controller-tools/cmd/controller-gen@latest
1512
@controller-gen +crd:generateEmbeddedObjectMeta=true +paths="./api/..." +output:crd:stdout > crdv1.yaml
13+
14+
.PHONY: build
15+
build:
16+
@echo "+ Building...."
17+
goreleaser build --single-target --rm-dist --snapshot --output .
18+
19+
.PHONY: image
20+
image: build
21+
docker build -t thing-controller:$(TAG) -f Dockerfile .

README.md

+4-7
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,13 @@ kubectl delete -f thing-controller.yaml
5454

5555
### Building
5656

57-
You don't need to build to run the example above,
58-
but if you make changes:
57+
We use `goreleaser` to build. Once is installed, run
5958

6059
```sh
61-
go mod vendor
62-
go build -o thing-controller
60+
make build
6361
```
64-
65-
Or just make a new container image:
62+
or just make a new container image:
6663

6764
```sh
68-
docker build . -t thing-controller
65+
make image
6966
```

0 commit comments

Comments
 (0)