Skip to content

Commit 21c3cd2

Browse files
committed
build & ci improvements
* Dockerfile: Rewrote the Dockerfile to use a multi-stage build. The builder stage uses golang:1.24-alpine to compile the binary, and the runtime stage uses alpine:latest, significantly reducing the final image size and improving security. * Makefile: Added all common targets to .PHONY to prevent conflicts with local files/directories. * GitHub Actions: Integrated golangci-lint-action into the CI workflow to enforce code quality and best practice automatically on every push.
1 parent 029f26f commit 21c3cd2

3 files changed

Lines changed: 55 additions & 6 deletions

File tree

.github/workflows/go.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@ jobs:
1717
with:
1818
go-version: 1.24
1919

20+
- name: golangci-lint
21+
uses: golangci/golangci-lint-action@v6
22+
with:
23+
version: latest
24+
2025
- name: Build
21-
run: make lint build
26+
run: make build
2227

2328
- name: Test
2429
run: |

Dockerfile

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
1-
FROM golang:1.24
1+
# Stage 1: Build
2+
FROM golang:1.24-alpine AS builder
23

3-
ADD . /bert
4+
# Install make and git (required for Makefile and versioning)
5+
RUN apk add --no-cache make git
46

57
WORKDIR /bert
8+
COPY . .
69

10+
# Build the linux amd64 binary
711
RUN make go-build-linux-amd64
12+
13+
# Stage 2: Runtime
14+
FROM alpine:latest
15+
16+
RUN apk add --no-cache ca-certificates
17+
18+
WORKDIR /bert
19+
COPY --from=builder /bert/bin/bert-linux-amd64 /bert/bin/bert-linux-amd64
20+
COPY --from=builder /bert/test /bert/test
21+
22+
# Set the binary as the entrypoint
23+
ENTRYPOINT ["/bert/bin/bert-linux-amd64"]

Makefile

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,16 @@ PID := $(GOBUILD)/.$(PROJECTNAME).pid
3636
# Make is verbose in Linux. Make it silent.
3737
MAKEFLAGS += --silent
3838

39+
.PHONY: default
3940
default: install lint format test build
4041

42+
.PHONY: install
4143
install: go-get
4244

45+
.PHONY: format
4346
format: go-format
4447

48+
.PHONY: lint
4549
lint: go-lint
4650

4751
.PHONY: build
@@ -56,84 +60,105 @@ build:
5660
bin/bert-$(GOHOSTOS)-$(GOHOSTARCH) completion bash > $(GOBUILD)/completions/bert.bash
5761
bin/bert-$(GOHOSTOS)-$(GOHOSTARCH) completion fish > $(GOBUILD)/completions/bert.fish
5862

59-
#@cat $(STDERR) | sed -e '1s/.*/\nError:\n/' | sed 's/make\[.*/ /' | sed "/^/s/^/ /" 1>&2
63+
#@cat $(STDERR) | sed -e '1s/.*/\nError:\n/' | sed 's/make\[.*\]/ /' | sed "/^/s/^/ /" 1>&2
6064

6165

66+
.PHONY: test
6267
test: install go-test
6368

69+
.PHONY: clean
6470
clean:
6571
@-rm $(GOBIN)/$(PROGRAMNAME)* 2> /dev/null
6672
@-$(MAKE) go-clean
6773

74+
.PHONY: go-lint
6875
go-lint:
6976
@echo " > Linting source files..."
7077
go vet $(MODFLAGS) -c=10 `go list $(MODFLAGS) ./...`
7178

79+
.PHONY: go-format
7280
go-format:
7381
@echo " > Formating source files..."
7482
gofmt -s -w $(GOFILES)
7583

84+
.PHONY: go-build-current
7685
go-build-current:
7786
@echo " > Building $(GOHOSTOS)/$(GOHOSTARCH) binaries..."
7887
@GOPATH=$(GOPATH) GOOS=$(GOHOSTOS) GOARCH=$(GOHOSTARCH) GOBIN=$(GOBIN) go build $(MODFLAGS) $(LDFLAGS) -o $(GOBIN)/$(PROGRAMNAME) $(GOBASE)/cmd
7988

89+
.PHONY: go-build
8090
go-build: go-get go-build-linux-amd64 go-build-linux-arm64 go-build-linux-arm go-build-darwin-amd64 go-build-darwin-arm64 go-build-windows-amd64 go-build-windows-arm
8191

92+
.PHONY: go-test
8293
go-test:
8394
@echo " > Running Go tests..."
8495
go test $(MODFLAGS) -covermode=count `go list $(MODFLAGS) ./...`
8596

97+
.PHONY: go-build-linux-amd64
8698
go-build-linux-amd64:
8799
@echo " > Building linux amd64 binaries..."
88100
@GOPATH=$(GOPATH) GOOS=$(GOOS_LINUX) GOARCH=$(GOARCH_AMD64) GOBIN=$(GOBIN) go build $(MODFLAGS) $(LDFLAGS) -o $(GOBIN)/$(PROGRAMNAME)-$(GOOS_LINUX)-$(GOARCH_AMD64) $(GOBASE)/cmd
89101

102+
.PHONY: go-build-linux-arm64
90103
go-build-linux-arm64:
91104
@echo " > Building linux arm64 binaries..."
92105
@GOPATH=$(GOPATH) GOOS=$(GOOS_LINUX) GOARCH=$(GOARCH_ARM64) GOBIN=$(GOBIN) go build $(MODFLAGS) $(LDFLAGS) -o $(GOBIN)/$(PROGRAMNAME)-$(GOOS_LINUX)-$(GOARCH_ARM64) $(GOBASE)/cmd
93106

107+
.PHONY: go-build-linux-arm
94108
go-build-linux-arm:
95109
@echo " > Building linux arm binaries..."
96110
@GOPATH=$(GOPATH) GOOS=$(GOOS_LINUX) GOARCH=$(GOARCH_ARM) GOBIN=$(GOBIN) go build $(MODFLAGS) $(LDFLAGS) -o $(GOBIN)/$(PROGRAMNAME)-$(GOOS_LINUX)-$(GOARCH_ARM) $(GOBASE)/cmd
97111

112+
.PHONY: go-build-darwin-amd64
98113
go-build-darwin-amd64:
99114
@echo " > Building darwin amd64 binaries..."
100115
@GOPATH=$(GOPATH) GOOS=$(GOOS_DARWIN) GOARCH=$(GOARCH_AMD64) GOBIN=$(GOBIN) go build $(MODFLAGS) $(LDFLAGS) -o $(GOBIN)/$(PROGRAMNAME)-$(GOOS_DARWIN)-$(GOARCH_AMD64) $(GOBASE)/cmd
101116

117+
.PHONY: go-build-darwin-arm64
102118
go-build-darwin-arm64:
103119
@echo " > Building darwin arm64 binaries..."
104120
@GOPATH=$(GOPATH) GOOS=$(GOOS_DARWIN) GOARCH=$(GOARCH_ARM64) GOBIN=$(GOBIN) go build $(MODFLAGS) $(LDFLAGS) -o $(GOBIN)/$(PROGRAMNAME)-$(GOOS_DARWIN)-$(GOARCH_ARM64) $(GOBASE)/cmd
105121

122+
.PHONY: go-build-windows-amd64
106123
go-build-windows-amd64:
107124
@echo " > Building windows amd64 binaries..."
108125
@GOPATH=$(GOPATH) GOOS=$(GOOS_WINDOWS) GOARCH=$(GOARCH_AMD64) GOBIN=$(GOBIN) go build $(MODFLAGS) $(LDFLAGS) -o $(GOBIN)/$(PROGRAMNAME)-$(GOOS_WINDOWS)-$(GOARCH_AMD64).exe $(GOBASE)/cmd
109126

127+
.PHONY: go-build-windows-arm
110128
go-build-windows-arm:
111129
@echo " > Building windows arm binaries..."
112130
@GOPATH=$(GOPATH) GOOS=$(GOOS_WINDOWS) GOARCH=$(GOARCH_ARM) GOBIN=$(GOBIN) go build $(MODFLAGS) $(LDFLAGS) -o $(GOBIN)/$(PROGRAMNAME)-$(GOOS_WINDOWS)-$(GOARCH_ARM).exe $(GOBASE)/cmd
113131

132+
.PHONY: go-generate
114133
go-generate:
115134
@echo " > Generating dependency files..."
116135
@GOPATH=$(GOPATH) GOBIN=$(GOBIN) go generate $(generate)
117136

137+
.PHONY: go-get
118138
go-get:
119139
@echo " > Checking if there is any missing dependencies..."
120140
@GOPATH=$(GOPATH) GOBIN=$(GOBIN) go mod tidy
121141

142+
.PHONY: go-install
122143
go-install:
123144
@GOPATH=$(GOPATH) GOBIN=$(GOBIN) go install $(GOFILES)
124145

146+
.PHONY: go-clean
125147
go-clean:
126148
@echo " > Cleaning build cache"
127149
@GOPATH=$(GOPATH) GOBIN=$(GOBIN) go clean $(MODFLAGS) $(GOBASE)/cmd
128150
@GOPATH=$(GOPATH) GOBIN=$(GOBIN) go clean -modcache
129151

152+
.PHONY: run-sanity-tests
130153
run-sanity-tests: build-docker run-linux-dockerized-tests
131154

155+
.PHONY: build-docker
132156
build-docker:
133157
@echo " > Building docker image..."
134158
docker build -t sha1n/$(PROJECTNAME):latest .
135159
docker tag sha1n/$(PROJECTNAME):latest sha1n/$(PROJECTNAME):$(VERSION:v%=%)
136160

161+
.PHONY: run-linux-dockerized-tests
137162
run-linux-dockerized-tests:
138163
@echo " > Running with experimental UI..."
139164
docker run --rm -ti sha1n/bert /bert/bin/bert-linux-amd64 -c /bert/test/data/spec_test_load.yaml
@@ -148,6 +173,7 @@ run-linux-dockerized-tests:
148173
@echo " > Running with ad-hoc commands..."
149174
docker run --rm -ti sha1n/bert /bert/bin/bert-linux-amd64 'ls' 'ls -laH' --executions 10
150175

176+
.PHONY: release
151177
release:
152178
ifdef GITHUB_TOKEN
153179
@echo " > Releasing..."
@@ -156,11 +182,13 @@ else
156182
$(error GITHUB_TOKEN is not set)
157183
endif
158184

159-
.PHONY: help
185+
.PHONY: all
160186
all: help
187+
188+
.PHONY: help
161189
help: Makefile
162190
@echo
163191
@echo " Choose a command run in "$(PROJECTNAME)":"
164192
@echo
165193
@sed -n 's/^##//p' $< | column -t -s ':' | sed -e 's/^/ /'
166-
@echo
194+
@echo

0 commit comments

Comments
 (0)