|
| 1 | +# This Makefile automates possible operations of this project. |
| 2 | +# |
| 3 | +# Images and description on Docker Hub will be automatically rebuilt on |
| 4 | +# pushes to `master` branch of this repo and on updates of parent image. |
| 5 | +# |
| 6 | +# Note! Docker Hub `post_push` hook must be always up-to-date with default |
| 7 | +# values of current Makefile. To update it just use: |
| 8 | +# make post-push-hook |
| 9 | +# |
| 10 | +# It's still possible to build, tag and push images manually. Just use: |
| 11 | +# make release |
| 12 | + |
| 13 | + |
| 14 | +IMAGE_NAME := instrumentisto/clippy |
| 15 | +VERSION ?= 0.0.207 |
| 16 | +TAGS ?= 0.0.207,0.0,0,latest |
| 17 | + |
| 18 | + |
| 19 | +comma := , |
| 20 | +eq = $(if $(or $(1),$(2)),$(and $(findstring $(1),$(2)),\ |
| 21 | + $(findstring $(2),$(1))),1) |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +# Build Docker image. |
| 26 | +# |
| 27 | +# Usage: |
| 28 | +# make image [VERSION=<image-version>] |
| 29 | +# [no-cache=(no|yes)] |
| 30 | + |
| 31 | +image: |
| 32 | + docker build --network=host --force-rm \ |
| 33 | + $(if $(call eq,$(no-cache),yes),--no-cache --pull,) \ |
| 34 | + -t $(IMAGE_NAME):$(VERSION) . |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | +# Tag Docker image with given tags. |
| 39 | +# |
| 40 | +# Usage: |
| 41 | +# make tags [VERSION=<image-version>] |
| 42 | +# [TAGS=<docker-tag-1>[,<docker-tag-2>...]] |
| 43 | + |
| 44 | +tags: |
| 45 | + $(foreach tag,$(subst $(comma), ,$(TAGS)),\ |
| 46 | + $(call tags.do,$(VERSION),$(tag))) |
| 47 | +define tags.do |
| 48 | + $(eval from := $(strip $(1))) |
| 49 | + $(eval to := $(strip $(2))) |
| 50 | + docker tag $(IMAGE_NAME):$(from) $(IMAGE_NAME):$(to) |
| 51 | +endef |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | +# Manually push Docker images to Docker Hub. |
| 56 | +# |
| 57 | +# Usage: |
| 58 | +# make push [TAGS=<docker-tag-1>[,<docker-tag-2>...]] |
| 59 | + |
| 60 | +push: |
| 61 | + $(foreach tag,$(subst $(comma), ,$(TAGS)),\ |
| 62 | + $(call push.do,$(tag))) |
| 63 | +define push.do |
| 64 | + $(eval tag := $(strip $(1))) |
| 65 | + docker push $(IMAGE_NAME):$(tag) |
| 66 | +endef |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | +# Make manual release of Docker images to Docker Hub. |
| 71 | +# |
| 72 | +# Usage: |
| 73 | +# make release [VERSION=<image-version>] [no-cache=(no|yes)] |
| 74 | +# [TAGS=<docker-tag-1>[,<docker-tag-2>...]] |
| 75 | + |
| 76 | +release: | image tags push |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | +# Create `post_push` Docker Hub hook. |
| 81 | +# |
| 82 | +# When Docker Hub triggers automated build all the tags defined in `post_push` |
| 83 | +# hook will be assigned to built image. It allows to link the same image with |
| 84 | +# different tags, and not to build identical image for each tag separately. |
| 85 | +# See details: |
| 86 | +# http://windsock.io/automated-docker-image-builds-with-multiple-tags |
| 87 | +# |
| 88 | +# Usage: |
| 89 | +# make post-push-hook [TAGS=<docker-tag-1>[,<docker-tag-2>...]] |
| 90 | + |
| 91 | +post-push-hook: |
| 92 | + @mkdir -p hooks/ |
| 93 | + docker run --rm -v $(PWD)/post_push.tmpl:/post_push.tmpl:ro \ |
| 94 | + -e TAGS='$(TAGS)' \ |
| 95 | + hairyhenderson/gomplate:slim -f /post_push.tmpl \ |
| 96 | + > hooks/post_push |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | +# Run Bats tests for Docker image. |
| 101 | +# |
| 102 | +# Documentation of Bats: |
| 103 | +# https://github.com/sstephenson/bats |
| 104 | +# |
| 105 | +# Usage: |
| 106 | +# make test [VERSION=<image-version>] |
| 107 | + |
| 108 | +test: deps.bats |
| 109 | + IMAGE=$(IMAGE_NAME):$(VERSION) test/bats/bats test/suite.bats |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | +# Resolve project dependencies for running tests. |
| 114 | +# |
| 115 | +# Usage: |
| 116 | +# make deps.bats [BATS_VER=<bats-version>] |
| 117 | + |
| 118 | +BATS_VER ?= 0.4.0 |
| 119 | + |
| 120 | +deps.bats: |
| 121 | +ifeq ($(wildcard test/bats),) |
| 122 | + @mkdir -p test/bats/vendor/ |
| 123 | + curl -fL -o test/bats/vendor/bats.tar.gz \ |
| 124 | + https://github.com/sstephenson/bats/archive/v$(BATS_VER).tar.gz |
| 125 | + tar -xzf test/bats/vendor/bats.tar.gz -C test/bats/vendor/ |
| 126 | + @rm -f test/bats/vendor/bats.tar.gz |
| 127 | + ln -s $(PWD)/test/bats/vendor/bats-$(BATS_VER)/libexec/* test/bats/ |
| 128 | +endif |
| 129 | + |
| 130 | + |
| 131 | + |
| 132 | +.PHONY: image tags push release post-push-hook test deps.bats |
0 commit comments