-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMakefile
133 lines (120 loc) · 4.78 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
## variables:
## AWS_REGION = AWS Region: defaults to us-gov-west-1
## IMAGE = Folder for image: defaults to base
## TAG = Tag for image reference: defaults to latest
## NAMESPACE = Namespace for the repositories
## NEW_TAG = New tag to assign to an image
##
# Gets Account ID
AWS_ACCOUNT_ID:=$(shell aws sts get-caller-identity | jq .Account -r)
# Sets default namespace for repo
NAMESPACE ?= dvp
# Sets default region
AWS_REGION ?= us-gov-west-1
# Sets default tag
TAG ?= dev
# Shorten full repo path
REPOSITORY:=$(AWS_ACCOUNT_ID).dkr.ecr.$(AWS_REGION).amazonaws.com
# Build Args
BUILD_DATE_TIME ?= $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
BUILD_TOOL ?= Makefile
BUILD_VERSION ?= $(shell git rev-parse --short HEAD)
BUILD_NUMBER ?= $(shell echo $$RANDOM)
TARGET ?= base
# https://stackoverflow.com/questions/10858261/abort-makefile-if-variable-not-set
# Fuction to check if variables are defined
check_defined = \
$(strip $(foreach 1,$1, \
$(call __check_defined,$1,$(strip $(value 2)))))
__check_defined = \
$(if $(value $1),, \
$(error Undefined $1$(if $2, ($2))))
.PHONY : help
help : Makefile
@sed -n 's/^##//p' $<
## login: Login to ECR
.PHONY: login
login:
aws ecr get-login-password | docker login --username AWS --password-stdin $(REPOSITORY)
## build/saml: Build saml-proxy image
.PHONY: build/saml
build/saml : IMAGE = saml-proxy
build/saml:
## build: Build Docker image
docker build -t $(REPOSITORY)/$(NAMESPACE)/$(IMAGE):$(TAG) \
-f Dockerfile \
--target deploy \
--build-arg AWS_ACCOUNT_ID=$(AWS_ACCOUNT_ID) \
--build-arg BUILD_DATE_TIME=$(BUILD_DATE_TIME) \
--build-arg BUILD_TOOL=$(BUILD_TOOL) \
--build-arg VERSION=$(BUILD_VERSION) \
--build-arg BUILD_NUMBER=$(BUILD_NUMBER) \
--no-cache .
## build/saml_tests: Build saml-proxy image
.PHONY: build/saml_tests
build/saml_tests : IMAGE = saml-proxy-tests
build/saml_tests:
## build: Build Docker image
docker build -t $(REPOSITORY)/$(NAMESPACE)/$(IMAGE):$(TAG) \
-f Dockerfile.test \
--build-arg AWS_ACCOUNT_ID=$(AWS_ACCOUNT_ID) \
--build-arg BUILD_DATE_TIME=$(BUILD_DATE_TIME) \
--build-arg BUILD_TOOL=$(BUILD_TOOL) \
--build-arg BUILD_VERSION=$(BUILD_VERSION) \
--build-arg BUILD_NUMBER=$(BUILD_NUMBER) \
--no-cache .
## regression: Regression Tests
.PHONY: regression
regression:
@:$(call check_defined, IMAGE, IMAGE variable should be saml-proxy-tests)
docker run \
-v "/var/run/docker.sock:/var/run/docker.sock" \
--rm $(REPOSITORY)/$(NAMESPACE)/$(IMAGE):$(TAG) \
--saml-proxy-url=$(SAML_PROXY_URL) \
--client-id=$(CLIENT_ID) \
--idp=$(IDP) \
--authorization-url=$(AUTHORIZATION_URL) \
--valid-login-gov-user-email=$(VALID_LOGIN_GOV_USER_EMAIL) \
--valid-login-gov-user-seed=$(VALID_LOGIN_GOV_USER_SEED) \
--login-gov-user-password=$(LOGIN_GOV_USER_PASSWORD) \
--icn-error-login-gov-user-email=$(ICN_ERROR_LOGIN_GOV_USER_EMAIL) \
--icn-error-login-gov-user-seed=$(ICN_ERROR_LOGIN_GOV_USER_SEED)
--regression-test-timeout=$(REGRESSION_TEST_TIMEOUT)
## pull: Pull an image to ECR
.PHONY: pull
pull:
@:$(call check_defined, IMAGE, IMAGE variable should be saml-proxy)
docker pull $(REPOSITORY)/$(NAMESPACE)/$(IMAGE):$(TAG)
## push: Pushes an image to ECR
.PHONY: push
push:
@:$(call check_defined, IMAGE, IMAGE variable should be saml-proxy)
docker push $(REPOSITORY)/$(NAMESPACE)/$(IMAGE):$(TAG)
## tag: Adds a tag to an existing image in ECR
.PHONY: tag
tag:
@:$(call check_defined, IMAGE, IMAGE variable should be saml-proxy)
@echo Tagging $(NAMESPACE)/$(IMAGE):$(TAG) with $(NEW_TAG)
@aws ecr put-image --repository-name $(NAMESPACE)/$(IMAGE) --image-tag $(NEW_TAG) --image-manifest "$$(aws ecr batch-get-image --repository-name $(NAMESPACE)/$(IMAGE) --image-ids imageTag=$(TAG) --query 'images[].imageManifest' --output text)" > /dev/null
## labels: Get ECR labels.
.PHONY: get_labels
get_labels:
@:$(call check_defined, IMAGE, IMAGE variable should be saml-proxy)
@echo Getting labels from ECR for $(NAMESPACE)/$(IMAGE):$(TAG)
@aws ecr batch-get-image --repository-name $(NAMESPACE)/$(IMAGE) --image-id imageTag=$(TAG) --accepted-media-types "application/vnd.docker.distribution.manifest.v1+json" --output json |jq -r '.images[].imageManifest' |jq -r '.history[0].v1Compatibility' |jq -r '.config.Labels'
## check_tag: Verifies that the tag exists in the repository
.PHONY: check_tag
check_tag:
@:$(call check_defined, IMAGE, IMAGE variable should be saml-proxy)
@{ \
CHECK_TAG=$$(aws ecr list-images --repository-name $(NAMESPACE)/$(IMAGE) --query 'imageIds[?imageTag==`"$(TAG)"`]' --output text);\
if [[ -z "$${CHECK_TAG}" ]];then\
echo "Image with $(TAG) tag was not found!";\
exit 1;\
fi;\
}
## clean: Removes a local image
.PHONY: clean
clean:
@:$(call check_defined, IMAGE, IMAGE variable should be saml-proxy)
docker image rm $(REPOSITORY)/$(NAMESPACE)/$(IMAGE):$(TAG)