-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
72 lines (55 loc) · 1.89 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
IMAGE_NAME := openstack-starter
CONTAINER_NAME := $(IMAGE_NAME)-shell
COPY_STATE_FILE := .makefile.docker-cp.state
TAG_STATE_FILE := .makefile.image-build.state
# List of files used in the test environement (container)
run_files = .env openrc.sh main.tf .terraform.lock.hcl ansible.cfg site.yml create-server.sh
# Test if container is running
running = $(shell docker ps --filter name=^$(CONTAINER_NAME)$$ --quiet)
# Timestamp is used as tag for docker image
timestamp = $(shell date +%s)
tag = $(shell cat $(TAG_STATE_FILE))
images = $(shell docker images --filter reference=$(IMAGE_NAME):* --format "{{.Repository}}:{{.Tag}}")
# default rule
build: up $(COPY_STATE_FILE)
# The 'state' file prevent to copy 'run_files' without changes.
$(COPY_STATE_FILE): $(run_files)
@touch $@
@echo "Copy this files in the container : $?";
@for file in $?; do \
docker cp $$file $(CONTAINER_NAME):/root/; \
done
image: $(TAG_STATE_FILE)
$(TAG_STATE_FILE): Dockerfile
@echo $(timestamp) > $(TAG_STATE_FILE)
docker build --tag $(IMAGE_NAME):$(timestamp) .
@if [ -n "$(running)" ]; then \
echo "Restart container with new image"; \
$(MAKE) --quiet stop; \
$(MAKE) --quiet run; \
fi
run:
ifeq ($(running),)
@rm --force $(COPY_STATE_FILE)
docker run --interactive --tty --detach --rm --name $(CONTAINER_NAME) $(IMAGE_NAME):$(tag) /bin/bash
endif
up: image run
# Note that you can detach from a container with the Ctrl-P Ctrl-Q sequence.
shell: build
docker attach $(CONTAINER_NAME)
stop:
ifneq ($(running),)
docker rm --force $(CONTAINER_NAME)
endif
clean: stop
@rm --force $(TAG_STATE_FILE)
@if [ -n "$(images)" ]; then \
echo "docker rmi (...)"; \
docker rmi $(images); \
fi
# TODO: use docker image
#gitlab-ci:
# gitlab-runner exec docker --docker-pull-policy=if-not-present build-job
# TODO: register with config file
#gitlab-register:
.PHONY: build image run up shell stop clean gitlab-ci gitlab-register