Skip to content

Commit 8f73d53

Browse files
committed
Makefile: Fixes and improvements
- Using make tasks to leverage caching for installing tools. - Fix ko tasks - Use more Makefile agnostic approach
1 parent 80833b1 commit 8f73d53

File tree

2 files changed

+104
-55
lines changed

2 files changed

+104
-55
lines changed

CONTRIBUTING.md

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Contributing
22

3-
### Thank you for you interest in apko! You are welcome here.
3+
> Thank you for you interest in apko! You are welcome here.
44
55
apko and [melange](https://github.com/chainguard-dev/melange) are open
66
source projects, always open for outside contributors.
@@ -19,7 +19,7 @@ bootstrap a development environment under any platform with docker installed.
1919
To run it, simply execute the following script from the top of the apko
2020
repository:
2121

22-
```bash
22+
```shell
2323
./hack/make-devenv.sh
2424
```
2525

@@ -31,39 +31,59 @@ mounted in the current working directory.
3131
You can use your editor to change the apko code and execute apko in the
3232
development shell to test it out:
3333

34-
```
35-
go run ./main.go
36-
37-
e5cf7d79f68b:/apko# go run ./main.go version
34+
```shell
35+
$ go run ./main.go
36+
Usage:
37+
apko [command]
38+
39+
Available Commands:
40+
build Build an image from a YAML configuration file
41+
build-minirootfs Build a minirootfs image from a YAML configuration file
42+
clean Clean the apko cache directory
43+
completion Generate the autocompletion script for the specified shell
44+
dot Output a digraph showing the resolved dependencies of an apko config
45+
help Help about any command
46+
install-keys Discover and install keys for all repositories
47+
login Log in to a registry
48+
publish Build and publish an image
49+
show-config Show the configuration derived from loading a YAML file
50+
show-packages Show the packages and versions that would be installed by a configuration
51+
version Prints the version
52+
53+
Flags:
54+
-h, --help help for apko
55+
--log-level string log level (e.g. debug, info, warn, error, fatal, panic) (default "INFO")
56+
-C, --workdir string working dir (default is current dir where executed) (default "/Users/marco/code/priv/apko")
57+
58+
Use "apko [command] --help" for more information about a command.
59+
$ go run ./main.go version
3860
_ ____ _ __ ___
3961
/ \ | _ \ | |/ / / _ \
4062
/ _ \ | |_) | | ' / | | | |
4163
/ ___ \ | __/ | . \ | |_| |
4264
/_/ \_\ |_| |_|\_\ \___/
4365
apko
4466
45-
GitVersion:
67+
GitVersion: devel
4668
GitCommit: unknown
4769
GitTreeState: unknown
4870
BuildDate: unknown
49-
GoVersion: go1.18
71+
GoVersion: go1.24.6
5072
Compiler: gc
5173
Platform: linux/amd64
52-
53-
5474
```
5575
5676
When done developing, simply exit the development shell. We would love to hear
5777
about your experience in the development shell and any ideas you may have!
5878
5979
## Linting and Tests
6080
61-
Before submitting a pull request, make sure tests and lints do not complain.
62-
Make sure you have go 1.18 and
63-
[golangci-lint](https://golangci-lint.run/welcome/install/) installed and try
81+
Before submitting a pull request, make sure tests and lints do not complain.
82+
Make sure you have go 1.24 or newer and
83+
[golangci-lint](https://golangci-lint.run/welcome/install/) installed (make task takes care of installing) and try
6484
running the linter and tests:
6585
66-
```
67-
go test ./...
68-
golangci-lint run
86+
```shell
87+
make fmt lint
88+
make test
6989
```

Makefile

Lines changed: 68 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ LDFLAGS=-buildid= -X $(PKG).gitVersion=$(GIT_VERSION) \
3939

4040
DIGEST ?=
4141

42-
define create_kocache_path
43-
mkdir -p $(KOCACHE_PATH)
44-
endef
42+
PROJECT_BIN := $(shell pwd)/bin
4543

4644
##########
4745
# default
@@ -53,35 +51,54 @@ default: help
5351
# ko build
5452
##########
5553

54+
DOCKER_HOST ?= $(shell docker context inspect --format '{{.Endpoints.docker.Host}}')
55+
56+
KO_BIN := $(PROJECT_BIN)/ko
57+
KO_VERSION := v0.18.0
58+
KO_DOCKER_REPO ?= chainguard.dev/apko
59+
KOCACHE := $(PROJECT_BIN)/kocache
60+
KO_TAGS := --tags $(IMAGE_TAG) --tags $(GIT_VERSION) --tags $(GIT_HASH)
61+
62+
$(KO_BIN):
63+
@echo "Installing ko@$(KO_VERSION) to $(PROJECT_BIN)"
64+
@GOBIN=$(PROJECT_BIN) go install github.com/google/ko@$(KO_VERSION)
65+
66+
$(KOCACHE):
67+
@mkdir -p $@
68+
5669
.PHONY: ko
57-
ko: ## Build images using ko
58-
$(create_kocache_path)
59-
$(eval DIGEST := $(shell LDFLAGS="$(LDFLAGS)" GIT_HASH=$(GIT_HASH) GIT_VERSION=$(GIT_VERSION) \
60-
KOCACHE=$(KOCACHE_PATH) ko build --bare \
61-
--platform=all --tags $(IMAGE_TAG) --tags $(GIT_VERSION) --tags $(GIT_HASH) \
62-
chainguard.dev/apko))
70+
ko: $(KO_BIN) $(KOCACHE) ## Build images using ko
71+
@$(MAKE) --no-print-directory log-$@
72+
@$(eval DIGEST := $(shell LDFLAGS="$(LDFLAGS)" GIT_HASH=$(GIT_HASH) GIT_VERSION=$(GIT_VERSION) \
73+
KO_DOCKER_REPO=$(KO_DOCKER_REPO) \
74+
KOCACHE=$(KOCACHE) \
75+
$< build --bare --platform=all $(KO_TAGS)))
6376
@echo Image Digest $(DIGEST)
6477

6578
.PHONY: ko-local
66-
ko-local: ## Build images locally using ko
67-
$(create_kocache_path)
79+
ko-local: $(KO_BIN) $(KOCACHE) ## Build images locally using ko
80+
@$(MAKE) --no-print-directory log-$@
81+
@DOCKER_HOST=$(DOCKER_HOST) \
82+
KO_DOCKER_REPO=$(KO_DOCKER_REPO) \
6883
LDFLAGS="$(LDFLAGS)" GIT_HASH=$(GIT_HASH) GIT_VERSION=$(GIT_VERSION) \
69-
KOCACHE=$(KOCACHE_PATH) ko build --bare \
70-
--tags $(IMAGE_TAG) --tags $(GIT_VERSION) --tags $(GIT_HASH) --local \
71-
chainguard.dev/apko
84+
$< build --bare --local $(KO_TAGS)
7285

7386
.PHONY: ko-apply
74-
ko-apply: ## Build the image and apply the manifests
75-
$(create_kocache_path)
87+
ko-apply: $(KO_BIN) $(KOCACHE) ## Build the image and apply the manifests
88+
@$(MAKE) --no-print-directory log-$@
89+
@KO_DOCKER_REPO=$(KO_DOCKER_REPO) \
90+
KOCACHE=$(KOCACHE) \
7691
LDFLAGS="$(LDFLAGS)" \
77-
KOCACHE=$(KOCACHE_PATH) ko apply --base-import-paths \
92+
$< apply --base-import-paths \
7893
--recursive --filename config/
7994

80-
.PHONY: ko-apply
81-
ko-resolve: ## Build the image generate the Task YAML
82-
$(create_kocache_path)
95+
.PHONY: ko-resolve
96+
ko-resolve: $(KO_BIN) $(KOCACHE) ## Build the image generate the Task YAML
97+
@$(MAKE) --no-print-directory log-$@
98+
@KO_DOCKER_REPO=$(KO_DOCKER_REPO) \
99+
KOCACHE=$(KOCACHE) \
83100
LDFLAGS="$(LDFLAGS)" \
84-
KOCACHE=$(KOCACHE_PATH) ko resolve --base-import-paths \
101+
$< resolve --base-import-paths \
85102
--recursive --filename config/ > task.yaml
86103

87104
##########
@@ -108,26 +125,28 @@ install: $(SRCS) ## Builds and moves apko into BINDIR (default /usr/bin)
108125
# lint / test section
109126
#####################
110127

111-
GOLANGCI_LINT_DIR = $(shell pwd)/bin
112-
GOLANGCI_LINT_BIN = $(GOLANGCI_LINT_DIR)/golangci-lint
128+
GOLANGCI_LINT_BIN := $(PROJECT_BIN)/golangci-lint
129+
GOLANGCI_LINT_VERSION := v2.6.1
113130

114-
.PHONY: golangci-lint
115-
golangci-lint:
116-
rm -f $(GOLANGCI_LINT_BIN) || :
117-
set -e ;\
118-
GOBIN=$(GOLANGCI_LINT_DIR) go install github.com/golangci/golangci-lint/cmd/golangci-lint/v2@v2.2.1 ;\
131+
$(GOLANGCI_LINT_BIN):
132+
@echo "Installing golangci-lint@$(GOLANGCI_LINT_VERSION) to $(PROJECT_BIN)"
133+
@GOBIN=$(PROJECT_BIN) go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION)
134+
135+
$(GOBIN)/goimports:
136+
@echo "Installing goimports to $(GOBIN)"
137+
@go install golang.org/x/tools/cmd/goimports@latest
119138

120139
.PHONY: fmt
121-
fmt: ## Format all go files
122-
@ $(MAKE) --no-print-directory log-$@
123-
goimports -w $(GOFILES)
140+
fmt: $(GOBIN)/goimports ## Format all go files
141+
@$(MAKE) --no-print-directory log-$@
142+
@$< -l $(GOFILES)
124143

125144
.PHONY: checkfmt
126145
checkfmt: SHELL := /usr/bin/env bash
127-
checkfmt: ## Check formatting of all go files
128-
@ $(MAKE) --no-print-directory log-$@
129-
$(shell test -z "$(shell gofmt -l $(GOFILES) | tee /dev/stderr)")
130-
$(shell test -z "$(shell goimports -l $(GOFILES) | tee /dev/stderr)")
146+
checkfmt: $(GOBIN)/goimports ## Check formatting of all go files
147+
@$(MAKE) --no-print-directory log-$@
148+
@test -z "$$(gofmt -l $(GOFILES))" || { echo "Files need formatting"; exit 1; }
149+
@test -z "$$($< -l $(GOFILES))" || { echo "Linting issues found"; exit 1; }
131150

132151
log-%:
133152
@grep -h -E '^$*:.*?## .*$$' $(MAKEFILE_LIST) | \
@@ -140,8 +159,9 @@ log-%:
140159
}'
141160

142161
.PHONY: lint
143-
lint: checkfmt golangci-lint ## Run linters and checks like golangci-lint
144-
$(GOLANGCI_LINT_BIN) run -n
162+
lint: checkfmt $(GOLANGCI_LINT_BIN) ## Run linters and checks like golangci-lint
163+
@$(MAKE) --no-print-directory log-$@
164+
@$(GOLANGCI_LINT_BIN) run -n
145165

146166
.PHONY: test
147167
test: ## Run go test
@@ -177,9 +197,18 @@ ci:
177197
#######################
178198
# Sign images
179199
#######################
200+
201+
COSIGN_VERSION := v3.0.2
202+
203+
$(PROJECT_BIN)/cosign:
204+
@echo "Installing cosign to $(PROJECT_BIN)"
205+
@GOBIN=$(PROJECT_BIN) go install github.com/sigstore/cosign/v3/cmd/cosign@$(COSIGN_VERSION)
206+
180207
.PHONY: sign-image
181-
sign-image: ko ## Sign images built using ko
182-
cosign sign -y $(DIGEST)
208+
sign-image: $(PROJECT_BIN)/cosign ko ## Sign images built using ko
209+
@$(MAKE) --no-print-directory log-$@
210+
@echo "Signing $(DIGEST)"
211+
@$< sign -y $(DIGEST)
183212

184213
##################
185214
# help

0 commit comments

Comments
 (0)