Restructure entrypoint to group tagging in a function to make it more clear. Use custom tag, if configured in the name

This commit is contained in:
Lars Gohr
2019-10-01 22:24:13 +02:00
parent 6b9504b15a
commit 081fbdad65
3 changed files with 35 additions and 17 deletions

View File

@@ -16,8 +16,6 @@ ENTRYPOINT ["/entrypoint.sh"]
FROM runtime as testEnv
RUN apk add --no-cache coreutils bats ncurses
FROM testEnv as test
ADD test.bats /test.bats
ADD stub.sh /usr/local/bin/docker
ADD mock.sh /usr/bin/date

View File

@@ -16,20 +16,27 @@ if [ -z "${INPUT_PASSWORD}" ]; then
exit 1
fi
function translateTag() {
BRANCH=$(echo ${GITHUB_REF} | sed -e "s/refs\/heads\///g" | sed -e "s/\//-/g")
if [ "${BRANCH}" = "master" ]; then
BRANCH="latest"
fi;
# if there is a tag inside the name already
if [ $(echo ${INPUT_NAME} | sed -e "s/://g") != ${INPUT_NAME} ]; then
TAG=$(echo ${INPUT_NAME} | cut -d':' -f2)
INPUT_NAME=$(echo ${INPUT_NAME} | cut -d':' -f1)
elif [ "${BRANCH}" = "master" ]; then
TAG="latest"
# if it's a tag
if [ $(echo ${GITHUB_REF} | sed -e "s/refs\/tags\///g") != ${GITHUB_REF} ]; then
BRANCH="latest"
fi;
elif [ $(echo ${GITHUB_REF} | sed -e "s/refs\/tags\///g") != ${GITHUB_REF} ]; then
TAG="latest"
# if it's a pull request
if [ $(echo ${GITHUB_REF} | sed -e "s/refs\/pull\///g") != ${GITHUB_REF} ]; then
BRANCH="${GITHUB_SHA}"
elif [ $(echo ${GITHUB_REF} | sed -e "s/refs\/pull\///g") != ${GITHUB_REF} ]; then
TAG="${GITHUB_SHA}"
else
TAG="${BRANCH}"
fi;
}
translateTag
DOCKERNAME="${INPUT_NAME}:${TAG}"
if [ ! -z "${INPUT_WORKDIR}" ]; then
cd "${INPUT_WORKDIR}"
@@ -37,7 +44,6 @@ fi
echo ${INPUT_PASSWORD} | docker login -u ${INPUT_USERNAME} --password-stdin ${INPUT_REGISTRY}
DOCKERNAME="${INPUT_NAME}:${BRANCH}"
BUILDPARAMS=""
if [ ! -z "${INPUT_DOCKERFILE}" ]; then
@@ -63,6 +69,6 @@ else
docker build $BUILDPARAMS -t ${DOCKERNAME} .
docker push ${DOCKERNAME}
fi
echo ::set-output name=tag::"${BRANCH}"
echo ::set-output name=tag::"${TAG}"
docker logout

View File

@@ -231,6 +231,20 @@ Called /usr/local/bin/docker logout"
[ "$output" = "$expected" ]
}
@test "it pushes to the tag if configured in the name" {
export INPUT_NAME='my/repository:custom-tag'
run /entrypoint.sh
local expected="Called /usr/local/bin/docker login -u USERNAME --password-stdin
Called /usr/local/bin/docker build -t my/repository:custom-tag .
Called /usr/local/bin/docker push my/repository:custom-tag
::set-output name=tag::custom-tag
Called /usr/local/bin/docker logout"
echo $output
[ "$output" = "$expected" ]
}
@test "it errors when with.name was not set" {
unset INPUT_NAME