-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentrypoint.sh
202 lines (172 loc) · 7.3 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/bin/sh
export TAGS=""
export REPOSITORY="$INPUT_REPOSITORY"
export BUILD_PUSH="push"
export STRAIGHT=""
export VERSION=""
export SOURCE="$GITHUB_JOB"
export ARGS=""
export FILTER=""
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Parse inputs
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
if [ -n "$INPUT_FILTER" ]; then
IFS="
"
for arg in $INPUT_FILTER; do
if [ -n "$FILTER" ]; then
FILTER="$FILTER,$arg"
else
FILTER="$arg"
fi
done
unset IFS
fi
if [ -n "$INPUT_ROOTVERSION" ]; then
VERSION="$INPUT_ROOTVERSION"
fi
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Verify Dockerfile
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# If any repository is given, and if the Dockerfile has a default repository defined, then ensure they are equal.
if [ -n "$REPOSITORY" ]; then
if grep -q "ARG REPOSITORY=" "$INPUT_PATH/$INPUT_DOCKERFILE"; then
if ! grep -q "ARG REPOSITORY=$REPOSITORY" "$INPUT_PATH/$INPUT_DOCKERFILE"; then
if ! grep -q "ARG REPOSITORY='$REPOSITORY'" "$INPUT_PATH/$INPUT_DOCKERFILE"; then
if ! grep -q "ARG REPOSITORY="'"'"$REPOSITORY"'"' "$INPUT_PATH/$INPUT_DOCKERFILE"; then
echo "::error:: the Dockerfile '$INPUT_PATH/$INPUT_DOCKERFILE' contains a different ARG REPOSITORY
than given in the action config"
exit 6
fi
fi
fi
fi
fi
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Prepare Dockerfile
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
if ! grep -q "ARG REPOSITORY" "$INPUT_PATH/$INPUT_DOCKERFILE"; then
echo "ARG REPOSITORY='$REPOSITORY'" >> "$INPUT_PATH/$INPUT_DOCKERFILE"
fi
if ! grep -q "ARG VERSION" "$INPUT_PATH/$INPUT_DOCKERFILE"; then
echo "ARG VERSION='$VERSION'" >> "$INPUT_PATH/$INPUT_DOCKERFILE"
fi
if ! grep -q "LABEL fun.gofunky.tuplip.repository" "$INPUT_PATH/$INPUT_DOCKERFILE"; then
# use Dockerfile-internal $REPOSITORY
# shellcheck disable=SC2016
echo 'LABEL fun.gofunky.tuplip.repository="$REPOSITORY"' >> "$INPUT_PATH/$INPUT_DOCKERFILE"
fi
if ! grep -q "LABEL fun.gofunky.tuplip.version" "$INPUT_PATH/$INPUT_DOCKERFILE"; then
# use Dockerfile-internal $VERSION
# shellcheck disable=SC2016
echo 'LABEL fun.gofunky.tuplip.version="$VERSION"' >> "$INPUT_PATH/$INPUT_DOCKERFILE"
fi
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Login to Docker repository if necessary
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
if [ -n "$INPUT_BUILDONLY" ]; then
BUILD_PUSH="tag"
else
echo "Logging into Docker registry..."
if [ -n "$INPUT_USERNAME" ]; then
if [ -z "$INPUT_PASSWORD" ]; then
echo "::error:: input 'password' is not set even though 'username' is"
exit 22
fi
echo "$INPUT_PASSWORD" | docker login -u "$INPUT_USERNAME" --password-stdin
else
# Relogin will succeed if the user authenticated before this action
if ! docker login | grep -iq 'succeeded';
then
echo "::error:: docker was not logged in and no credentials were provided"
exit 13
fi
fi
fi
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Build docker image
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
if [ -n "$INPUT_BUILDSCRIPT" ]; then
if [ -z "$INPUT_SOURCETAG" ]; then
echo "::error:: input 'sourceTag' is not set even though 'buildScript' is"
exit 22
fi
echo "Executing given build script..."
sh "$INPUT_BUILDSCRIPT"
fi
if [ -n "$INPUT_SOURCETAG" ]; then
SOURCE="$INPUT_SOURCETAG"
if [ -z "$INPUT_BUILDSCRIPT" ]; then
echo "Skipping docker build..."
fi
else
if [ -n "$INPUT_BUILDARGS" ]; then
IFS="
"
for arg in $INPUT_BUILDARGS; do
ARGS="$ARGS --build-arg $arg"
done
unset IFS
fi
echo "Executing docker build..."
docker build -t "$SOURCE" -f "$INPUT_PATH/$INPUT_DOCKERFILE" ${VERSION:+--build-arg VERSION} \
${REPOSITORY:+--build-arg REPOSITORY} $ARGS "$INPUT_PATH"
fi
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Inspect docker image
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
if [ -z "$REPOSITORY" ]; then
if ! REPOSITORY=$(docker inspect --format '{{ index .Config.Labels "fun.gofunky.tuplip.repository" }}' "$SOURCE");
then
echo "::error:: target repository was neither given nor detected in the docker image"
exit 61
fi
fi
if [ -z "$VERSION" ]; then
if ! VERSION=$(docker inspect --format '{{ index .Config.Labels "fun.gofunky.tuplip.version" }}' "$SOURCE");
then
echo "::error:: root version was neither given nor detected in the docker image"
exit 61
fi
fi
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Executing tuplip
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
echo "Executing tuplip $BUILD_PUSH..."
if [ -n "$STRAIGHT" ]; then
TAGS=$( \
echo "$STRAIGHT" | tuplip $BUILD_PUSH "$SOURCE" ${REPOSITORY:+to "$REPOSITORY"} from stdin \
--verbose --straight --separator="," ${INPUT_CHECKSEMVER:+--check-semver} \
${INPUT_ADDLATEST:+--add-latest} ${INPUT_EXCLUSIVELATEST:+--exclusive-latest} \
)
else
TAGS=$( \
tuplip $BUILD_PUSH "$SOURCE" ${REPOSITORY:+to "$REPOSITORY"} from file "$INPUT_PATH/$INPUT_DOCKERFILE" \
--verbose ${INPUT_EXCLUDEMAJOR:+--exclude-major} ${INPUT_EXCLUDEMINOR:+--exclude-minor} \
${INPUT_EXCLUDEBASE:+--exclude-base} ${INPUT_ADDLATEST:+--add-latest} ${INPUT_EXCLUSIVELATEST:+--exclusive-latest} \
${INPUT_CHECKSEMVER:+--check-semver} ${VERSION:+--root-version "$VERSION"} ${FILTER:+--filter "$FILTER"} \
)
fi
STATUS="$?"
# Workaround until actions/toolkit#403 is resolved
# shellcheck disable=SC2039
TAGS="${TAGS//'%'/'%25'}"
# shellcheck disable=SC2039
TAGS="${TAGS//$'\n'/'%0A'}"
# shellcheck disable=SC2039
TAGS="${TAGS//$'\r'/'%0D'}"
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Logout of Docker repository
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# This is supposed to prevent accidental caching of a Docker image with a valid login
echo "Logging out of Docker registry..."
docker logout
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Check status and store outputs
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
if [ "$STATUS" -eq 0 ]; then
echo "::set-output name=tags::$TAGS"
else
echo "::error:: tuplip command did not succeed"
exit 1
fi