-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathMakefile
211 lines (171 loc) · 6.91 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# Makefile for BlackLab Docker images
#
# This provides some convenient shorthands for common Docker (Compose) commands
# related to building and tagging images, running and deploying the application.
#
# Build images:
# make build # will be tagged as "latest"
# make build VERSION=4-alpha # will be tagged as "4-alpha"
# make build-plain # Build images with plain output (for CI)
#
# Tag images:
# make tag VERSION=4-alpha # Tag latest images as "4-alpha"
# make tag-commit # Tag latest images with commit hash
#
# Tag and push to Docker Hub:
# make push # Push version "latest"
# make push VERSION=4-alpha # Tag as "4-alpha" and push
# make push-commit # Tag with commit hash and push
#
# Build, tag and push:
# make release VERSION=4-alpha # Build, tag and push images
# make release-commit # Build, tag with commit hash and push
#
# Deploy to server (i.e. pull and (re)deploy application):
# (specified Docker context must exist)
# make deploy VERSION=4.0-alpha CONTEXT=my-server
# make deploy-commit CONTEXT=my-server # Deploy current commit image (must exist)
#
# Running application:
# make dev # Run development server (with docker-compose.override.yml)
# make up # Start application
# make stop # Stop application
# make down # Stop application and remove containers
# make restart # Restart application
# make logs # Show logs
# make logs-f # Show and follow logs (Ctrl+C to exit)
#
# Image management:
# make lsi # List local blacklab images
# make rmi VERSION=4-alpha # Remove "4-alpha" images
# Config variables
#----------------------------------------
# Our project name. This is important if our project directory has a different name,
# such as when calling from Jenkins. In that case, the --project-name option will automatically
# be added.
PROJECT_NAME = blacklab
# Name prefix for the Docker image(s)
IMAGE_NAME_PREFIX = instituutnederlandsetaal/blacklab
# Helper variables
#-------------------------------------------
# Helper to be able to have leading whitespace in variable...
EMPTY_STRING =
CURRENT_DIR = $(shell basename $(PWD))
# See if our directory name matches our project name.
# If not, pass --project-name to Compose so it always behaves the same way.
# (if we didn't do this, container names would vary based on the directory name)
opt-project-name:
ifneq ($(PROJECT_NAME),$(CURRENT_DIR))
OPT_PROJECT_NAME = $(EMPTY_STRING) --project-name $(PROJECT_NAME)
endif
-include opt-project-name
# Pass CONTEXT=<name> to use that Docker context
ifdef CONTEXT
CONTEXT_PARAM = $(EMPTY_STRING) --context $(CONTEXT)
endif
# Our standard commands for calling Docker and Compose
DOCKER_COMMAND = docker$(CONTEXT_PARAM)
COMPOSE_NO_FILE = $(DOCKER_COMMAND) compose$(OPT_PROJECT_NAME)
COMPOSE_COMMAND = $(COMPOSE_NO_FILE) -f docker-compose.yml
COMPOSE_COMMAND_DEV = $(COMPOSE_NO_FILE)
# Make sure VERSION defaults to 'latest'
ifndef VERSION
VERSION = latest
endif
# Short hash of latest Git commit
GIT_COMMIT_HASH = $$(git log -1 --pretty=%h)
# For build, push, etc.: apply to all profiles, not just default
ALL_PROFILES = $(EMPTY_STRING) --profile default --profile tools
# By default, don't add any options to docker compose build
ifndef BUILD_OPT
BUILD_OPT =
endif
# Application management targets
#-------------------------------------------
show-info:
echo Git commit hash: $(GIT_COMMIT_HASH)
echo current dir name: $(CURRENT_DIR)
echo OPT_PROJECT_NAME:$(OPT_PROJECT_NAME)
# Stop application and remove containers
down:
$(COMPOSE_COMMAND) down
# Build images
build:
$(COMPOSE_COMMAND)$(ALL_PROFILES) build$(BUILD_OPT)
# Make sure images are always tagged as latest, even if we override VERSION
$(DOCKER_COMMAND) tag $(IMAGE_NAME_PREFIX):$(VERSION) $(IMAGE_NAME_PREFIX):latest
$(DOCKER_COMMAND) tag $(IMAGE_NAME_PREFIX)-solr:$(VERSION) $(IMAGE_NAME_PREFIX)-solr:latest
$(DOCKER_COMMAND) tag $(IMAGE_NAME_PREFIX)-proxy:$(VERSION) $(IMAGE_NAME_PREFIX)-proxy:latest
# Build images (plain output for CI)
build-plain:
BUILD_OPT="$(BUILD_OPT) --progress plain" $(MAKE) build
# Tag latest images with tag VERSION (default: latest)
tag:
$(DOCKER_COMMAND) tag $(IMAGE_NAME_PREFIX):latest $(IMAGE_NAME_PREFIX):$(VERSION)
$(DOCKER_COMMAND) tag $(IMAGE_NAME_PREFIX)-solr:latest $(IMAGE_NAME_PREFIX)-solr:$(VERSION)
$(DOCKER_COMMAND) tag $(IMAGE_NAME_PREFIX)-proxy:latest $(IMAGE_NAME_PREFIX)-proxy:$(VERSION)
# Tag latest images with most recent git commit hash
tag-commit:
VERSION=$(GIT_COMMIT_HASH) $(MAKE) tag
# List images
lsi:
docker image ls 'instituutnederlandsetaal/blacklab*'
# Remove images with tag VERSION (default: latest)
rmi:
$(DOCKER_COMMAND) rmi $(IMAGE_NAME_PREFIX):$(VERSION)
$(DOCKER_COMMAND) rmi $(IMAGE_NAME_PREFIX)-solr:$(VERSION)
$(DOCKER_COMMAND) rmi $(IMAGE_NAME_PREFIX)-proxy:$(VERSION)
rmi-commit:
VERSION=$(GIT_COMMIT_HASH) $(MAKE) rmi
# Tag latest images with VERSION (default: latest) and push to Docker Hub
push: tag
$(DOCKER_COMMAND) push $(IMAGE_NAME_PREFIX):$(VERSION)
$(DOCKER_COMMAND) push $(IMAGE_NAME_PREFIX)-solr:$(VERSION)
$(DOCKER_COMMAND) push $(IMAGE_NAME_PREFIX)-proxy:$(VERSION)
$(DOCKER_COMMAND) push $(IMAGE_NAME_PREFIX):latest
$(DOCKER_COMMAND) push $(IMAGE_NAME_PREFIX)-solr:latest
$(DOCKER_COMMAND) push $(IMAGE_NAME_PREFIX)-proxy:latest
# Tag images with most recent git commit hash and push them to Docker Hub
push-commit:
VERSION=$(GIT_COMMIT_HASH) $(MAKE) push
# Build images and push them to Docker Hub.
# Pass VERSION to tag images with a specific version.
release: build push
:
release-commit:
VERSION=$(GIT_COMMIT_HASH) $(MAKE) release
# Pull images with version VERSION (default: latest) from Docker Hub
pull:
$(DOCKER_COMMAND) pull $(IMAGE_NAME_PREFIX):$(VERSION)
$(DOCKER_COMMAND) pull $(IMAGE_NAME_PREFIX)-solr:$(VERSION)
$(DOCKER_COMMAND) pull $(IMAGE_NAME_PREFIX)-proxy:$(VERSION)
# Pull images with version VERSION (default: latest) from Docker Hub
pull-commit:
VERSION=$(GIT_COMMIT_HASH) $(MAKE) pull
# Stop application
stop:
$(COMPOSE_COMMAND) stop
# Run application with local dev config
# (will build images only if missing)
dev:
$(COMPOSE_COMMAND_DEV) up -d
# Restart application
restart:
$(COMPOSE_COMMAND) restart
# Show application logs
logs:
$(COMPOSE_COMMAND) logs
# Show and follow application logs
# (Ctrl+C to exit)
logs-f:
$(COMPOSE_COMMAND) logs -f
# Start application
up:
$(COMPOSE_COMMAND) up -d
# Deploy application to server (based on images from Docker Hub) with production config
# by pulling the newest versions of images, then using them to bring the application up
deploy: pull
$(COMPOSE_COMMAND) up -d --no-build
# Tag images with most recent git commit hash and push them to Docker Hub
deploy-commit:
VERSION=$(GIT_COMMIT_HASH) $(MAKE) deploy