This repository has been archived by the owner on May 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
/
Makefile
176 lines (139 loc) · 5.58 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
SHELL ?= /bin/bash
.DEFAULT_GOAL := build
################################################################################
# Version details #
################################################################################
# This will reliably return the short SHA1 of HEAD or, if the working directory
# is dirty, will return that + "-dirty"
GIT_VERSION = $(shell git describe --always --abbrev=7 --dirty --match=NeVeRmAtCh)
################################################################################
# Go build details #
################################################################################
BASE_PACKAGE_NAME := github.com/cnabio/duffle
################################################################################
# Containerized development environment-- or lack thereof #
################################################################################
ifneq ($(SKIP_DOCKER),true)
PROJECT_ROOT := $(dir $(realpath $(firstword $(MAKEFILE_LIST))))
DEV_IMAGE := golang:1.13
DOCKER_CMD_PREFIX := docker run \
--rm \
-e SKIP_DOCKER=true \
-v $(PROJECT_ROOT):/go/src/$(BASE_PACKAGE_NAME) \
-w /go/src/$(BASE_PACKAGE_NAME)
DOCKER_INTERACTIVE ?= true
ifeq ($(DOCKER_INTERACTIVE),true)
DOCKER_CMD_PREFIX := $(DOCKER_CMD_PREFIX) -it
endif
DOCKER_CMD := $(DOCKER_CMD_PREFIX) $(DEV_IMAGE)
endif
################################################################################
# Binaries and Docker images we build and publish #
################################################################################
ifdef DOCKER_REGISTRY
DOCKER_REGISTRY := $(DOCKER_REGISTRY)/
endif
ifdef DOCKER_ORG
DOCKER_ORG := $(DOCKER_ORG)/
endif
BASE_IMAGE_NAME := duffle
ifdef VERSION
MUTABLE_DOCKER_TAG := latest
else
VERSION := $(GIT_VERSION)
MUTABLE_DOCKER_TAG := edge
endif
LDFLAGS := -w -s -X $(BASE_PACKAGE_NAME)/pkg/version.Version=$(VERSION)
IMAGE_NAME := $(DOCKER_REGISTRY)$(DOCKER_ORG)$(BASE_IMAGE_NAME):$(VERSION)
MUTABLE_IMAGE_NAME := $(DOCKER_REGISTRY)$(DOCKER_ORG)$(BASE_IMAGE_NAME):$(MUTABLE_DOCKER_TAG)
################################################################################
# Utility targets #
################################################################################
.PHONY: goimports
goimports:
$(DOCKER_CMD) sh -c "find . -name \"*.go\" | fgrep -v vendor/ | xargs goimports -w -local github.com/cnabio/duffle"
.PHONY: build-drivers
build-drivers:
mkdir -p bin
cp drivers/azure-vm/duffle-azvm.sh bin/duffle-azvm
cd drivers/azure-vm && pip3 install -r requirements.txt
################################################################################
# Tests #
################################################################################
.PHONY: lint
lint:
$(DOCKER_CMD) golangci-lint run --config ./golangci.yml
.PHONY: test
test:
$(DOCKER_CMD) go test ./...
################################################################################
# Build / Publish #
################################################################################
.PHONY: build
build: build-all-bins build-image
.PHONY: build-all-bins
build-all-bins:
$(DOCKER_CMD) bash -c "LDFLAGS=\"$(LDFLAGS)\" scripts/build.sh"
# You can make this target build for a specific OS and architecture using GOOS
# and GOARCH environment variables.
.PHONY: build-bin
build-bin:
$(DOCKER_CMD) bash -c "GOOS=\"$(GOOS)\" GOARCH=\"$(GOARCH)\" LDFLAGS=\"$(LDFLAGS)\" scripts/build.sh"
# This target is for contributor convenience.
.PHONY: build-%
build-%:
$(DOCKER_CMD) bash -c "GOOS=$* LDFLAGS=\"$(LDFLAGS)\" scripts/build.sh"
.PHONY: build-image
build-image:
docker build \
-t $(IMAGE_NAME) \
--build-arg LDFLAGS='$(LDFLAGS)' \
.
docker tag $(IMAGE_NAME) $(MUTABLE_IMAGE_NAME)
.PHONY: push
push: push-image
.PHONY: push-image
push-image:
docker push $(IMAGE_NAME)
docker push $(MUTABLE_IMAGE_NAME)
################################################################################
# Example Bundles Build / Validation #
################################################################################
# Unfortunately, the ajv cli does not yet support fetching remote references
# So, we fetch schema that are ref'd by the bundle.schema.json and supply to ajv
BUNDLE_SCHEMA := bundle.schema.json
DEFINITIONS_SCHEMA := definitions.schema.json
REMOTE_SCHEMA := $(BUNDLE_SCHEMA) $(DEFINITIONS_SCHEMA)
# bundle-all runs the provided make target on all bundles with a 'duffle.json' file in their directory
define bundle-all
@for dir in $$(ls -1 examples); do \
if [[ -e "examples/$$dir/duffle.json" ]]; then \
BUNDLE=$$dir make --no-print-directory $(1) || exit $$? ; \
fi ; \
done
endef
.PHONY: get-ajv
get-ajv:
@if ! $$(which ajv > /dev/null 2>&1); then \
npm install -g [email protected]; \
fi
.PHONY: get-schema
get-schema:
@for schema in $(REMOTE_SCHEMA); do \
if ! [[ -f /tmp/$$schema ]]; then \
curl -sLo /tmp/$$schema https://cnab.io/v1/$$schema ; \
fi ; \
done
.PHONY: validate
validate: get-ajv get-schema
ifndef BUNDLE
$(call bundle-all,validate)
else
@echo "building and validating $(BUNDLE)"
@cd examples/$(BUNDLE) && \
duffle build -o bundle.json > /dev/null && \
ajv test \
-s /tmp/$(BUNDLE_SCHEMA) \
-r /tmp/$(DEFINITIONS_SCHEMA) \
-d bundle.json --valid
endif