forked from elgohr/Publish-Docker-Github-Action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
executable file
·158 lines (129 loc) · 4.19 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
#!/bin/sh
set -e
function main() {
echo "" # see https://github.com/actions/toolkit/issues/168
sanitize "${INPUT_NAME}" "name"
sanitize "${INPUT_USERNAME}" "username"
sanitize "${INPUT_PASSWORD}" "password"
REGISTRY_NO_PROTOCOL=$(echo "${INPUT_REGISTRY}" | sed -e 's/^https:\/\///g')
if uses "${INPUT_REGISTRY}" && ! isPartOfTheName "${REGISTRY_NO_PROTOCOL}"; then
INPUT_NAME="${REGISTRY_NO_PROTOCOL}/${INPUT_NAME}"
fi
translateDockerTag
DOCKERNAME="${INPUT_NAME}:${TAG}"
echo "Building and pushing ${DOCKERNAME}"
# check if we should do anything at all with this branch
if { [ -z ${PUSH_BRANCH_TO_DOCKERHUB} ] || [ "${PUSH_BRANCH_TO_DOCKERHUB}" = "false" ]; } && [ "${TAG}" != "develop" ] && [ "${TAG}" != "develop-1.0" ] && [ "${TAG}" != "develop-2.0" ] && [ "${TAG}" != "master" ] && ! isReleaseBranch && ! isGitTag ; then
echo "workflow environment PUSH_BRANCH_TO_DOCKERHUB is false or not set and this is no default branch -> stopping push gracefully -> no error"
exit 0;
fi
if uses "${INPUT_WORKDIR}"; then
changeWorkingDirectory
fi
echo ${INPUT_PASSWORD} | docker login -u ${INPUT_USERNAME} --password-stdin ${INPUT_REGISTRY}
BUILDPARAMS=""
if uses "${INPUT_DOCKERFILE}"; then
useCustomDockerfile
fi
if uses "${INPUT_BUILDARGS}"; then
addBuildArgs
fi
if uses "${INPUT_BUILDTARGET}"; then
useBuildTarget
fi
if usesBoolean "${INPUT_CACHE}"; then
useBuildCache
fi
if usesBoolean "${INPUT_SNAPSHOT}"; then
pushWithSnapshot
else
pushWithoutSnapshot
fi
echo "tag=${TAG}" >> $GITHUB_OUTPUT
docker logout
}
function sanitize() {
if [ -z "${1}" ]; then
>&2 echo "Unable to find the ${2}. Did you set with.${2}?"
exit 1
fi
}
function isPartOfTheName() {
[ $(echo "${INPUT_NAME}" | sed -e "s/${1}//g") != "${INPUT_NAME}" ]
}
function translateDockerTag() {
local BRANCH=$(echo ${GITHUB_REF} | sed -e "s/refs\/heads\///g" | sed -e "s/\//-/g" | sed -e 's/[^ -~]//g')
if hasCustomTag; then
TAG=$(echo ${INPUT_NAME} | cut -d':' -f2)
INPUT_NAME=$(echo ${INPUT_NAME} | cut -d':' -f1)
elif isOnMaster; then
TAG="latest"
elif isGitTag && usesBoolean "${INPUT_TAG_NAMES}"; then
TAG=$(echo ${GITHUB_REF} | sed -e "s/refs\/tags\///g")
elif isGitTag; then
TAG="latest"
elif isPullRequest; then
BRANCH=$(echo ${GITHUB_HEAD_REF} | sed -e "s/refs\/heads\///g" | sed -e "s/\//-/g" | sed -e 's/[^ -~]//g')
TAG="${BRANCH}"
elif isReleaseBranch; then
TAG=$(echo "${GITHUB_REF}-rc" | sed -e "s/refs\/heads\/release\///g")
else
TAG="${BRANCH}"
fi;
}
function hasCustomTag() {
[ $(echo "${INPUT_NAME}" | sed -e "s/://g") != "${INPUT_NAME}" ]
}
function isOnMaster() {
[ "${BRANCH}" = "master" ]
}
function isGitTag() {
[ $(echo "${GITHUB_REF}" | sed -e "s/refs\/tags\///g") != "${GITHUB_REF}" ]
}
function isPullRequest() {
[ $(echo "${GITHUB_REF}" | sed -e "s/refs\/pull\///g") != "${GITHUB_REF}" ]
}
function isReleaseBranch() {
[ $(echo "${GITHUB_REF}" | sed -e "s/refs\/heads\/release\///g") != "${GITHUB_REF}" ]
}
function changeWorkingDirectory() {
cd "${INPUT_WORKDIR}"
}
function useCustomDockerfile() {
BUILDPARAMS="$BUILDPARAMS -f ${INPUT_DOCKERFILE}"
}
function addBuildArgs() {
for arg in $(echo "${INPUT_BUILDARGS}" | tr ',' '\n'); do
BUILDPARAMS="$BUILDPARAMS --build-arg ${arg}"
echo "::add-mask::${arg}"
done
}
function useBuildCache() {
if docker pull ${DOCKERNAME} 2>/dev/null; then
BUILDPARAMS="$BUILDPARAMS --cache-from ${DOCKERNAME}"
fi
}
function useBuildTarget() {
BUILDPARAMS="$BUILDPARAMS --target=${INPUT_BUILDTARGET}"
}
function uses() {
[ ! -z "${1}" ]
}
function usesBoolean() {
[ ! -z "${1}" ] && [ "${1}" = "true" ]
}
function pushWithSnapshot() {
local TIMESTAMP=`date +%Y%m%d%H%M%S`
local SHORT_SHA=$(echo "${GITHUB_SHA}" | cut -c1-6)
local SNAPSHOT_TAG="${TIMESTAMP}${SHORT_SHA}"
local SHA_DOCKER_NAME="${INPUT_NAME}:${SNAPSHOT_TAG}"
docker build $BUILDPARAMS -t ${DOCKERNAME} -t ${SHA_DOCKER_NAME} .
docker push ${DOCKERNAME}
docker push ${SHA_DOCKER_NAME}
echo "snapshot-tag=${SNAPSHOT_TAG}" >> $GITHUB_OUTPUT
}
function pushWithoutSnapshot() {
docker build $BUILDPARAMS -t ${DOCKERNAME} .
docker push ${DOCKERNAME}
}
main