-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathentrypoint.sh
executable file
·107 lines (88 loc) · 3.71 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
100
101
102
103
104
105
106
107
#!/bin/bash -
#title :entrypoint.sh
#description :This script build image with multiple option and push the image to Google Container Registry.
#author :RafikFarhad<[email protected]>
#date :20200703
#version :2.0.1
#usage :./entrypoint.sh
#notes :Required env values are: INPUT_REGISTRY,INPUT_PROJECT_ID,INPUT_IMAGE_NAME
# Optional env values are: INPUT_GCLOUD_SERVICE_KEY,INPUT_IMAGE_TAG,INPUT_DOCKERFILE,INPUT_TARGET,INPUT_CONTEXT,INPUT_BUILD_ARGS
#bash_version :5.0.17(1)-release
###################################################
ALL_IMAGE_TAG=()
# detect service_account json flavour
if [ $GOOGLE_APPLICATION_CREDENTIALS ] && ls $GOOGLE_APPLICATION_CREDENTIALS; then
# workload identity
echo "Workload identity found ..."
cp $GOOGLE_APPLICATION_CREDENTIALS /tmp/key.json
else
if [ -z $INPUT_GCLOUD_SERVICE_KEY ]; then
echo "GCLOUD_SERVICE_KEY is a required field when workload identity is not used. Exiting ..."
exit 1
fi
# persing service account json
if ! echo $INPUT_GCLOUD_SERVICE_KEY | python3 -m base64 -d >/tmp/key.json 2>/dev/null; then
if ! echo $INPUT_GCLOUD_SERVICE_KEY >/tmp/key.json 2>/dev/null; then
echo "Failed to get gcloud_service_key. It could be plain text or base64 encoded service account JSON file"
exit 1
else
# service account found as plain text json
echo "This action is unable to decode INPUT_GCLOUD_SERVICE_KEY as base64. It assumes INPUT_GCLOUD_SERVICE_KEY as plain text"
fi
else
# service account found as base64 encoded json
echo "Successfully decoded from base64"
fi
fi
if ! gcloud auth login --cred-file=/tmp/key.json --quiet; then
echo "Unable to login to gcloud. Exiting ..."
exit 1
fi
if gcloud auth configure-docker $INPUT_REGISTRY --quiet; then
echo "Authentication successful to $INPUT_REGISTRY ..."
else
echo "Docker login failed. Exiting ..."
exit 1
fi
# split -> trim -> compact -> uniq -> bash array
ALL_IMAGE_TAG=($(python3 -c "print(' '.join(list(set([v for v in [v.strip() for v in '$INPUT_IMAGE_TAG'.split(',')] if v]))))"))
# default to 'latest' when $ALL_IMAGE_TAG is empty
if [ ${#ALL_IMAGE_TAG[@]} -eq 0 ] ; then
echo "INPUT_IMAGE_TAG tag is not parsable. Using latest by default"
ALL_IMAGE_TAG=(latest)
fi
TEMP_IMAGE_NAME="$INPUT_IMAGE_NAME:temporary"
if [ "$INPUT_PUSH_ONLY" = true ]; then
echo "Skipping image build ..."
TEMP_IMAGE_NAME="$INPUT_REGISTRY/$INPUT_PROJECT_ID/$INPUT_IMAGE_NAME:$ALL_IMAGE_TAG[0]"
else
echo "Building image ..."
[ -z $INPUT_TARGET ] && TARGET_ARG="" || TARGET_ARG="--target $INPUT_TARGET"
[ -z $INPUT_DOCKERFILE ] && FILE_ARG="" || FILE_ARG="--file $INPUT_DOCKERFILE"
if [ ! -z "$INPUT_BUILD_ARGS" ]; then
for ARG in $(echo "$INPUT_BUILD_ARGS" | tr ',' '\n'); do
BUILD_PARAMS="$BUILD_PARAMS --build-arg ${ARG}"
done
fi
echo "docker build $BUILD_PARAMS $TARGET_ARG -t $TEMP_IMAGE_NAME $FILE_ARG $INPUT_CONTEXT"
if docker build $BUILD_PARAMS $TARGET_ARG -t $TEMP_IMAGE_NAME $FILE_ARG $INPUT_CONTEXT; then
echo "Image built ..."
else
echo "Image building failed. Exiting ..."
exit 1
fi
fi
for IMAGE_TAG in ${ALL_IMAGE_TAG[@]}; do
IMAGE_NAME="$INPUT_REGISTRY/$INPUT_PROJECT_ID/$INPUT_IMAGE_NAME:$IMAGE_TAG"
echo "Fully qualified image name: $IMAGE_NAME"
echo "Creating docker tag ..."
docker tag $TEMP_IMAGE_NAME $IMAGE_NAME
echo "Pushing image $IMAGE_NAME ..."
if ! docker push $IMAGE_NAME; then
echo "Pushing failed. Exiting ..."
exit 1
else
echo "Image pushed."
fi
done
echo "Process complete."