-
Notifications
You must be signed in to change notification settings - Fork 0
/
02_build_ecs.sh
72 lines (55 loc) · 1.72 KB
/
02_build_ecs.sh
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
#!/bin/bash
set -eu
cd $(dirname $0) && cd ../../
trap 'echo Error Occurred!!! Exit...' ERR
PROFILE=""
APPNAME="Bastion"
REGION="ap-northeast-1"
while getopts p: OPT; do
case $OPT in
p)
PROFILE="$OPTARG"
;;
esac
done
function buildECS {
local profileOption=""
if [ -n "${1:-}" ]; then
profileOption="--profile ${1}"
fi
#### docker build & push
local repositoryName=$(echo "${APPNAME}-ECR" | tr '[:upper:]' '[:lower:]')
local accountId=$(aws sts get-caller-identity --query "Account" --output text ${profileOption})
local repositoryEnddpoint="${accountId}.dkr.ecr.ap-northeast-1.amazonaws.com"
local repositoryUri="${repositoryEnddpoint}/${repositoryName}"
local ecrTag="$(git rev-parse HEAD)"
local ecrTagPrevious=$(aws ecr describe-images --repository-name ${repositoryName} \
--query "reverse(sort_by(imageDetails[*], &imagePushedAt))[0].imageTags[0]" \
${profileOption} |
sed -e 's/"//g')
docker build \
--cache-from ${ecrTagPrevious} \
--build-arg BUILDKIT_INLINE_CACHE=1 \
-t ${repositoryName} \
.
docker tag ${repositoryName}:latest ${repositoryUri}:${ecrTag}
### Dockle
local dockleVersion=$(
curl --silent "https://api.github.com/repos/goodwithtech/dockle/releases/latest" |
grep '"tag_name":' |
sed -E 's/.*"v([^"]+)".*/\1/'
)
docker run \
--rm \
-v /var/run/docker.sock:/var/run/docker.sock \
-v $(pwd)/.dockleignore:/.dockleignore \
-e AWS_DEFAULT_REGION=${REGION} \
goodwithtech/dockle:v${dockleVersion} \
--exit-code 1 \
--exit-level "FATAL" \
${repositoryUri}:${ecrTag}
aws ecr get-login-password --region ${REGION} ${profileOption} |
docker login --username AWS --password-stdin ${repositoryEnddpoint}
docker push ${repositoryUri}:${ecrTag}
}
buildECS "${PROFILE:-}"