This repository has been archived by the owner on Oct 31, 2022. It is now read-only.
generated from actions/container-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
executable file
·135 lines (112 loc) · 3.12 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
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
#!/usr/bin/env bash -l
IMAGE="$INPUT_IMAGE"
ACCESS_TOKEN="$INPUT_ACCESSTOKEN"
REGISTRY="$INPUT_REGISTRY"
DOCKER_USERNAME="$INPUT_DOCKERHUBUSERNAME"
GCR_HOSTNAME="$INPUT_HOSTNAME"
# Parsing the image name
if [[ -z "$IMAGE" ]]; then
echo "You must provide the image input"
exit 1
fi
if [[ $IMAGE =~ ":" ]]; then
IFS=":" read -ra EL <<< "$IMAGE"
IMAGE_NAME=${EL[0]}
TAG=${EL[1]}
else
IMAGE_NAME=$IMAGE
TAG="latest"
fi
echo "IMAGE_NAME=$IMAGE_NAME"
echo "TAG=$TAG"
# Setting API base address
case "$REGISTRY" in
"docker-hub")
API_ADDRESS="https://registry.hub.docker.com/v2"
;;
"github-packages")
API_ADDRESS="https://docker.pkg.github.com/v2"
;;
"gcr")
if [[ -z "$GCR_HOSTNAME" ]]; then
echo "The input 'hostname' is required"
exit 1
fi
API_ADDRESS="${GCR_HOSTNAME}/v2"
;;
*)
echo "'$REGISTRY' is not a valid value for the input 'registry'"
exit 1
;;
esac
# Getting a token
case "$REGISTRY" in
"docker-hub")
ADDRESS="https://auth.docker.io/token?scope=repository:${IMAGE_NAME}:pull&service=registry.docker.io"
ARGS=()
if [[ ! -z "$DOCKER_USERNAME" ]]; then
if [[ -z "$ACCESS_TOKEN" ]]; then
echo "If you specify a Docker Hub username, you must also give an access token"
exit 1
fi
ADDRESS+="&account=$DOCKER_USERNAME"
ARGS+=('-u' "${DOCKER_USERNAME}:$ACCESS_TOKEN")
fi
TOKEN=$(curl -sL "${ARGS[@]}" "$ADDRESS" | jq -r '.token')
;;
*)
TOKEN=$ACCESS_TOKEN
;;
esac
if [[ -z "$TOKEN" || "$TOKEN" = "null" ]]; then
case "$REGISTRY" in
"github-packages")
echo "The 'token' input is required when 'registry' is '$REGISTRY'"
exit 1
;;
"docker-hub")
echo "Unable to get a token to call the API"
exit 1
;;
esac
fi
# Getting all the labels
set_headers () {
if [[ ! -z "$TOKEN" ]]; then
case "$REGISTRY" in
"gcr")
HEADERS=('-u' "_json_key:$TOKEN")
;;
*)
HEADERS=('-H' "Authorization: Bearer $TOKEN")
;;
esac
else
HEADERS=()
fi
}
ADDRESS="${API_ADDRESS}/${IMAGE_NAME}/manifests/$TAG"
set_headers
HEADERS+=('-H' "Accept: application/vnd.docker.distribution.manifest.v2+json")
DIGEST=$(curl -sL "${HEADERS[@]}" "$ADDRESS" | jq -r '.config.digest')
if [[ -z "$DIGEST" || "$DIGEST" = "null" ]]; then
MESSAGE="The image '$IMAGE' has not been found"
if [[ "$REGISTRY" = "gcr" ]]; then
MESSAGE+=" on $GCR_HOSTNAME"
fi
echo "$MESSAGE"
exit 1
fi
ADDRESS="${API_ADDRESS}/${IMAGE_NAME}/blobs/$DIGEST"
set_headers
LABELS=$(curl -sL "${HEADERS[@]}" "$ADDRESS" | jq -r '.config.Labels')
if [[ -z "$LABELS" || "$LABELS" = "null" ]]; then
echo "No label has been found"
exit 0
fi
# Setting outputs
for KEY in $(jq -r ". | keys[]" <<< $LABELS)
do
VALUE=$(jq -r ".[\"$KEY\"]" <<< $LABELS)
echo ::set-output name="$KEY"::"$VALUE"
done