forked from RafikFarhad/push-to-gcr-github-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
executable file
·86 lines (63 loc) · 2.4 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
#!/bin/bash -
#title :entrypoint.sh
#description :This script build image with multiple option and push the image to Google Container Registry.
#author :RafikFarhad<rafikfarhad@gmail.com>
#date :20200703
#version :2.0.1
#usage :./entrypoint.sh
#notes :Required env values are: INPUT_GCLOUD_SERVICE_KEY,INPUT_REGISTRY,INPUT_PROJECT_ID,INPUT_IMAGE_NAME
# Optional env values are: INPUT_IMAGE_TAG,INPUT_DOCKERFILE,INPUT_TARGET,INPUT_CONTEXT,INPUT_BUILD_ARGS
#bash_version :5.0.17(1)-release
###################################################
function split_csv() {
IFS=','
csv_data=$1
local -n global_list_array=$2
for i in $csv_data; do
global_list_array+=($i)
done
unset IFS
}
ALL_IMAGE_TAG=()
echo "Authenticating docker to gcloud ..."
if ! echo $INPUT_GCLOUD_SERVICE_KEY | python -m base64 -d >/tmp/key.json 2>/dev/null; then
echo "Failed to decode gcloud_service_key -- did you forget to encode it using 'python -m base64 -e < yourkey.json'?"
exit 1
fi
if cat /tmp/key.json | docker login -u _json_key --password-stdin https://$INPUT_REGISTRY; then
echo "Logged in to google cloud ..."
else
echo "Docker login failed. Exiting ..."
exit 1
fi
split_csv $INPUT_IMAGE_TAG ALL_IMAGE_TAG
TEMP_IMAGE_NAME="$INPUT_IMAGE_NAME:temporary"
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
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."