From 081fbdad65d25e3428dc0a85c7789d447fdb3b98 Mon Sep 17 00:00:00 2001 From: Lars Gohr Date: Tue, 1 Oct 2019 22:24:13 +0200 Subject: [PATCH] Restructure entrypoint to group tagging in a function to make it more clear. Use custom tag, if configured in the name --- Dockerfile | 2 -- entrypoint.sh | 36 +++++++++++++++++++++--------------- test.bats | 14 ++++++++++++++ 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/Dockerfile b/Dockerfile index 6fb25b5..95dde7a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/entrypoint.sh b/entrypoint.sh index 7adb146..22fabeb 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -16,20 +16,27 @@ if [ -z "${INPUT_PASSWORD}" ]; then exit 1 fi -BRANCH=$(echo ${GITHUB_REF} | sed -e "s/refs\/heads\///g" | sed -e "s/\//-/g") -if [ "${BRANCH}" = "master" ]; then - BRANCH="latest" -fi; +function translateTag() { + BRANCH=$(echo ${GITHUB_REF} | sed -e "s/refs\/heads\///g" | sed -e "s/\//-/g") + # 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 + 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 -if [ $(echo ${GITHUB_REF} | sed -e "s/refs\/tags\///g") != ${GITHUB_REF} ]; then - 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; +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 diff --git a/test.bats b/test.bats index 1f167e6..b3e1e13 100755 --- a/test.bats +++ b/test.bats @@ -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