This repository was archived by the owner on May 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathentrypoint.sh
More file actions
153 lines (113 loc) · 4.32 KB
/
entrypoint.sh
File metadata and controls
153 lines (113 loc) · 4.32 KB
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
#!/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"
setInputRegistry
if uses "${INPUT_WORKDIR}"; then
changeWorkingDirectory
fi
echo ${INPUT_PASSWORD} | docker login -u ${INPUT_USERNAME} --password-stdin ${INPUT_REGISTRY}
BUILDPARAMS=""
CONTEXT="."
if uses "${INPUT_DOCKERFILE}"; then
useCustomDockerfile
fi
if uses "${INPUT_BUILDARGS}"; then
addBuildArgs
fi
if uses "${INPUT_CONTEXT}"; then
CONTEXT="${INPUT_CONTEXT}"
fi
if [ -z "${INPUT_SEMVER}" ]; then
INPUT_SEMVER="latest"
fi;
DOCKER_LATEST="${INPUT_NAME}:latest"
echo "::debug file=entrypoint.sh::Work directory: ${PWD}"
echo "::debug file=entrypoint.sh::Directories: $(ls)"
echo "::debug file=entrypoint.sh::Starting docker build $BUILDPARAMS -t ${DOCKER_LATEST} ${CONTEXT}"
docker build $BUILDPARAMS -t ${DOCKER_LATEST} ${CONTEXT}
echo "::debug file=entrypoint.sh::Finished building ${DOCKER_LATEST}"
echo "::debug file=entrypoint.sh::Starting docker push ${DOCKER_LATEST}"
docker push ${DOCKER_LATEST}
echo "::debug file=entrypoint.sh::Finished pushing ${DOCKER_LATEST}"
IMAGE_ID="$(docker images -q ${DOCKER_LATEST})"
echo "::debug file=entrypoint.sh::Image Id: ${IMAGE_ID}"
CONTAINER_ID="$(docker create ${IMAGE_ID})"
echo "::debug file=entrypoint.sh::Container Id: ${CONTAINER_ID}"
docker cp $CONTAINER_ID:VERSION ./version
docker rm $CONTAINER_ID
VERSION="$(cat version)"
echo "::debug file=entrypoint.sh::Version: $VERSION"
if [ "${VERSION}" ]; then
INPUT_SEMVER="${VERSION}"
echo "::debug: file=entrypoint.sh::Version overridden with: ${VERSION}"
fi;
if [ "${INPUT_SEMVER}" = "latest" ]; then
outputAndLogout
exit 0;
fi;
DOCKERNAME="${INPUT_NAME}:${INPUT_SEMVER}"
echo "::debug file=entrypoint.sh::Starting docker tag ${DOCKER_LATEST} ${DOCKERNAME}"
docker tag ${DOCKER_LATEST} ${DOCKERNAME}
echo "::debug file=entrypoint.sh::Finished tagging ${DOCKER_LATEST} ${DOCKERNAME}"
echo "::debug file=entrypoint.sh::Starting docker push ${DOCKERNAME}"
docker push ${DOCKERNAME}
echo "::debug file=entrypoint.sh::Finished pushing ${DOCKERNAME}"
MAJOR="$(echo ${INPUT_SEMVER} | cut -d'.' -f1)"
MINOR="$(echo ${INPUT_SEMVER} | cut -d'.' -f2)"
PATCH="$(echo ${INPUT_SEMVER} | cut -d'.' -f3)"
echo "::debug file=entrypoint.sh::Starting docker tag ${DOCKER_LATEST} ${INPUT_NAME}:${MAJOR}"
docker tag ${DOCKER_LATEST} ${INPUT_NAME}:${MAJOR}
echo "::debug file=entrypoint.sh::Finished tagging ${DOCKER_LATEST} ${INPUT_NAME}:${MAJOR}"
echo "::debug file=entrypoint.sh::Starting docker push ${INPUT_NAME}:${MAJOR}"
docker push ${INPUT_NAME}:${MAJOR}
echo "::debug file=entrypoint.sh::Finished pushing ${INPUT_NAME}:${MAJOR}"
echo "::debug file=entrypoint.sh::Starting docker tag ${DOCKER_LATEST} ${INPUT_NAME}:${MAJOR}.${MINOR}"
docker tag ${DOCKER_LATEST} ${INPUT_NAME}:${MAJOR}.${MINOR}
echo "::debug file=entrypoint.sh::Finished tagging ${DOCKER_LATEST} ${INPUT_NAME}:${MAJOR}.${MINOR}"
echo "::debug file=entrypoint.sh::Starting docker push ${INPUT_NAME}:${MAJOR}.${MINOR}"
docker push ${INPUT_NAME}:${MAJOR}.${MINOR}
echo "::debug file=entrypoint.sh::Finished pushing ${INPUT_NAME}:${MAJOR}.${MINOR}"
outputAndLogout
}
function sanitize() {
if [ -z "${1}" ]; then
>&2 echo "Unable to find the ${2}. Did you set with.${2}?"
exit 1
fi
}
function setInputRegistry() {
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
}
function isPartOfTheName() {
[ $(echo "${INPUT_NAME}" | sed -e "s/${1}//g") != "${INPUT_NAME}" ]
}
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 uses() {
[ ! -z "${1}" ]
}
function usesBoolean() {
[ ! -z "${1}" ] && [ "${1}" = "true" ]
}
function outputAndLogout() {
echo ::set-output name=tag::"${INPUT_SEMVER}"
docker logout
}
main