|
| 1 | +# Makefile for shipping and testing the container image. |
| 2 | + |
| 3 | +MAKEFLAGS += --warn-undefined-variables |
| 4 | +.DEFAULT_GOAL := build |
| 5 | +.PHONY: * |
| 6 | + |
| 7 | +# we get these from CI environment if available, otherwise from git |
| 8 | +GIT_COMMIT ?= $(shell git rev-parse --short HEAD) |
| 9 | +GIT_BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD) |
| 10 | +WORKSPACE ?= $(shell pwd) |
| 11 | + |
| 12 | +namespace ?= autopilotpattern |
| 13 | +tag := branch-$(shell basename $(GIT_BRANCH)) |
| 14 | +imageWordpress := $(namespace)/wordpress |
| 15 | +imageNginx := $(namespace)/wordpress-nginx |
| 16 | + |
| 17 | +#dockerLocal := DOCKER_HOST= DOCKER_TLS_VERIFY= DOCKER_CERT_PATH= docker |
| 18 | +dockerLocal := docker |
| 19 | +#composeLocal := DOCKER_HOST= DOCKER_TLS_VERIFY= DOCKER_CERT_PATH= docker-compose |
| 20 | +composeLocal := docker-compose |
| 21 | + |
| 22 | +## Display this help message |
| 23 | +help: |
| 24 | + @awk '/^##.*$$/,/[a-zA-Z_-]+:/' $(MAKEFILE_LIST) | awk '!(NR%2){print $$0p}{p=$$0}' | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}' | sort |
| 25 | + |
| 26 | + |
| 27 | +# ------------------------------------------------ |
| 28 | +# Container builds |
| 29 | + |
| 30 | +## Builds the application container image locally |
| 31 | +build: |
| 32 | + $(dockerLocal) build -t=$(imageWordpress):$(tag) . |
| 33 | + cd nginx && $(dockerLocal) build -t=$(imageNginx):$(tag) . |
| 34 | + |
| 35 | +## Push the current application container images to the Docker Hub |
| 36 | +push: |
| 37 | + $(dockerLocal) push $(imageWordpress):$(tag) |
| 38 | + $(dockerLocal) push $(imageNginx):$(tag) |
| 39 | + |
| 40 | +## Tag the current images as 'latest' |
| 41 | +tag: |
| 42 | + $(dockerLocal) tag $(imageWordpress):$(tag) $(imageWordpress):latest |
| 43 | + $(dockerLocal) tag $(imageNginx):$(tag) $(imageNginx):latest |
| 44 | + |
| 45 | +## Push latest tag(s) to the Docker Hub |
| 46 | +ship: tag |
| 47 | + $(dockerLocal) push $(imageWordpress):$(tag) |
| 48 | + $(dockerLocal) push $(imageWordpress):latest |
| 49 | + $(dockerLocal) push $(imageNginx):$(tag) |
| 50 | + $(dockerLocal) push $(imageNginx):latest |
| 51 | + |
| 52 | + |
| 53 | +# ------------------------------------------------ |
| 54 | +# Test running |
| 55 | + |
| 56 | +## Pull the container images from the Docker Hub |
| 57 | +pull: |
| 58 | + $(dockerLocal) pull $(imageWordpress):$(tag) |
| 59 | + $(dockerLocal) pull $(imageNginx):$(tag) |
| 60 | + |
| 61 | +## Print environment for build debugging |
| 62 | +debug: |
| 63 | + @echo WORKSPACE=$(WORKSPACE) |
| 64 | + @echo GIT_COMMIT=$(GIT_COMMIT) |
| 65 | + @echo GIT_BRANCH=$(GIT_BRANCH) |
| 66 | + @echo namespace=$(namespace) |
| 67 | + @echo tag=$(tag) |
| 68 | + @echo imageWordpress=$(imageWordpress) |
| 69 | + @echo imageNginx=$(imageNginx) |
| 70 | + |
| 71 | +# ------------------------------------------------------- |
| 72 | +# helper functions for testing if variables are defined |
| 73 | +# |
| 74 | +check_var = $(foreach 1,$1,$(__check_var)) |
| 75 | +__check_var = $(if $(value $1),,\ |
| 76 | + $(error Missing $1 $(if $(value 2),$(strip $2)))) |
0 commit comments