forked from bcgov/startup-sample-project-aws-containers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
makefile
208 lines (164 loc) · 9.06 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!make
-include .env
export $(shell sed 's/=.*//' .env)
export GIT_LOCAL_BRANCH?=$(shell git rev-parse --abbrev-ref HEAD)
export DEPLOY_DATE?=$(shell date '+%Y%m%d%H%M')
export COMMIT_SHA?=$(shell git rev-parse --short=7 HEAD)
export IMAGE_TAG=${COMMIT_SHA}-${DEPLOY_DATE}
##############################################################
# Define default environment variables for local development #
##############################################################
export PROJECT := $(or $(PROJECT),ssp)
export PROFILE := $(or $(PROFILE),ssp-dev)
export DB_USER := $(or $(DB_USER),development)
export DB_PASSWORD := $(or $(DB_PASSWORD),development)
export DB_NAME := $(or $(DB_NAME),development)
export DB_SERVER := $(or $(DB_SERVER),mongodb)
define deployTag
"${PROJECT}-${DEPLOY_DATE}"
endef
export ACCOUNT_ID := $(shell aws sts get-caller-identity 2>/dev/null | jq '.Account')
export DEPLOYMENT_IMAGE := "$(ACCOUNT_ID).dkr.ecr.$(REGION).amazonaws.com/$(PROJECT):$(IMAGE_TAG)"
#################
# Status Output #
#################
print-status:
@echo " +---------------------------------------------------------+ "
@echo " | Current Settings | "
@echo " +---------------------------------------------------------+ "
@echo " | ACCOUNT ID: $(ACCOUNT_ID) "
@echo " | PROJECT: $(PROJECT) "
@echo " | REGION: $(REGION) "
@echo " | PROFILE: $(PROFILE) "
@echo " | GIT LOCAL BRANCH: $(GIT_LOCAL_BRANCH) "
@echo " | COMMIT_SHA: $(COMMIT_SHA) "
@echo " | IMAGE_TAG: $(IMAGE_TAG) "
@echo " +---------------------------------------------------------+ "
# If no .env file exists in the project root dir, run `make setup-development-env` and fill in credentials
pipeline-deploy-dev: | pipeline-build pipeline-push pipeline-deploy-prep pipeline-deploy-version
local: | build-local run-local ## Task-Alias -- Run the steps for local development
#####################
# Local Development #
#####################
build-local: ## -- Target : Builds the local development containers.
@echo "+\n++ Make: Building local Docker image ...\n+"
@docker-compose -f docker-compose.dev.yml build
run-local: ## -- Target : Runs the local development containers.
@echo "+\n++ Make: Running locally ...\n+"
@docker-compose -f docker-compose.dev.yml up
run-local-db: ## -- Target : Runs the local development containers.
@echo "+\n++ Make: Running db locally ...\n+"
@docker-compose -f docker-compose.dev.yml up mongodb
close-local: ## -- Target : Closes the local development containers.
@echo "+\n++ Make: Closing local container ...\n+"
@docker-compose -f docker-compose.dev.yml down
local-client-workspace:
@docker exec -it $(PROJECT)-client bash
local-server-workspace:
@docker exec -it $(PROJECT)-server bash
local-database-workspace:
@docker exec -it $(PROJECT)-mongodb bash
local-db-seed:
@docker exec -it $(PROJECT)-server npm run db:seed
local-db-migration:
@docker exec -it $(PROJECT)-server npm run db:migration
local-server-tests:
@docker exec -it $(PROJECT)-server npm test
####################
# Utility commands #
####################
check_aws_login:
@echo AWS ACCOUNT_ID: ${ACCOUNT_ID}
setup-image-repository: check_aws_login
@cd terraform/ecr && terraform init && terraform apply
setup-aws-ecs-linked-role: check_aws_login
@echo "Creating ECS service linked role."
-@aws iam create-service-linked-role --aws-service-name ecs.amazonaws.com
# Provision required infrastructure/services for deployment in AWS.
setup-aws-infrastructure: pipeline-push setup-aws-ecs-linked-role
@echo "Provisioning services in AWS...\n+"
@aws ecs put-account-setting --name containerInstanceLongArnFormat --value enabled
@aws ecs put-account-setting --name serviceLongArnFormat --value enabled
@aws ecs put-account-setting --name taskLongArnFormat --value enabled
@terraform init terraform/aws
@terraform apply -var client_app_image=$(DEPLOYMENT_IMAGE) terraform/aws
# De-provision infrastructure/services in AWS.
cleanup-aws-infrastructure: check_aws_login
@echo "De-provisioning services in AWS...\n+"
@terraform init terraform/aws
@terraform destroy terraform/aws
# Set an AWS profile for pipeline
setup-aws-profile:
@echo "+\n++ Make: Setting AWS Profile...\n+"
@aws configure set aws_access_key_id $(AWS_ACCESS_KEY_ID) --profile $(PROFILE)
@aws configure set aws_secret_access_key $(AWS_SECRET_ACCESS_KEY) --profile $(PROFILE)
@aws configure set aws_session_token $(AWS_SESSION_TOKEN) --profile $(PROFILE)
setup-development-env:
@echo "+\n++ Make: Preparing project for dev environment...\n+"
@cp .config/.env.example ./.env
##########################################
# Pipeline build and deployment commands #
##########################################
pipeline-build:
@echo "+\n++ Performing build of Docker images...\n+"
@echo "Building images with: $(GIT_LOCAL_BRANCH)"
@docker-compose -f docker-compose.yml build
pipeline-push: setup-aws-profile setup-image-repository
@echo "+\n++ Pushing image to container registry...\n+"
@aws --region $(REGION) --profile $(PROFILE) ecr get-login-password | docker login --username AWS --password-stdin $(ACCOUNT_ID).dkr.ecr.$(REGION).amazonaws.com
@docker tag $(PROJECT):$(GIT_LOCAL_BRANCH) $(DEPLOYMENT_IMAGE)
@docker push $(DEPLOYMENT_IMAGE)
@echo "DEPLOYMENT_IMAGE is $(DEPLOYMENT_IMAGE)"
pipeline-deploy-version:
@echo "+\n++ Deploying to ECS...\n+"
@terraform apply --var app_image=... // re-runs plan now that image is defined...should be no-op for most things, except ECS task, service, and some other bits.
pipeline-healthcheck:
@aws --profile $(PROFILE) elasticbeanstalk describe-environments --application-name $(PROJECT) --environment-name $(DEPLOY_ENV) --query 'Environments[*].{Status: Status, Health: Health}'
##########################################
# GH deployment commands #
##########################################
gh-pipeline-push:
@echo "+\n++ Pushing image to Dockerhub...\n+"
# @$(shell aws ecr get-login --no-include-email --region $(REGION) --profile $(PROFILE))
@aws --region $(REGION) ecr get-login-password | docker login --username AWS --password-stdin $(ACCOUNT_ID).dkr.ecr.$(REGION).amazonaws.com
@docker tag $(PROJECT):$(GIT_LOCAL_BRANCH) $(ACCOUNT_ID).dkr.ecr.$(REGION).amazonaws.com/$(PROJECT):$(IMAGE_TAG)
@docker push $(ACCOUNT_ID).dkr.ecr.$(REGION).amazonaws.com/$(PROJECT):$(IMAGE_TAG)
gh-pipeline-deploy-prep:
@echo "+\n++ Creating Dockerrun.aws.json file...\n+"
@.build/build_dockerrun.sh > Dockerrun.aws.json
gh-pipeline-deploy-version:
@echo "+\n++ Deploying to Elasticbeanstalk...\n+"
@zip -r $(call deployTag).zip Dockerrun.aws.json
@aws configure set region $(REGION)
@aws s3 cp $(call deployTag).zip s3://$(S3_BUCKET)/$(PROJECT)/$(call deployTag).zip
@aws elasticbeanstalk create-application-version --application-name $(PROJECT) --version-label $(call deployTag) --source-bundle S3Bucket="$(S3_BUCKET)",S3Key="$(PROJECT)/$(call deployTag).zip"
@aws elasticbeanstalk update-environment --application-name $(PROJECT) --environment-name $(DEPLOY_ENV) --version-label $(call deployTag)
##########################################
# IMG Promotion commands #
##########################################
pipeline-promote-prep:
@echo "--------------------------------------------------------------------------------";
@echo "NOTE: This requires the PROMOTE_FROM_TAG and PROMOTE_TO_TAG be set in .build/image_promote.sh"
@echo "--------------------------------------------------------------------------------";
@echo "\nPromoting to Image Registry...\n"
@.build/promote_img.sh
pipeline-promote-staging:
@echo "+\n++ Promoting to Elasticbeanstalk [STAGING]...\n+"
@zip -r $(call deployTag)_staging.zip Dockerrun.aws.json
@aws --profile $(PROFILE) configure set region $(REGION)
@aws --profile $(PROFILE) s3 cp $(call deployTag)_staging.zip s3://$(S3_BUCKET)/$(PROJECT)/$(call deployTag)_staging.zip
@aws --profile $(PROFILE) elasticbeanstalk create-application-version --application-name $(PROJECT) --version-label $(call deployTag) --source-bundle S3Bucket="$(S3_BUCKET)",S3Key="$(PROJECT)/$(call deployTag)_staging.zip"
@aws --profile $(PROFILE) elasticbeanstalk update-environment --application-name $(PROJECT) --environment-name startup-sample-project-staging --version-label $(call deployTag)
pipeline-promote-prod:
@echo "+\n++ Promoting to Elasticbeanstalk [PRODUCTION]...\n+"
@zip -r $(call deployTag)_prod.zip Dockerrun.aws.json
@aws --profile $(PROFILE) configure set region $(REGION)
@aws --profile $(PROFILE) s3 cp $(call deployTag)_prod.zip s3://$(S3_BUCKET)/$(PROJECT)/$(call deployTag)_prod.zip
@aws --profile $(PROFILE) elasticbeanstalk create-application-version --application-name $(PROJECT) --version-label $(call deployTag) --source-bundle S3Bucket="$(S3_BUCKET)",S3Key="$(PROJECT)/$(call deployTag)_prod.zip"
@aws --profile $(PROFILE) elasticbeanstalk update-environment --application-name $(PROJECT) --environment-name startup-sample-project-prod --version-label $(call deployTag)
##########################################
# Git tagging aliases #
##########################################
tag-dev:
@git tag -fa dev -m "Deploying $(BRANCH):$(IMAGE_TAG) to dev env" $(IMAGE_TAG)
@git push --force origin refs/tags/dev:refs/tags/dev