-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker-makefile
100 lines (82 loc) · 3.36 KB
/
docker-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
lc = $(subst A,a,$(subst B,b,$(subst C,c,$(subst D,d,$(subst E,e,$(subst F,f,$(subst G,g,$(subst H,h,$(subst I,i,$(subst J,j,$(subst K,k,$(subst L,l,$(subst M,m,$(subst N,n,$(subst O,o,$(subst P,p,$(subst Q,q,$(subst R,r,$(subst S,s,$(subst T,t,$(subst U,u,$(subst V,v,$(subst W,w,$(subst X,x,$(subst Y,y,$(subst Z,z,$1))))))))))))))))))))))))))
# The registry we are targetting
REPO_URI := $(or $(DOCKER_REGISTRY),docker-registry.example.com:5000)
# Sets the name of the container being built, get the current path for the container name
CURRENT_PATH := $(notdir $(patsubst %/,%,$(CURDIR)))
# if IMAGE_NAME is undefined use the dir name we are in
NAME := $(call lc,$(or $(IMAGE_NAME),${CURRENT_PATH}))
# Set your version tag here, examples for git short tag and date below
# Defaults to GO_PIPELINE_LABEL
# TAG := $(shell date +%Y%m%d%H%M%S)
TAG := $(or $(GO_PIPELINE_LABEL),$(shell git rev-parse --short HEAD))
# Temp name to build against to avoid name clashes
IMG_BUILD := ${NAME}:tmp
# Full final tag name to push to as well as latest
IMG := ${REPO_URI}/${NAME}:${TAG}
BRANCH := ${REPO_URI}/${NAME}:$(shell git rev-parse --abbrev-ref HEAD | sed -e 's,/,_,g')
LATEST := ${REPO_URI}/${NAME}:latest
TAG_FILE := image_full_tag.txt
# What command do we use to build with
DOCKER_CMD := $(or $(DOCKER_CMD),docker)
# Default commands for running our docker container
CMD :=
# Commands passed to Docker to test the container
TESTS := $(addsuffix .test, $(patsubst %.c,%.o,$(wildcard tests/*)))
# When the bare command 'make' is run what do we do?
all: tag
# If you need to login to Dockerhub
login:
@${DOCKER_CMD} log -u ${DOCKER_USER} -p ${DOCKER_PASS}
.PHONY: login
# Show logs for the temp container
logs:
@${DOCKER_CMD} logs ${NAME}_${TAG}
.PHONY: logs
# Run the temp container, to run a different command call make thus:
# make run CMD=mycommand
run:
@${DOCKER_CMD} run -it ${IMG} ${CMD}
.PHONY: run
# Delete any container images we have built
clean:
-@${DOCKER_CMD} rm ${NAME}_${TAG}
-@${DOCKER_CMD} rmi ${IMG_BUILD}
.PHONY: clean
# Delete any container images we depend on
cleanall: clean
-@${DOCKER_CMD} images -q "*/${NAME}" | xargs docker image rm
.PHONY: cleanall
# Pull the latest version for build speed ups, and the tag we are building to avoid dups
pull:
-@${DOCKER_CMD} pull ${LATEST}
@${DOCKER_CMD} pull ${IMG} || exit 0 && echo Tag already exists upstream at ${IMG}, exiting ; exit 1
.PHONY: pull
# Build a container with a temp tag to avoid clashes
build: export DOCKER_BUILDKIT=1
build: pull
@${DOCKER_CMD} build ${DOCKER_BUILD_ARGS} -t ${IMG_BUILD} .
.PHONY: build
# Test your docker container works
test: build $(TESTS)
.PHONY: test
%.test : %
@echo Running test $<
@${DOCKER_CMD} run --name ${NAME}_${TAG} ${IMG_BUILD} $(shell cat $<)
@${DOCKER_CMD} rm ${NAME}_${TAG}
.PHONY: %.test
# Tag your container with the version tag and latest
tag: test
@${DOCKER_CMD} tag ${IMG_BUILD} ${IMG}
@${DOCKER_CMD} tag ${IMG_BUILD} ${LATEST}
@${DOCKER_CMD} tag ${IMG_BUILD} ${BRANCH}
# Push your container out to the registery
push: tag
@${DOCKER_CMD} push ${IMG}
@${DOCKER_CMD} push ${LATEST}
@${DOCKER_CMD} push ${BRANCH}
@echo ${NAME} built and pushed to ${IMG}
ifdef GO_PIPELINE_NAME
$(info Running in a GoCD pipeline writing image details to ${TAG_FILE})
@echo ${IMG} > ${TAG_FILE}
endif
.PHONY: push