-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuildspec.yml
More file actions
201 lines (173 loc) · 8.67 KB
/
buildspec.yml
File metadata and controls
201 lines (173 loc) · 8.67 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
version: 0.2
phases:
install:
commands:
- python3 -m pip install --upgrade pip
- pip install yq
pre_build:
on-failure: ABORT
commands:
- COMMIT=`git rev-parse --short HEAD`
- TAG=`git describe --tags --exact-match 2> /dev/null || echo ''`
- |
if [[ "$MODE" == "OnPush" ]]
then
BRANCHTAG=`git describe --tags --exact-match 2> /dev/null || git symbolic-ref -q --short HEAD || git name-rev $(git rev-parse --short HEAD) | cut -d' ' -f2 || git rev-parse --short HEAD`
else
BRANCHTAG=$BRANCHNAME
fi
- COMMITDATE=`git --no-pager log -1 --pretty='format:%cd' --date='format:%Y-%m-%d %H:%M:%S'`
- BUILDDATE=$(TZ='America/Los_Angeles' date +"%Y%m%d")
- echo "BUILDDATE=$BUILDDATE"
- echo "Branchtag ${BRANCHTAG}; COMMITDATE=${COMMITDATE}; REPONAME=${REPONAME}; BRANCHNAME=${BRANCHNAME}"
- aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin ${ECR_REGISTRY}
- |
DEVBUCK=$(aws ssm get-parameter --name /uc3/mrt/config-bucket-dev --query Parameter.Value --output text)
aws s3 cp s3://${DEVBUCK}/uc3/mrt/mrt-ecs-manifest/ecs-release-manifest.yaml /tmp/ecs-release-manifest.yaml
TAG_ECS_DEV=$(yq -r ".\"ecs-tagmap\".\"${REPONAME}\".\"ecs-dev\"" /tmp/ecs-release-manifest.yaml)
echo TAG_ECS_DEV=$TAG_ECS_DEV
TAG_ECS_EPHEMERAL=$(yq -r ".\"ecs-tagmap\".\"${REPONAME}\".\"ecs-ephemeral\"" /tmp/ecs-release-manifest.yaml)
echo TAG_ECS_EPHEMERAL=$TAG_ECS_EPHEMERAL
TAG_ECS_DBSNAPSHOT=$(yq -r ".\"ecs-tagmap\".\"${REPONAME}\".\"ecs-dbsnapshot\"" /tmp/ecs-release-manifest.yaml)
echo TAG_ECS_DBSNAPSHOT=$TAG_ECS_DBSNAPSHOT
TAG_ECS_STG=$(yq -r ".\"ecs-tagmap\".\"${REPONAME}\".\"ecs-stg\"" /tmp/ecs-release-manifest.yaml)
echo TAG_ECS_STG=$TAG_ECS_STG
TAG_ECS_PRD=$(yq -r ".\"ecs-tagmap\".\"${REPONAME}\".\"ecs-prd\"" /tmp/ecs-release-manifest.yaml)
echo TAG_ECS_PRD=$TAG_ECS_PRD
build:
on-failure: CONTINUE
commands:
- |
CA_CERT_NAME=UC3-Self-Signed-CA.crt
aws ssm get-parameter --name /uc3/default/uc3_ca/$CA_CERT_NAME --output text --query 'Parameter.Value' > $CA_CERT_NAME
docker build --quiet --push \
--build-arg ECR_REGISTRY=${ECR_REGISTRY} \
-t ${ECR_REGISTRY}/${REPONAME}:${BRANCHTAG} .
docker build --quiet --push \
--build-arg ECR_REGISTRY=${ECR_REGISTRY} \
-t ${ECR_REGISTRY}/${REPONAME}:archive-${BRANCHTAG}-${BUILDDATE} .
if [[ "$BRANCHTAG" == "main" ]]
then
docker build --quiet --push \
--build-arg ECR_REGISTRY=${ECR_REGISTRY} \
-t ${ECR_REGISTRY}/${REPONAME}:dev .
fi
- |
SERVICENAME=admintool
if [[ "$BRANCHTAG" == "$TAG_ECS_DEV" ]]
then
docker build --quiet --push \
--build-arg ECR_REGISTRY=${ECR_REGISTRY} \
-t ${ECR_REGISTRY}/${REPONAME}:ecs-dev .
RUNNING_COUNT=$(aws ecs list-tasks \
--cluster mrt-ecs-dev-stack \
--service-name $SERVICENAME \
--desired-status RUNNING \
--query 'length(taskArns)' \
--output text)
if [ $RUNNING_COUNT -gt 0 ]
then
aws ecs update-service --cluster mrt-ecs-dev-stack --service $SERVICENAME \
--desired-count $RUNNING_COUNT --output text --no-cli-pager \
--query "service.deployments[0].[id,taskDefinition,status,desiredCount,createdAt]" \
--force-new-deployment || echo "Skip ecs-dev Deployment"
fi
fi
if [[ "$BRANCHTAG" == "$TAG_ECS_EPHEMERAL" ]]
then
docker build --quiet --push \
--build-arg ECR_REGISTRY=${ECR_REGISTRY} \
-t ${ECR_REGISTRY}/${REPONAME}:ecs-ephemeral .
RUNNING_COUNT=$(aws ecs list-tasks \
--cluster mrt-ecs-ephemeral-stack \
--service-name $SERVICENAME \
--desired-status RUNNING \
--query 'length(taskArns)' \
--output text)
if [ $RUNNING_COUNT -gt 0 ]
then
aws ecs update-service --cluster mrt-ecs-ephemeral-stack --service $SERVICENAME \
--desired-count $RUNNING_COUNT --output text --no-cli-pager \
--query "service.deployments[0].[id,taskDefinition,status,desiredCount,createdAt]" \
--force-new-deployment || echo "Skip ecs-ephemeral Deployment"
fi
fi
if [[ "$BRANCHTAG" == "$TAG_ECS_DBSNAPSHOT" ]]
then
docker build --quiet --push \
--build-arg ECR_REGISTRY=${ECR_REGISTRY} \
-t ${ECR_REGISTRY}/${REPONAME}:ecs-dbsnapshot .
RUNNING_COUNT=$(aws ecs list-tasks \
--cluster mrt-ecs-dbsnapshot-stack \
--service-name $SERVICENAME \
--desired-status RUNNING \
--query 'length(taskArns)' \
--output text)
if [ $RUNNING_COUNT -gt 0 ]
then
aws ecs update-service --cluster mrt-ecs-dbsnapshot-stack --service $SERVICENAME \
--desired-count $RUNNING_COUNT --output text --no-cli-pager \
--query "service.deployments[0].[id,taskDefinition,status,desiredCount,createdAt]" \
--force-new-deployment || echo "Skip ecs-dbsnapshot Deployment"
fi
fi
- |
if [[ "$BRANCHTAG" == "main" ]]
then
aws lambda update-function-code \
--function-name arn:aws:lambda:us-west-2:451826914157:function:uc3-mrt-admintool-img-dev \
--image-uri ${ECR_REGISTRY}/${REPONAME}:${BRANCHTAG} \
--output text --region us-west-2 \
--no-cli-pager || echo "Skip Lambda Deployment"
aws lambda update-function-code \
--function-name arn:aws:lambda:us-west-2:451826914157:function:uc3-mrt-admintool-img-stg \
--image-uri ${ECR_REGISTRY}/${REPONAME}:${BRANCHTAG} \
--output text --region us-west-2 \
--no-cli-pager || echo "Skip Lambda Deployment"
aws lambda update-function-code \
--function-name arn:aws:lambda:us-west-2:451826914157:function:uc3-mrt-admintool-img-prd \
--image-uri ${ECR_REGISTRY}/${REPONAME}:${BRANCHTAG} \
--output text --region us-west-2 \
--no-cli-pager || echo "Skip Lambda Deployment"
fi
- |
mkdir -p doc api/${REPONAME}/ruby
docker run --rm -v "$(pwd)/doc:/var/task/doc" --entrypoint /var/task/make_rdoc.sh ${ECR_REGISTRY}/${REPONAME}:ecs-dev
cp -r doc/* api/${REPONAME}/ruby/
aws s3 cp --recursive api s3://${S3CFBUCKET}/api
aws cloudfront create-invalidation --distribution-id ${CFDISTRIBUTIONID} --paths /api/${REPONAME}/* --region us-east-1
docker pull ${ECR_REGISTRY}/reveal-md --quiet
docker-compose -f present/deploy/docker-compose-static.yml up
aws s3 rm --recursive --only-show-errors s3://${S3CFBUCKET}/present/ecs-deploy|| echo 'delete skipped'
aws s3 cp --recursive --only-show-errors present/deploy/present_build s3://${S3CFBUCKET}/present/ecs-deploy
aws s3 cp --recursive --only-show-errors present/deploy/images s3://${S3CFBUCKET}/present/ecs-deploy/images
aws cloudfront create-invalidation --distribution-id ${CFDISTRIBUTIONID} --paths /present/ecs-deploy/* --region us-east-1
docker-compose -f present/tagging/docker-compose-static.yml up
mkdir -p present/tagging/present_build/mermaid/dist
curl "https://cdn.jsdelivr.net/npm/[email protected]/dist/mermaid.min.js" -o present/tagging/present_build/mermaid/dist/mermaid.min.js
aws s3 rm --recursive --only-show-errors s3://${S3CFBUCKET}/present/tagging || echo 'delete skipped'
aws s3 cp --recursive --only-show-errors present/tagging/present_build s3://${S3CFBUCKET}/present/tagging
aws s3 cp --recursive --only-show-errors present/tagging/images s3://${S3CFBUCKET}/present/tagging/images
aws cloudfront create-invalidation --distribution-id ${CFDISTRIBUTIONID} --paths /present/tagging/* --region us-east-1
- |
# daily / on-demand rebuild of deployed images
# note that this also applied to a main branch build
if [[ "$MODE" == "OnDemand" ]]
then
BUILT="$BRANCHTAG;"
for stack in ecs-dev ecs-ephemeral ecs-dbsnapshot ecs-stg ecs-prd
do
TAG_ECS=$(yq -r ".\"ecs-tagmap\".\"${REPONAME}\".\"$stack\"" /tmp/ecs-release-manifest.yaml)
if [[ "$BUILT" != *"$TAG_ECS;"* ]]
then
BUILT="$BUILT$TAG_ECS;"
git checkout $TAG_ECS
docker build --quiet --push \
--build-arg ECR_REGISTRY=${ECR_REGISTRY} \
-t ${ECR_REGISTRY}/${REPONAME}:${TAG_ECS} .
docker build --quiet --push \
--build-arg ECR_REGISTRY=${ECR_REGISTRY} \
-t ${ECR_REGISTRY}/${REPONAME}:archive-${TAG_ECS}-${BUILDDATE} .
fi
done
fi