forked from kciter/aws-ecr-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
99 lines (86 loc) · 2.8 KB
/
entrypoint.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
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
#!/bin/bash
set -e
function main() {
sanitize "${INPUT_ACCESS_KEY_ID}" "access_key_id"
sanitize "${INPUT_SECRET_ACCESS_KEY}" "secret_access_key"
sanitize "${INPUT_REGION}" "region"
sanitize "${INPUT_ACCOUNT_ID}" "account_id"
sanitize "${INPUT_REPO}" "repo"
sanitize "${INPUT_EXTRA_BUILD_ARGS}" "extra_build_args"
ACCOUNT_URL="$INPUT_ACCOUNT_ID.dkr.ecr.$INPUT_REGION.amazonaws.com"
aws_configure
assume_role
login
run_pre_build_script $INPUT_PREBUILD_SCRIPT
docker_build $INPUT_TAGS $ACCOUNT_URL $INPUT_EXTRA_BUILD_ARGS
create_ecr_repo $INPUT_CREATE_REPO
docker_push_to_ecr $INPUT_TAGS $ACCOUNT_URL
}
function sanitize() {
if [ -z "${1}" ]; then
>&2 echo "Unable to find the ${2}. Did you set with.${2}?"
exit 1
fi
}
function aws_configure() {
export AWS_ACCESS_KEY_ID=$INPUT_ACCESS_KEY_ID
export AWS_SECRET_ACCESS_KEY=$INPUT_SECRET_ACCESS_KEY
export AWS_DEFAULT_REGION=$INPUT_REGION
}
function login() {
echo "== START LOGIN"
LOGIN_COMMAND=$(aws ecr get-login --no-include-email --region $AWS_DEFAULT_REGION)
$LOGIN_COMMAND
echo "== FINISHED LOGIN"
}
function assume_role() {
if [ "${INPUT_ASSUME_ROLE}" != "" ]; then
sanitize "${INPUT_ASSUME_ROLE}" "assume_role"
echo "== START ASSUME ROLE"
ROLE="arn:aws:iam::${INPUT_ACCOUNT_ID}:role/${INPUT_ASSUME_ROLE}"
CREDENTIALS=$(aws sts assume-role --role-arn ${ROLE} --role-session-name ecrpush --query 'Credentials.[AccessKeyId,SecretAccessKey,SessionToken]' --output text)
read id key token <<< ${CREDENTIALS}
export AWS_ACCESS_KEY_ID="${id}"
export AWS_SECRET_ACCESS_KEY="${key}"
export AWS_SESSION_TOKEN="${token}"
echo "== FINISHED ASSUME ROLE"
fi
}
function create_ecr_repo() {
if [ "${1}" = true ]; then
echo "== START CREATE REPO"
aws ecr describe-repositories --region $AWS_DEFAULT_REGION --repository-names $INPUT_REPO > /dev/null 2>&1 || \
aws ecr create-repository --region $AWS_DEFAULT_REGION --repository-name $INPUT_REPO
echo "== FINISHED CREATE REPO"
fi
}
function run_pre_build_script() {
if [ ! -z "${1}" ]; then
echo "== START PREBUILD SCRIPT"
chmod a+x $1
$1
echo "== FINISHED PREBUILD SCRIPT"
fi
}
function docker_build() {
echo "== START DOCKERIZE"
local TAG=$1
local docker_tag_args=""
local DOCKER_TAGS=$(echo "$TAG" | tr "," "\n")
for tag in $DOCKER_TAGS; do
docker_tag_args="$docker_tag_args -t $2/$INPUT_REPO:$tag"
done
docker build -f $INPUT_DOCKERFILE $INPUT_EXTRA_BUILD_ARGS $docker_tag_args $INPUT_PATH
echo "== FINISHED DOCKERIZE"
}
function docker_push_to_ecr() {
echo "== START PUSH TO ECR"
local TAG=$1
local DOCKER_TAGS=$(echo "$TAG" | tr "," "\n")
for tag in $DOCKER_TAGS; do
docker push $2/$INPUT_REPO:$tag
echo ::set-output name=image::$2/$INPUT_REPO:$tag
done
echo "== FINISHED PUSH TO ECR"
}
main