-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
170 lines (128 loc) · 4.59 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
SHELL := /bin/bash
.ONESHELL:
.SHELLFLAGS := -eu -o pipefail -O extglob -c
.DELETE_ON_ERROR:
MAKEFLAGS += --warn-undefined-variables
MAKEFLAGS += --no-builtin-rules
.DEFAULT_GOAL := help
VAULT := homelab
VERSION := $(shell cat ./VERSION)
COMMIT_HASH := $(shell git log -1 --pretty=format:"sha-%h")
PLATFORMS := "linux/arm/v7,linux/arm64/v8,linux/amd64"
BUILD_FLAGS ?=
ifndef HOMELAB_OP_SERVICE_ACCOUNT_TOKEN
$(error HOMELAB_OP_SERVICE_ACCOUNT_TOKEN is not set in your environment)
endif
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: dotenv
dotenv: .env ## Setup build secrets in .env files
.env: .env.template
OP_SERVICE_ACCOUNT_TOKEN=${HOMELAB_OP_SERVICE_ACCOUNT_TOKEN} VAULT=$(VAULT) op inject --force --in-file $< --out-file $@
# 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 := spelunker
SPELUNKER_BUILDER := $(SPELUNKER)-builder
SPELUNKER_REPO := ${GITHUB_REGISTRY}/woeplanet
SPELUNKER_IMAGE := ${SPELUNKER}
SPELUNKER_DOCKERFILE := ./docker/${SPELUNKER}/Dockerfile
HADOLINT_IMAGE := hadolint/hadolint
.PHONY: lint
lint: lint-pylint lint-flake8 lint-docker ## Run all linters on the code base
.PHONY: lint-pylint
lint-pylint: ## Run pylint on the code base
pylint --verbose -j 4 --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}-dockerfile
.PHONY: lint-${SPELUNKER}-dockerfile
lint-${SPELUNKER}-dockerfile:
$(MAKE) _lint-dockerfile -e BUILD_DOCKERFILE="${SPELUNKER_DOCKERFILE}"
BUILD_TARGETS := build-assets build-spelunker
.PHONY: build
build: $(BUILD_TARGETS) ## Build all images
REBUILD_TARGETS := rebuild-spelunker
.PHONY: rebuild
rebuild: $(REBUILD_TARGETS) ## Rebuild all images (no cache)
RELEASE_TARGETS := release-spelunker
.PHONY: release
release: $(RELEASE_TARGETS) ## Tag and push all images
# spelunker targets
.PHONY: build-assets
build-assets:
yarn install
grunt build
build-spelunker: build-assets ## Build the spelunker image
$(MAKE) _build-image \
-e BUILD_DOCKERFILE=./docker/$(SPELUNKER)/Dockerfile \
-e BUILD_IMAGE=$(SPELUNKER_IMAGE)
rebuild-spelunker: build-assets ## Rebuild the spelunker image (no cache)
$(MAKE) _build-image \
-e BUILD_DOCKERFILE=./docker/$(SPELUNKER)/Dockerfile \
-e BUILD_IMAGE=$(SPELUNKER_IMAGE) \
-e BUILD_FLAGS="--no-cache"
release-spelunker: build-spelunker ## Tag and push the spelunker image
$(MAKE) _tag-image \
-e BUILD_IMAGE=$(SPELUNKER_IMAGE) \
-e BUILD_TAG=$(COMMIT_HASH)
$(MAKE) _tag-image \
-e BUILD_IMAGE=$(SPELUNKER_IMAGE) \
-e BUILD_TAG=$(VERSION)
.PHONY: up
up: repo_login ## Bring the container stack up
docker compose up -d
.PHONY: down
down: ## Bring the container stack down
docker compose down
.PHONY: pull
pull: ## Pull all current Docker images
docker compose pull
.PHONY: restart
restart: down up ## Restart the container stack
.PHONY: _lint-dockerfile
_lint-dockerfile:
docker run --rm -i -e HADOLINT_IGNORE=DL3008,DL3018,DL3003 ${HADOLINT_IMAGE} < ${BUILD_DOCKERFILE}
.PHONY: init-builder
init-builder:
docker buildx inspect $(SPELUNKER_BUILDER) > /dev/null 2>&1 || \
docker buildx create --name $(SPELUNKER_BUILDER) --bootstrap --use
.PHONY: _build-image
_build-image: repo-login
docker buildx build --platform=$(PLATFORMS) \
--file ${BUILD_DOCKERFILE} \
--push \
--tag ${SPELUNKER_REPO}/${BUILD_IMAGE}:latest \
--provenance=false \
--build-arg VERSION=${VERSION} \
--build-arg UBUNTU_VERSION=${UBUNTU_VERSION} \
--ssh default \
$(BUILD_FLAGS) .
.PHONY: _tag-image
_tag-image: repo-login
docker buildx imagetools create ${SPELUNKER_REPO}/$(BUILD_IMAGE):latest \
--tag ${SPELUNKER_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; run make dotenv)
endif