-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
178 lines (144 loc) · 5.57 KB
/
Makefile
File metadata and controls
178 lines (144 loc) · 5.57 KB
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
-include .env
DOCKER_RUNNER ?= docker-compose run --rm
C_GREEN ?= \e[32m
C_RED ?= \e[31m
C_RESET ?= \e[0m
AWS_DEFAULT_REGION ?= $(shell cat ~/.aws/config | grep -m 1 region | sed 's/region = //')
AWS_ACCOUNT_ID ?= $(shell aws sts get-caller-identity --query "Account" --output text)
DOCKER_REGISTRY_URL ?= ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_DEFAULT_REGION}.amazonaws.com
DOCKER_REPOSITORY ?= devops-wordpress
PROJECT_NAME ?= devops-wordpress
S3_BUCKET ?= da-devops-terraform
pull-required-images:
@if [ -z "$(shell docker image ls --filter=reference=amazon/aws-cli -q)" ]; then\
docker-compose pull aws;\
fi
@if [ -z "$(shell docker image ls --filter=reference=stedolan/jq -q)" ]; then\
docker-compose pull jq;\
fi
@if [ -z "$(shell docker image ls --filter=reference=hashicorp/terraform -q)" ]; then\
docker-compose pull ci-terraform;\
fi
.PHONY:pull-required-images
docs-terraform:
@scripts/update-terraform-docs.sh
.PHONY:docs-terraform
build-wp:
@cd docker \
&& DOCKER_REPOSITORY=${DOCKER_REPOSITORY} \
$(MAKE) build
.PHONY: build-wp
push-wp:
@cd docker \
&& DOCKER_REPOSITORY=${DOCKER_REPOSITORY} \
AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION} \
DOCKER_REGISTRY_URL=${DOCKER_REGISTRY_URL} \
$(MAKE) push
.PHONY: push-wp
deploy-wp:
@$(DOCKER_RUNNER) \
-e PROJECT_NAME=${PROJECT_NAME} \
-e DOCKER_REGISTRY_URL=${DOCKER_REGISTRY_URL} \
-e DOCKER_REPOSITORY=${DOCKER_REPOSITORY} \
--entrypoint /bin/sh \
aws \
scripts/deploy-wp.sh
.PHONY: deploy-wp
ecr-login: pull-required-images
@echo "\n === 🔐 Login to ECR docker registry: ${C_GREEN}${DOCKER_REGISTRY_URL}${C_RESET}\n"
@$(DOCKER_RUNNER) aws ecr get-login-password \
--region ${AWS_DEFAULT_REGION} \
| docker login \
--username AWS \
--password-stdin ${DOCKER_REGISTRY_URL}
@echo "\n === ✅ Done"
.PHONY:ecr_login
update-wp: ecr-login build-wp push-wp
.PHONY:update-wp
ga-test-env-files:
@if [ ! -f ".github/secrets" ]; then \
echo "${C_RED}must create secrets file in .github/secrets"; \
echo "check template in .github/templates/secrets.template"; \
exit 1; \
fi
@if [ ! -f ".github/.env" ]; then \
echo "${C_RED}must create secrets file in .github/.env"; \
echo "check template in .github/templates/.env.template"; \
exit 1; \
fi
.PHONY:ga-test-env-files
ga-test-pr-build-wp: ga-test-env-files
act pull_request \
--job build_push_wp \
--secret-file .github/secrets \
--env-file .github/.env \
-P ubuntu-20.04=flemay/musketeers
.PHONY:ga-test-pr-build-wp
ga-test-push-deploy-wp: ga-test-env-files
act push \
--job deploy_wp \
--secret-file .github/secrets \
--env-file .github/.env \
-P ubuntu-20.04=flemay/musketeers
.PHONY:ga-test-push-wp
aws-caller-identity:
@echo "Retrieving AWS caller identity"
@$(DOCKER_RUNNER) aws sts get-caller-identity --output json
.PHONY:test-aws
tf-backend-storage:
@echo ">> dynamodb create-table"
@$(DOCKER_RUNNER) aws dynamodb create-table \
--table-name ${S3_BUCKET}-lock \
--attribute-definitions AttributeName=LockID,AttributeType=S \
--key-schema AttributeName=LockID,KeyType=HASH \
--provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 \
--tags Key=Name,Value=${PROJECT_NAME}-dynamodb
@echo ">> s3 mb"
@$(DOCKER_RUNNER) aws s3 mb s3://${S3_BUCKET} --region ap-southeast-2
.PHONY:tf-backend-storage
tf-ci-plan:
@$(DOCKER_RUNNER) ci-terraform init
@$(DOCKER_RUNNER) ci-terraform plan -var-file="main.tfvars" -out terraform-plan
.PHONY:tf-ci-plan
tf-ci-apply:
@$(DOCKER_RUNNER) ci-terraform apply -auto-approve "terraform-plan"
.PHONY:tf-ci-apply
tf-ci-remove:
@$(DOCKER_RUNNER) ci-terraform destroy -auto-approve -var-file="main.tfvars"
.PHONY:tf-ci-remove
tf-ci-lb:
@$(DOCKER_RUNNER) aws elbv2 describe-load-balancers --names ${PROJECT_NAME}-lb --query LoadBalancers[].DNSName --output text
.PHONY:tf-ci-lb
tf-ci-lbarn:
@$(DOCKER_RUNNER) aws elbv2 describe-load-balancers --names ${PROJECT_NAME}-lb --query LoadBalancers[].LoadBalancerArn --output text
.PHONY:tf-ci-lbarn
tf-all:
make tf-ci-plan
make tf-ci-apply
.PHONY: tf-all
kick-n-run: pull-required-images
@echo "${C_GREEN}"
@echo "This process will kickoff all terraform modules and apply to your AWS account"
@echo "It will also build and push a fresh docker image for the ECR"
@echo "The whole process might take up to 7-10min till finish (applying and waiting resource provisioning)"
@echo "${C_RED}"
@echo -n "\nContinue? [y/N] If yes, literally kick 🦶 and run 🏃♀️🏃♂️💨💨\n" && read ans && [ $${ans:-N} = y ]
@echo "${C_RESET}"
@$(DOCKER_RUNNER) ci-terraform init
@$(DOCKER_RUNNER) ci-terraform apply -auto-approve -var-file=main.tfvars -target=module.container_registry
$(MAKE) update-wp
@$(DOCKER_RUNNER) ci-terraform apply -auto-approve -var-file=main.tfvars
# $(MAKE) wait-lb
.PHONY:kick-n-run
wait-lb: pull-required-images
@echo "${C_RED}Waiting until the LB is ready...${C_RESET}"
@$(DOCKER_RUNNER) aws elbv2 wait load-balancer-available --load-balancer-arns $(make tf-ci-lbarn)
@sleep 2m
#@echo "${C_RED}Waiting until the Target Groups is ready...${C_RESET}"
#@$(DOCKER_RUNNER) aws elbv2 wait target-in-service --target-group-arn $(shell $(DOCKER_RUNNER) jq -r ".outputs[\"lb-module\"].value.target_group_arn" ./terraform/terraform.tfstate)
@echo "${C_GREEN}Green is good, the LB was provisioned, but the targets might be in registering stage yet.${C_RESET}"
#@echo "\bOpen the browser at http://$(shell $(DOCKER_RUNNER) jq -r ".outputs[\"lb-module\"].value.load_balancer.dns_name" ./terraform/terraform.tfstate)"
@echo "DNS name outputted below"
$(MAKE) tf-ci-lb
@echo "give it a few minutes to boot up"
.PHONY:wait-lb