-
Notifications
You must be signed in to change notification settings - Fork 1
/
Makefile
151 lines (119 loc) · 5.16 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
ifeq ($(origin DRONE_TAG), environment)
VERSION := $(DRONE_TAG)
else
VERSION ?= $(shell git describe --tags --exact-match HEAD 2> /dev/null || git rev-parse --short HEAD)
endif
BUILD_DIRECTORY := build
RELEASE_DIRECTORY := $(BUILD_DIRECTORY)/release/$(VERSION)
GOOS := $(shell go env GOOS)
GOARCH := $(shell go env GOARCH)
TARGET_BUILD ?= $(GOOS)/$(GOARCH)
TARGET_BUILDS ?= $(TARGET_BUILD)
BINARY_NAME := drone-secrets-sync
BINARY_OUTPUT_LOCATION ?= $(RELEASE_DIRECTORY)/$(BINARY_NAME)_$(subst /,-,$(TARGET_BUILD))
BINARY_OUTPUT_BIN_COPY_LOCATION := bin/$(BINARY_NAME)
ENTRYPOINT := $(wildcard cmd/drone-secrets-sync/*.go)
GO_FILES := $(shell find . -type f -name '*.go' ! -name '*_test.go' ! -path '*/build/*')
MARKDOWN_FILES := $(shell find . -type f -name '*.md' ! -path '*/site-packages/*' ! -path '*build/*' ! -path './test/bats/*')
JSONNET_FILES := $(shell find . -type f -name '*.jsonnet' ! -path '*/build/*')
INSTALL_PATH = /usr/local/bin/$(BINARY_NAME)
KANIKO_EXECUTOR ?= docker run --rm -v ${PWD}:${PWD} -w ${PWD} gcr.io/kaniko-project/executor:latest
DOCKER_IMAGE_NAME := colin-nolan/$(BINARY_NAME):$(VERSION)
TARGET_PLATFORM ?= $(if $(filter darwin,$(GOOS)),linux/$(GOARCH),$(GOOS)/$(GOARCH))
TARGET_PLATFORMS ?= $(TARGET_PLATFORM)
IMAGE_OUTPUT_LOCATION ?= $(RELEASE_DIRECTORY)/$(BINARY_NAME)-image_$(subst /,-,$(TARGET_PLATFORM)).tar
MULTIARCH_OUTPUT_LOCATION := $(RELEASE_DIRECTORY)/multiarch
SHELL := /bin/bash
all: build
build:
iterations=0; \
for target_build in $(TARGET_BUILDS); do \
iterations=$$(($${iterations} + 1)); \
IFS="/" read -r os arch <<< "$${target_build}"; \
target="$(RELEASE_DIRECTORY)/$(BINARY_NAME)_$${os}-$${arch}"; \
make "$${target}" GOOS="$${os}" GOARCH="$${arch}" BINARY_OUTPUT_LOCATION="$${target}"; \
done; \
if [ "$${iterations}" -eq 1 ]; then \
mkdir -p "$(dir $(BINARY_OUTPUT_BIN_COPY_LOCATION))"; \
cp "$${target}" "$(BINARY_OUTPUT_BIN_COPY_LOCATION)"; \
fi
$(BINARY_OUTPUT_LOCATION): $(GO_FILES)
GOOS=$(GOOS) GOARCH=$(GOARCH) go build \
-ldflags "-s -w -X main.version=$(VERSION)" \
-o "$(BINARY_OUTPUT_LOCATION)" $(ENTRYPOINT)
build-image:
for target_platform in $(TARGET_PLATFORMS); do \
target="$(RELEASE_DIRECTORY)/$(BINARY_NAME)-image_$${target_platform//\//-}.tar"; \
make "$${target}" TARGET_PLATFORM="$${target_platform}" IMAGE_OUTPUT_LOCATION="$${target}"; \
done
$(IMAGE_OUTPUT_LOCATION): $(GO_FILES) Dockerfile .dockerignore
mkdir -p $$(dirname $(IMAGE_OUTPUT_LOCATION))
@# Must work both containerised and not
$(KANIKO_EXECUTOR) \
--custom-platform=$(TARGET_PLATFORM) \
--no-push \
--dockerfile Dockerfile \
--build-arg VERSION=$(VERSION) \
--tar-path $(IMAGE_OUTPUT_LOCATION) \
--destination $(DOCKER_IMAGE_NAME) \
--context $${PWD} \
>&2
build-image-and-load: build-image
docker load -i $(IMAGE_OUTPUT_LOCATION)
# XXX: this rule does not align with `build-image`, which defines how to build only one image. There is no
# multi-image build rule, which will lead to `make` complaining of a target issue if one of the images
# does not exist. To get around this, all `build` and `build-image` need to be changed to have multi-os/arch support.
build-image-multiarch: $(MULTIARCH_OUTPUT_LOCATION)
$(MULTIARCH_OUTPUT_LOCATION): build-image
scripts/create-multiarch-image.sh \
$(MULTIARCH_OUTPUT_LOCATION) \
$(foreach target_platform,$(TARGET_PLATFORMS),$(RELEASE_DIRECTORY)/$(BINARY_NAME)-image_$(subst /,-,$(target_platform)).tar)
install: build
cp $(BINARY_OUTPUT_LOCATION) $(INSTALL_PATH)
uninstall:
rm -f $(INSTALL_PATH)
clean:
go clean
rm -rf $(RELEASE_DIRECTORY) bin
rm -f coverage.out output.log
lint: lint-code lint-markdown lint-jsonnet
lint-code:
golangci-lint run --timeout 15m0s
lint-markdown:
mdformat --check $(MARKDOWN_FILES)
lint-jsonnet:
for file in $(JSONNET_FILES); do \
jsonnetfmt --test $${file}; \
done
format: format-code format-markdown format-jsonnet
fmt: format
format-code: $(GO_FILES)
go fmt ./...
format-markdown:
mdformat $(MARKDOWN_FILES)
format-jsonnet:
for file in $(JSONNET_FILES); do \
jsonnetfmt -i $${file}; \
done
test:
rm -rf .coverage
make test-unit
make test-system
test-unit:
rm -rf .coverage/unit
mkdir -p .coverage/unit
CGO_ENABLED=1 go test -v -cover -race ./... -args -test.gocoverdir="$${PWD}/.coverage/unit"
test-system:
CGO_ENABLED=1 go build -cover -race -o build/drone-secrets-sync-coveraged $(ENTRYPOINT)
rm -rf .coverage/system
mkdir -p .coverage/system
GOCOVERDIR=.coverage/system SUT=build/drone-secrets-sync-coveraged test/bats/bin/bats -j 4 test/system/tests.bats
test-coverage-report:
@# TODO: The system test paths are absolute file paths opposed to package paths. It's not clear
@# how to correct these. However, codecov.io merges them correctly so not spending any longer
@# now trying to fix this so it works locally
go tool covdata textfmt -i=.coverage/unit,.coverage/system -o .coverage/coverage.out
go tool cover -html .coverage/coverage.out -o .coverage/coverage.html
version:
@echo $(VERSION)
.PHONY: all build build-image build-image-multiarch install uninstall clean lint lint-code lint-markdown lint-jsonnet format fmt format-code format-markdown format-jsonnet test test-unit test-system test-coverage-report