Skip to content

Commit 1fd291f

Browse files
authored
Merge pull request #3916 from apostasie/ci-make-tasks
CI: spring cleaning Makefile
2 parents d307278 + 9210685 commit 1fd291f

File tree

4 files changed

+231
-42
lines changed

4 files changed

+231
-42
lines changed

.github/workflows/lint.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,13 @@ jobs:
6868
with:
6969
go-version: ${{ env.GO_VERSION }}
7070
check-latest: true
71+
- name: install required linters and dev-tools
72+
run: |
73+
make install-dev-tools
7174
- name: yaml
7275
run: make lint-yaml
7376
- name: shell
7477
run: make lint-shell
7578
- name: go imports ordering
7679
run: |
77-
go install -v github.com/incu6us/goimports-reviser/v3@latest
7880
make lint-imports

.golangci.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,12 @@ linters-settings:
136136
- unnecessaryBlock
137137

138138
issues:
139+
max-issues-per-linter: 0
140+
max-same-issues: 0
139141
exclude-rules:
140142
- linters:
141143
- revive
142144
text: "unused-parameter"
145+
146+
output:
147+
sort-results: true

Makefile

Lines changed: 196 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,85 +18,240 @@
1818
# Licensed under the Apache License, Version 2.0
1919
# -----------------------------------------------------------------------------
2020

21+
##########################
22+
# Configuration
23+
##########################
24+
PACKAGE := "github.com/containerd/nerdctl/v2"
25+
ORG_PREFIXES := "github.com/containerd"
26+
2127
DOCKER ?= docker
2228
GO ?= go
2329
GOOS ?= $(shell $(GO) env GOOS)
2430
ifeq ($(GOOS),windows)
2531
BIN_EXT := .exe
2632
endif
2733

28-
PACKAGE := github.com/containerd/nerdctl/v2
29-
30-
# distro builders might wanna override these
34+
# distro builders might want to override these
3135
PREFIX ?= /usr/local
3236
BINDIR ?= $(PREFIX)/bin
3337
DATADIR ?= $(PREFIX)/share
3438
DOCDIR ?= $(DATADIR)/doc
3539

40+
BINARY ?= "nerdctl"
3641
MAKEFILE_DIR := $(patsubst %/,%,$(dir $(abspath $(lastword $(MAKEFILE_LIST)))))
3742
VERSION ?= $(shell git -C $(MAKEFILE_DIR) describe --match 'v[0-9]*' --dirty='.m' --always --tags)
3843
VERSION_TRIMMED := $(VERSION:v%=%)
3944
REVISION ?= $(shell git -C $(MAKEFILE_DIR) rev-parse HEAD)$(shell if ! git -C $(MAKEFILE_DIR) diff --no-ext-diff --quiet --exit-code; then echo .m; fi)
45+
LINT_COMMIT_RANGE ?= main..HEAD
46+
GO_BUILD_LDFLAGS ?= -s -w
47+
GO_BUILD_FLAGS ?=
4048

49+
##########################
50+
# Helpers
51+
##########################
4152
ifdef VERBOSE
4253
VERBOSE_FLAG := -v
4354
VERBOSE_FLAG_LONG := --verbose
4455
endif
4556

46-
GO_BUILD_LDFLAGS ?= -s -w
47-
GO_BUILD_FLAGS ?=
4857
export GO_BUILD=CGO_ENABLED=0 GOOS=$(GOOS) $(GO) -C $(MAKEFILE_DIR) build -ldflags "$(GO_BUILD_LDFLAGS) $(VERBOSE_FLAG) -X $(PACKAGE)/pkg/version.Version=$(VERSION) -X $(PACKAGE)/pkg/version.Revision=$(REVISION)"
4958

59+
ifndef NO_COLORS
60+
NC := \033[0m
61+
GREEN := \033[1;32m
62+
ORANGE := \033[1;33m
63+
endif
64+
5065
recursive_wildcard=$(wildcard $1$2) $(foreach e,$(wildcard $1*),$(call recursive_wildcard,$e/,$2))
5166

67+
define title
68+
@printf "$(GREEN)____________________________________________________________________________________________________\n"
69+
@printf "$(GREEN)%*s\n" $$(( ( $(shell echo "🤓$(1) 🤓" | wc -c ) + 100 ) / 2 )) "🤓$(1) 🤓"
70+
@printf "$(GREEN)____________________________________________________________________________________________________\n$(ORANGE)"
71+
endef
72+
73+
define footer
74+
@printf "$(GREEN)> %s: done!\n" "$(1)"
75+
@printf "$(GREEN)____________________________________________________________________________________________________\n$(NC)"
76+
endef
77+
78+
##########################
79+
# High-level tasks definitions
80+
##########################
5281
all: binaries
5382

83+
lint: lint-go-all lint-imports lint-yaml lint-shell lint-commits lint-mod lint-licenses-all
84+
85+
fix: fix-mod fix-imports fix-go-all
86+
87+
# TODO: fix race task and add it
88+
test: test-unit # test-unit-race test-unit-bench
89+
5490
help:
5591
@echo "Usage: make <target>"
5692
@echo
57-
@echo " * 'install' - Install binaries to system locations."
93+
@echo " * 'lint' - Run linters against codebase."
94+
@echo " * 'fix' - Automatically fixes imports, modules, and simple formatting."
95+
@echo " * 'test' - Run basic unit testing."
5896
@echo " * 'binaries' - Build nerdctl."
97+
@echo " * 'install' - Install binaries to system locations."
5998
@echo " * 'clean' - Clean artifacts."
60-
@echo " * 'lint' - Run various linters."
6199

62-
nerdctl:
63-
$(GO_BUILD) $(GO_BUILD_FLAGS) $(VERBOSE_FLAG) -o $(CURDIR)/_output/nerdctl$(BIN_EXT) ./cmd/nerdctl
100+
##########################
101+
# Building and installation tasks
102+
##########################
103+
binaries: $(CURDIR)/_output/$(BINARY)$(BIN_EXT)
104+
105+
$(CURDIR)/_output/$(BINARY)$(BIN_EXT):
106+
$(call title, $@)
107+
$(GO_BUILD) $(GO_BUILD_FLAGS) $(VERBOSE_FLAG) -o $(CURDIR)/_output/$(BINARY)$(BIN_EXT) ./cmd/nerdctl
108+
$(call footer, $@)
109+
110+
install:
111+
$(call title, $@)
112+
install -D -m 755 $(CURDIR)/_output/$(BINARY) $(DESTDIR)$(BINDIR)/$(BINARY)
113+
install -D -m 755 $(MAKEFILE_DIR)/extras/rootless/containerd-rootless.sh $(DESTDIR)$(BINDIR)/containerd-rootless.sh
114+
install -D -m 755 $(MAKEFILE_DIR)/extras/rootless/containerd-rootless-setuptool.sh $(DESTDIR)$(BINDIR)/containerd-rootless-setuptool.sh
115+
install -D -m 644 -t $(DESTDIR)$(DOCDIR)/nerdctl $(MAKEFILE_DIR)/docs/*.md
116+
$(call footer, $@)
64117

65118
clean:
119+
$(call title, $@)
66120
find . -name \*~ -delete
67121
find . -name \#\* -delete
68122
rm -rf $(CURDIR)/_output/* $(MAKEFILE_DIR)/vendor
123+
$(call footer, $@)
69124

70-
lint: lint-go lint-imports lint-yaml lint-shell
71-
125+
##########################
126+
# Linting tasks
127+
##########################
72128
lint-go:
73-
cd $(MAKEFILE_DIR) && GOOS=linux golangci-lint run $(VERBOSE_FLAG_LONG) ./... && \
74-
GOOS=windows golangci-lint run $(VERBOSE_FLAG_LONG) ./... && \
75-
GOOS=freebsd golangci-lint run $(VERBOSE_FLAG_LONG) ./...
129+
$(call title, $@: $(GOOS))
130+
@cd $(MAKEFILE_DIR) \
131+
&& golangci-lint run $(VERBOSE_FLAG_LONG) ./...
132+
$(call footer, $@)
133+
134+
lint-go-all:
135+
$(call title, $@)
136+
@cd $(MAKEFILE_DIR) \
137+
&& GOOS=linux make lint-go \
138+
&& GOOS=windows make lint-go \
139+
&& GOOS=freebsd make lint-go
140+
$(call footer, $@)
76141

77142
lint-imports:
78-
cd $(MAKEFILE_DIR) && goimports-reviser -recursive -list-diff -set-exit-status -output stdout -company-prefixes "github.com/containerd" ./...
79-
80-
lint-fix-imports:
81-
cd $(MAKEFILE_DIR) && goimports-reviser -company-prefixes "github.com/containerd" ./...
143+
$(call title, $@)
144+
@cd $(MAKEFILE_DIR) \
145+
&& goimports-reviser -recursive -list-diff -set-exit-status -output stdout -company-prefixes "$(ORG_PREFIXES)" ./...
146+
$(call footer, $@)
82147

83148
lint-yaml:
84-
cd $(MAKEFILE_DIR) && yamllint .
149+
$(call title, $@)
150+
cd $(MAKEFILE_DIR) \
151+
&& yamllint .
152+
$(call footer, $@)
85153

86154
lint-shell: $(call recursive_wildcard,$(MAKEFILE_DIR)/,*.sh)
155+
$(call title, $@)
87156
shellcheck -a -x $^
88-
157+
$(call footer, $@)
158+
159+
lint-commits:
160+
$(call title, $@)
161+
@cd $(MAKEFILE_DIR) \
162+
&& git-validation $(VERBOSE_FLAG) -run DCO,short-subject,dangling-whitespace -range "$(LINT_COMMIT_RANGE)"
163+
$(call footer, $@)
164+
165+
lint-mod:
166+
$(call title, $@)
167+
@cd $(MAKEFILE_DIR) \
168+
&& go mod tidy --diff
169+
$(call footer, $@)
170+
171+
lint-licenses:
172+
$(call title, $@: $(GOOS))
173+
@cd $(MAKEFILE_DIR) \
174+
&& ./hack/make-lint-licenses.sh
175+
$(call footer, $@)
176+
177+
lint-licenses-all:
178+
$(call title, $@)
179+
@cd $(MAKEFILE_DIR) \
180+
&& GOOS=linux make lint-licenses \
181+
&& GOOS=freebsd make lint-licenses \
182+
&& GOOS=windows make lint-licenses
183+
$(call footer, $@)
184+
185+
##########################
186+
# Automated fixing tasks
187+
##########################
188+
fix-go:
189+
$(call title, $@: $(GOOS))
190+
@cd $(MAKEFILE_DIR) \
191+
&& golangci-lint run --fix
192+
$(call footer, $@)
193+
194+
fix-go-all:
195+
$(call title, $@)
196+
@cd $(MAKEFILE_DIR) \
197+
&& GOOS=linux make fix-go \
198+
&& GOOS=freebsd make fix-go \
199+
&& GOOS=windows make fix-go
200+
$(call footer, $@)
201+
202+
fix-imports:
203+
$(call title, $@)
204+
@cd $(MAKEFILE_DIR) \
205+
&& goimports-reviser -company-prefixes $(ORG_PREFIXES) ./...
206+
$(call footer, $@)
207+
208+
fix-mod:
209+
$(call title, $@)
210+
@cd $(MAKEFILE_DIR) \
211+
&& go mod tidy
212+
$(call footer, $@)
213+
214+
##########################
215+
# Development tools installation
216+
##########################
217+
install-dev-tools:
218+
$(call title, $@)
219+
# golangci: v1.64.5
220+
# git-validation: main from 2023/11
221+
# ltag: v0.2.5
222+
# go-licenses: v2.0.0-alpha.1
223+
# goimports-reviser: v3.8.2
224+
@cd $(MAKEFILE_DIR) \
225+
&& go install github.com/golangci/golangci-lint/cmd/golangci-lint@0a603e49e5e9870f5f9f2035bcbe42cd9620a9d5 \
226+
&& go install github.com/vbatts/git-validation@679e5cad8c50f1605ab3d8a0a947aaf72fb24c07 \
227+
&& go install github.com/kunalkushwaha/ltag@b0cfa33e4cc9383095dc584d3990b62c95096de0 \
228+
&& go install github.com/google/go-licenses/v2@d01822334fba5896920a060f762ea7ecdbd086e8 \
229+
&& go install github.com/incu6us/goimports-reviser/v3@f034195cc8a7ffc7cc70d60aa3a25500874eaf04 \
230+
&& go install gotest.tools/gotestsum@ac6dad9c7d87b969004f7749d1942938526c9716
231+
@echo "Remember to add GOROOT/bin to your path"
232+
$(call footer, $@)
233+
234+
##########################
235+
# Testing tasks
236+
##########################
89237
test-unit:
90-
go test -v $(MAKEFILE_DIR)/pkg/...
91-
92-
binaries: nerdctl
93-
94-
install:
95-
install -D -m 755 $(CURDIR)/_output/nerdctl $(DESTDIR)$(BINDIR)/nerdctl
96-
install -D -m 755 $(MAKEFILE_DIR)/extras/rootless/containerd-rootless.sh $(DESTDIR)$(BINDIR)/containerd-rootless.sh
97-
install -D -m 755 $(MAKEFILE_DIR)/extras/rootless/containerd-rootless-setuptool.sh $(DESTDIR)$(BINDIR)/containerd-rootless-setuptool.sh
98-
install -D -m 644 -t $(DESTDIR)$(DOCDIR)/nerdctl $(MAKEFILE_DIR)/docs/*.md
99-
238+
$(call title, $@)
239+
@go test $(VERBOSE_FLAG) $(MAKEFILE_DIR)/pkg/...
240+
$(call footer, $@)
241+
242+
test-unit-bench:
243+
$(call title, $@)
244+
@go test $(VERBOSE_FLAG) $(MAKEFILE_DIR)/pkg/... -bench=.
245+
$(call footer, $@)
246+
247+
test-unit-race:
248+
$(call title, $@)
249+
@go test $(VERBOSE_FLAG) $(MAKEFILE_DIR)/pkg/... -race
250+
$(call footer, $@)
251+
252+
##########################
253+
# Release tasks
254+
##########################
100255
# Note that these options will not work on macOS - unless you use gnu-tar instead of tar
101256
TAR_OWNER0_FLAGS=--owner=0 --group=0
102257
TAR_FLATTEN_FLAGS=--transform 's/.*\///g'
@@ -107,6 +262,7 @@ define make_artifact_full_linux
107262
endef
108263

109264
artifacts: clean
265+
$(call title, $@)
110266
GOOS=linux GOARCH=amd64 make -C $(CURDIR) -f $(MAKEFILE_DIR)/Makefile binaries
111267
tar $(TAR_OWNER0_FLAGS) $(TAR_FLATTEN_FLAGS) -czvf $(CURDIR)/_output/nerdctl-$(VERSION_TRIMMED)-linux-amd64.tar.gz $(CURDIR)/_output/nerdctl $(MAKEFILE_DIR)/extras/rootless/*
112268

@@ -138,15 +294,19 @@ artifacts: clean
138294

139295
$(GO) -C $(MAKEFILE_DIR) mod vendor
140296
tar $(TAR_OWNER0_FLAGS) -czf $(CURDIR)/_output/nerdctl-$(VERSION_TRIMMED)-go-mod-vendor.tar.gz $(MAKEFILE_DIR)/go.mod $(MAKEFILE_DIR)/go.sum $(MAKEFILE_DIR)/vendor
297+
$(call footer, $@)
141298

142299
.PHONY: \
300+
all \
301+
lint \
302+
fix \
303+
test \
143304
help \
144-
nerdctl \
145-
clean \
146305
binaries \
147306
install \
307+
clean \
308+
lint-go lint-go-all lint-imports lint-yaml lint-shell lint-commits lint-mod lint-licenses lint-licenses-all \
309+
fix-go fix-go-all fix-imports fix-mod \
310+
install-dev-tools \
311+
test-unit test-unit-race test-unit-bench \
148312
artifacts
149-
lint \
150-
lint-yaml \
151-
lint-go \
152-
lint-shell

docs/testing/README.md

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,40 @@ and principles about writing tests.
55

66
For more comprehensive information about nerdctl test tools, see [tools.md](tools.md).
77

8-
## Lint
8+
## Code, fix, lint, rinse, repeat
99

1010
```
11-
go mod tidy
12-
golangci-lint run ./...
11+
# Do hack
12+
13+
# When done:
14+
# This will ensure proper import formatting, modules, and basic formatting of your code
15+
make fix
16+
17+
# And this will run all linters and report if any modification is required
18+
make lint
1319
```
1420

15-
This works on macOS as well - just pass along `GOOS=linux`.
21+
Note that both these tasks will work on any OS, including macOS.
22+
23+
Also note that `make lint` does call on subtask `make lint-commits` which is going to lint commits for the range
24+
`main..HEAD` by default (this is fine for most development flows that expect to be merged in main).
25+
26+
If you are targeting a specific branch that is not `main` (for example working against `release/1.7`),
27+
you likely want to explicitly set the commit range to that instead.
28+
29+
eg:
30+
`LINT_COMMIT_RANGE=target_branch..HEAD make lint-commits`
31+
or
32+
`LINT_COMMIT_RANGE=target_branch..HEAD make lint`
1633

1734
## Unit testing
1835

19-
Run `go test -v ./pkg/...`
36+
```
37+
# This will run basic unit testing
38+
make test
39+
```
40+
41+
This task must be run on a supported OS (linux, windows, or freebsd).
2042

2143
## Integration testing
2244

0 commit comments

Comments
 (0)