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 FROM runtime as testEnv
RUN apk add --no-cache coreutils bats ncurses RUN apk add --no-cache coreutils bats ncurses
FROM testEnv as test
ADD test.bats /test.bats ADD test.bats /test.bats
ADD stub.sh /usr/local/bin/docker ADD stub.sh /usr/local/bin/docker
ADD mock.sh /usr/bin/date ADD mock.sh /usr/bin/date

View File

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

View File

@@ -231,6 +231,20 @@ Called /usr/local/bin/docker logout"
[ "$output" = "$expected" ] [ "$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" { @test "it errors when with.name was not set" {
unset INPUT_NAME unset INPUT_NAME