-
Notifications
You must be signed in to change notification settings - Fork 1
/
Makefile
85 lines (73 loc) · 2.87 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
.DEFAULT_GOAL := image
# Docker image name and tag (e.g. reg/image)
REGISTRY := $(or $(DOCKER_USER), jbrinkmann)/
IMAGE := dotfiles
# The git branch name (e.g. primary, develop, ...)
BRANCH := $(or $(TRAVIS_BRANCH),`git rev-parse --abbrev-ref HEAD | tr / -`)
# Latest git tag (e.g. 0.0.0-1)
VERSION := $(shell git describe --tags |sed 's/-g[a-z0-9]\{7\}//')
# Include the branch name when not on primary (e.g. reg/image:0.0.0-1-develop)
TAG := $(shell [ "$(BRANCH)" = "primary" ] \
&& echo "$(REGISTRY)$(IMAGE):$(VERSION)" \
|| echo "$(REGISTRY)$(IMAGE):$(VERSION)-$(BRANCH)" \
)
# Additionally, include the git commit hash
COMMIT := $(or $(TRAVIS_COMMIT),`git rev-parse --short HEAD`)
.PHONY: image
image: ## Build the Dockerfile as an image.
# TODO: use `--squash` when it is no longer experimental
docker build --rm=true --compress --force-rm \
-t $(TAG) $(CURDIR)
.PHONY: tag
tag: ## Tagging with the commit hash allows immutable deploys.
docker tag $(TAG) $(TAG)-$(COMMIT)
ifeq ($(BRANCH),primary)
docker tag $(TAG) $(REGISTRY)$(IMAGE):latest
endif
.PHONY: login
login: ## Login to Docker registry.
@$(if $(and $(DOCKER_USER), $(DOCKER_PASS)), docker login -u $(DOCKER_USER) -p $(DOCKER_PASS), docker login)
.PHONY: push
push: login ## Publish image to Docker registry.
docker push $(REGISTRY)$(IMAGE)
.PHONY: debug
debug: ## Echo resolved variables.
@echo "REGISTRY: $(REGISTRY)"
@echo "IMAGE: $(IMAGE)"
@echo "BRANCH: $(BRANCH)"
@echo "VERSION: $(VERSION)"
@echo "TAG: $(TAG)"
@echo "COMMIT: $(COMMIT)"
# if this session isn't interactive, then we don't want to allocate a
# TTY, which would fail, but if it is interactive, we do want to attach
# so that the user can send e.g. ^C through.
# https://github.com/jessfraz/dockerfiles/blob/master/Makefile#L35
INTERACTIVE := $(shell [ -t 0 ] && echo 1 || echo 0)
ifeq ($(INTERACTIVE), 1)
DOCKER_FLAGS += -t
endif
# if we're running inside Docker Toolbox (Windows/Mac) then we need
# to change the socket (named pipe) to communicate with the host system
# docker daemon (otherwise, we cannot develop docker inside this dev-env)
NIXHOST := $(shell [ -e /var/run/docker.sock ] && echo 1 || echo 0)
ifeq ($(NIXHOST), 1)
SOCKETLOC += /var/run/docker.sock:/var/run/docker.sock:ro
else
SOCKETLOC += \\.\pipe\docker_engine:\\.\pipe\docker_engine:ro
endif
.PHONY: run
run: ## Run the Dockerfile in a container.
docker run --rm -i $(DOCKER_FLAGS) \
--name $(IMAGE) \
-v $(SOCKETLOC) \
$(TAG) || exit 0
.PHONY: clean
clean: clean-docker ## Clean up everything.
.PHONY: clean-docker
clean-docker:
@docker rmi -f $(shell docker images |grep $(REGISTRY)$(IMAGE) |awk '{print $$3}')
@docker rmi -f $(shell docker images --filter dangling=true -q)
.PHONY: all
all: debug image tag push
help:
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'