-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMakefile
141 lines (112 loc) · 3.79 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
136
137
138
139
140
141
SHELL := /bin/bash
# ---
# Read default configuration
include config.default
export $(shell sed 's/=.*//' config.default)
# Image to build
BUILD_IMAGE ?= $(BUILD_NAMESPACE)/$(IMAGE_NAME)
export BUILD_IMAGE
# ---
SED_MATCH ?= [^a-zA-Z0-9._-]
ifeq ($(CIRCLECI),true)
# Configure build variables based on CircleCI environment vars
BUILD_NUM = $(CIRCLE_BUILD_NUM)
BRANCH_NAME ?= $(shell sed 's/$(SED_MATCH)/-/g' <<< "$(CIRCLE_BRANCH)")
BUILD_TAG ?= $(shell sed 's/$(SED_MATCH)/-/g' <<< "$(CIRCLE_TAG)")
else
# Not in CircleCI environment, try to set sane defaults
BUILD_NUM = local
BRANCH_NAME ?= $(shell git rev-parse --abbrev-ref HEAD | sed 's/$(SED_MATCH)/-/g')
BUILD_TAG ?= $(shell git tag -l --points-at HEAD | tail -n1 | sed 's/$(SED_MATCH)/-/g')
endif
# If BUILD_TAG is blank there's no tag on this commit
ifeq ($(strip $(BUILD_TAG)),)
# Default to branch name
BUILD_TAG := $(BRANCH_NAME)
else
# Consider this the new :latest image
# FIXME: implement build tests before tagging with :latest
PUSH_LATEST := true
endif
REVISION_TAG = $(shell git rev-parse --short HEAD)
export BUILD_NUM
export BUILD_TAG
export BRANCH_NAME
# ---
# Check necessary commands exist
DOCKER := $(shell command -v docker 2> /dev/null)
COMPOSER := $(shell command -v composer 2> /dev/null)
JQ := $(shell command -v jq 2> /dev/null)
SHELLCHECK := $(shell command -v shellcheck 2> /dev/null)
SHFMT := $(shell command -v shfmt 2> /dev/null)
YAMLLINT := $(shell command -v yamllint 2> /dev/null)
FLAKE8 := $(shell command -v flake8 2> /dev/null)
# ============================================================================
ALL: init prepare lint build push
init:
@chmod 755 .githooks/*
@find .git/hooks -type l -exec rm {} \;
@find .githooks -type f -exec ln -sf ../../{} .git/hooks/ \;
format: format-sh
format-sh:
ifndef SHFMT
$(error "shfmt is not installed: https://github.com/mvdan/sh")
endif
@shfmt -i 2 -ci -w .
lint: init lint-sh lint-py lint-yaml lint-json lint-composer lint-docker
lint-sh:
ifndef SHELLCHECK
$(error "shellcheck is not installed: https://github.com/koalaman/shellcheck")
endif
ifndef SHFMT
$(error "shfmt is not installed: https://github.com/mvdan/sh")
endif
@shfmt -f . | xargs shellcheck
@shfmt -i 2 -ci -d .
lint-py:
ifndef FLAKE8
$(error "flake8 is not installed: https://pypi.org/project/flake8/")
endif
@flake8
lint-yaml:
ifndef YAMLLINT
$(error "yamllint is not installed: https://github.com/adrienverge/yamllint")
endif
@find . -type f -name '*.yml' | xargs yamllint
lint-json:
ifndef JQ
$(error "jq is not installed: https://stedolan.github.io/jq/download/")
endif
@find . -type f -name '*.json' | xargs jq type | grep -q '"object"'
lint-composer:
ifndef COMPOSER
$(warning "composer is not installed: https://getcomposer.org/doc/00-intro.md#installation-linux-unix-macos")
else
@find . -type f -name 'composer*.json' | xargs composer validate >/dev/null
endif
lint-docker:
ifndef DOCKER
$(error "docker is not installed: https://docs.docker.com/install/")
endif
@docker run --rm -i hadolint/hadolint < src/Dockerfile >/dev/null
prepare: Dockerfile
Dockerfile:
envsubst '$${BASE_NAMESPACE},$${BASE_IMAGE},$${BASE_TAG},$${CIRCLECI_USER}' \
< Dockerfile.in > src/Dockerfile
build:
docker build \
--tag=$(BUILD_NAMESPACE)/${IMAGE_NAME}:$(BUILD_TAG) \
--tag=$(BUILD_NAMESPACE)/${IMAGE_NAME}:build-$(BUILD_NUM) \
--tag=$(BUILD_NAMESPACE)/${IMAGE_NAME}:$(REVISION_TAG) \
src
push: push-tag push-latest
push-tag:
docker push $(BUILD_NAMESPACE)/${IMAGE_NAME}:$(BUILD_TAG)
docker push $(BUILD_NAMESPACE)/${IMAGE_NAME}:build-$(BUILD_NUM)
push-latest:
if [[ "$(PUSH_LATEST)" = "true" ]]; then { \
docker tag $(BUILD_NAMESPACE)/${IMAGE_NAME}:$(REVISION_TAG) $(BUILD_NAMESPACE)/${IMAGE_NAME}:latest; \
docker push $(BUILD_NAMESPACE)/${IMAGE_NAME}:latest; \
} else { \
echo "Not tagged.. skipping latest"; \
} fi