-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
163 lines (126 loc) · 4.76 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
SHELL := /bin/bash
.ONESHELL:
.SHELLFLAGS := -eu -o pipefail -O extglob -c
.DELETE_ON_ERROR:
MAKEFLAGS += --warn-undefined-variables
MAKEFLAGS += --no-builtin-rules
ifeq ($(.DEFAULT_GOAL),)
ifneq ($(shell test -f .env; echo $$?), 0)
$(error Cannot find a .env file; copy .env.sample and customise)
endif
endif
# Wrap the build in a check for an existing .env file
ifeq ($(shell test -f .env; echo $$?), 0)
include .env
ENVVARS := $(shell sed -ne 's/ *\#.*$$//; /./ s/=.*$$// p' .env )
$(foreach var,$(ENVVARS),$(eval $(shell echo export $(var)="$($(var))")))
.DEFAULT_GOAL := help
VERSION := $(shell cat ./VERSION)
COMMIT_HASH := $(shell git log -1 --pretty=format:"sha-%h")
BUILD_FLAGS ?=
SPELUNKER_SERVICE := spelunker
SPELUNKER_SERVICE_REPO := ${GITHUB_REGISTRY}/woeplanet
SPELUNKER_SERVICE_IMAGE := ${SPELUNKER_SERVICE}
SPELUNKER_SERVICE_DOCKERFILE := ./docker/${SPELUNKER_SERVICE}/Dockerfile
HADOLINT_IMAGE := hadolint/hadolint
.PHONY: help
help: ## Show this help message
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z0-9_-]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' Makefile
.PHONY: lint
lint: lint-pylint lint-flake8 ## Run all linters on the code base
.PHONY: lint-pylint
lint-pylint: ## Run pylint on the code base
pylint --verbose -j 4 --reports yes --recursive yes spelunker
.PHONY: lint-flake8
lint-flake8: ## Run flake8 on the code base
flake8 -j 4 spelunker
.PHONY: lint-docker
lint-docker: lint-compose lint-dockerfiles ## Lint all Docker related files
.PHONY: lint-compose
lint-compose: ## Lint docker-compose.yml
docker compose -f docker-compose.yml config 1> /dev/null
.PHONY: lint-dockerfiles
.PHONY: _lint-dockerfiles ## Lint all Dockerfiles
lint-dockerfiles: lint-${SPELUNKER_SERVICE}-dockerfile
.PHONY: lint-${SPELUNKER_SERVICE}-dockerfile
lint-${SPELUNKER_SERVICE}-dockerfile:
$(MAKE) _lint_dockerfile -e BUILD_DOCKERFILE="${SPELUNKER_SERVICE_DOCKERFILE}"
BUILD_TARGETS := build_spelunker_service
.PHONY: build
build: $(BUILD_TARGETS) ## Build all images
REBUILD_TARGETS := rebuild_spelunker_service
.PHONY: rebuild
rebuild: $(REBUILD_TARGETS) ## Rebuild all images (no cache)
RELEASE_TARGETS := release_spelunker_service
.PHONY: release
release: $(RELEASE_TARGETS) ## Tag and push all images
# spelunker-service targets
build_spelunker_service: ## Build the spelunker_service image
$(MAKE) _build_image \
-e BUILD_DOCKERFILE=./docker/$(SPELUNKER_SERVICE)/Dockerfile \
-e BUILD_IMAGE=$(SPELUNKER_SERVICE_IMAGE)
$(MAKE) _tag_image \
-e BUILD_IMAGE=$(SPELUNKER_SERVICE_IMAGE) \
-e BUILD_TAG=latest
rebuild_spelunker_service: ## Rebuild the spelunker_service image (no cache)
$(MAKE) _build_image \
-e BUILD_DOCKERFILE=./docker/$(SPELUNKER_SERVICE)/Dockerfile \
-e BUILD_IMAGE=$(SPELUNKER_SERVICE_IMAGE) \
-e BUILD_FLAGS="--no-cache"
$(MAKE) _tag_image \
-e BUILD_IMAGE=$(SPELUNKER_SERVICE_IMAGE) \
-e BUILD_TAG=latest
release_spelunker_service: build_spelunker_service repo_login ## Tag and push spelunker_service image
$(MAKE) _release_image \
-e BUILD_IMAGE=$(SPELUNKER_SERVICE_IMAGE)
.PHONY: up
up: repo_login ## Bring the Spelunker container stack up
docker compose up -d
.PHONY: down
down: _env_check repo_login ## Bring the Spelunker container stack down
docker compose down
.PHONY: restart
restart: down up ## Restart the Spelunker container stack
.PHONY: destroy
destroy: ## Bring the Spelunker container stack down (removing volumes)
docker compose down -v
.PHONY: _lint_dockerfile
_lint_dockerfile:
docker run --rm -i -e HADOLINT_IGNORE=DL3008 ${HADOLINT_IMAGE} < ${BUILD_DOCKERFILE}
.PHONY: _build_image
_build_image:
DOCKER_BUILDKIT=1 docker build --file ${BUILD_DOCKERFILE} --tag ${BUILD_IMAGE} --ssh default $(BUILD_FLAGS) .
.PHONY: _release_image
_release_image:
$(MAKE) _tag_image \
-e BUILD_IMAGE=$(BUILD_IMAGE) \
-e BUILD_TAG=$(VERSION)
$(MAKE) _tag_image \
-e BUILD_IMAGE=$(BUILD_IMAGE) \
-e BUILD_TAG=$(COMMIT_HASH)
$(MAKE) _registry_tag_image \
-e BUILD_IMAGE=$(BUILD_IMAGE) \
-e BUILD_TAG=latest
$(MAKE) _registry_tag_image \
-e BUILD_IMAGE=$(BUILD_IMAGE) \
-e BUILD_TAG=$(VERSION)
$(MAKE) _registry_tag_image \
-e BUILD_IMAGE=$(BUILD_IMAGE) \
-e BUILD_TAG=$(COMMIT_HASH)
docker push ${SPELUNKER_SERVICE_REPO}/$(BUILD_IMAGE):latest
docker push ${SPELUNKER_SERVICE_REPO}/$(BUILD_IMAGE):$(VERSION)
docker push ${SPELUNKER_SERVICE_REPO}/$(BUILD_IMAGE):$(COMMIT_HASH)
.PHONY: _tag_image
_tag_image:
docker tag ${BUILD_IMAGE} ${BUILD_IMAGE}:${BUILD_TAG}
.PHONY: _registry_tag_image
_registry_tag_image:
docker tag ${BUILD_IMAGE} ${SPELUNKER_SERVICE_REPO}/${BUILD_IMAGE}:${BUILD_TAG}
.PHONY: repo_login
repo_login:
echo "${GITHUB_PAT}" | docker login ${GITHUB_REGISTRY} -u ${GITHUB_USER} --password-stdin
# No .env file; fail the build
else
.DEFAULT:
$(error Cannot find a .env file; copy .env.sample and customise)
endif