-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
135 lines (117 loc) · 4.14 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
export IMAGE_NAME?=xirixiz/docker-helloworld-webserver
export APP_VERSION=1.0
export GIT_BRANCH=`git rev-parse --abbrev-ref HEAD`
export VCS_REF=`git rev-parse --short HEAD`
export VCS_URL=https://github.com/xirixiz/docker-helloworld-webserver
export BUILD_DATE=`date -u +"%d-%m-%YT%H:%M:%SZ"`
export TAG_DATE=`date -u +"%d%m%Y"`
export BASE_VERSION=busybox:latest
export QEMU_VERSION=5.1.0-2
export BUILD_IMAGE_NAME=local/alpine-base
export TARGET_ARCHITECTURES=amd64 arm64v8 arm32v7 arm32v6
export QEMU_ARCHITECTURES=arm aarch64
export DOCKER?=docker --config=~/.docker
export DOCKER_CLI_EXPERIMENTAL=enabled
export SHELL=/bin/bash
# Set the Docker TAG value based on the branch name. If not master, then always development
ifeq ($(GIT_BRANCH), master)
DOCKER_TAG=latest-${APP_VERSION}
else
DOCKER_TAG=development-${APP_VERSION}
endif
# Permanent local overrides
-include .env
.PHONY: build qemu wrap push manifest clean
qemu:
@echo "==> Setting up QEMU"
-$(DOCKER) run --rm --privileged multiarch/qemu-user-static:register --reset
-mkdir -p tmp/qemu
$(foreach ARCH, $(QEMU_ARCHITECTURES), make fetch-qemu-$(ARCH);)
@echo "==> Done setting up QEMU"
fetch-qemu-%:
$(eval ARCH := $*)
@echo "--> Fetching QEMU binary for $(ARCH)"
cd tmp/qemu && \
wget -N https://github.com/multiarch/qemu-user-static/releases/download/v$(QEMU_VERSION)/qemu-$(ARCH)-static.tar.gz && \
tar -zxf qemu-$(ARCH)-static.tar.gz && \
cp qemu-$(ARCH)-static ../../qemu/
@echo "--> Done."
wrap:
@echo "==> Building local base containers"
$(foreach ARCH, $(TARGET_ARCHITECTURES), make wrap-$(ARCH);)
@echo "==> Done."
wrap-amd64:
$(DOCKER) pull amd64/$(BASE_VERSION)
$(DOCKER) tag amd64/$(BASE_VERSION) $(BUILD_IMAGE_NAME):amd64
wrap-translate-%:
@if [[ "$*" == "arm64v8" ]] ; then \
echo "aarch64"; \
else \
echo "arm"; \
fi
wrap-%:
$(eval ARCH := $*)
@echo "--> Building local base container for $(ARCH)"
$(DOCKER) build --build-arg BUILD_DATE=$(BUILD_DATE) \
--build-arg ARCH=$(shell make wrap-translate-$(ARCH)) \
--build-arg BASE=$(ARCH)/$(BASE_VERSION) \
--build-arg VCS_REF=$(VCS_REF) \
--build-arg VCS_URL=$(VCS_URL) \
-t $(BUILD_IMAGE_NAME):$(ARCH) qemu
@echo "--> Done building local base container for $(ARCH)"
build:
@echo "==> Building all containers"
$(foreach ARCH, $(TARGET_ARCHITECTURES), make build-$(ARCH);)
@echo "==> Done."
build-%:
$(eval ARCH := $*)
@echo "--> Building $(ARCH)"
$(DOCKER) build --build-arg BUILD_DATE=$(BUILD_DATE) \
--build-arg ARCH=$(ARCH) \
--build-arg BASE=$(BUILD_IMAGE_NAME):$(ARCH) \
--build-arg VCS_REF=$(VCS_REF) \
--build-arg VCS_URL=$(VCS_URL) \
-t $(IMAGE_NAME):$(ARCH) src
@echo "--> Done building $(ARCH)"
push:
@echo "==> Pushing $(IMAGE_NAME)"
$(foreach ARCH, $(TARGET_ARCHITECTURES), make push-$(ARCH);)
@echo "==> Done."
push-%:
$(eval ARCH := $*)
$(DOCKER) tag $(IMAGE_NAME):$(ARCH) $(IMAGE_NAME):${DOCKER_TAG}-$(ARCH)
$(DOCKER) push $(IMAGE_NAME):$(DOCKER_TAG)-$(ARCH)
expand-%:
@if [ "$*" == "amd64" ] ; then \
echo '--arch $*'; \
elif [[ "$*" == *"arm"* ]] ; then \
echo '--arch arm --variant $*' | cut -c 1-21,27-; \
fi
manifest:
@echo "==> Building multi-architecture manifest"
$(foreach STEP, build push, make $(STEP)-manifest;)
@echo "==> Done."
build-manifest:
@echo "--> Creating manifest"
$(eval DOCKER_CONFIG := $(shell echo "$(DOCKER)" | cut -f 2 -d=)/config.json)
cat $(DOCKER_CONFIG) | grep -v auth
$(DOCKER) manifest create --amend \
$(IMAGE_NAME):latest \
$(foreach ARCH, $(TARGET_ARCHITECTURES), $(IMAGE_NAME):${DOCKER_TAG}-$(ARCH) )
$(foreach ARCH, $(TARGET_ARCHITECTURES), \
$(DOCKER) manifest annotate \
$(IMAGE_NAME):latest \
$(IMAGE_NAME):${DOCKER_TAG}-$(ARCH) $(shell make expand-$(ARCH));)
push-manifest:
@echo "--> Pushing manifest"
$(DOCKER) manifest push $(IMAGE_NAME):latest
clean:
@echo "==> Cleaning up tmp folder..."
rm -rf tmp/*
rm -rf qemu/qemu-*
@echo "==> Cleaning up old images..."
-$(DOCKER) rm -fv $$($(DOCKER) ps -a -q -f status=exited)
-$(DOCKER) rmi -f $$($(DOCKER) images -q -f dangling=true)
-$(DOCKER) rmi -f $(BUILD_IMAGE_NAME)
-$(DOCKER) rmi -f $$($(DOCKER) images --format '{{.Repository}}:{{.Tag}}' | grep $(IMAGE_NAME))
@echo "==> Done."