From 1133a364e46e0906081e8907c90c6959311e5a69 Mon Sep 17 00:00:00 2001 From: Lars Gohr Date: Wed, 9 Oct 2019 12:24:16 +0200 Subject: [PATCH] Prefix the name with the custom docker registry if not already present. Fixes https://github.com/elgohr/Publish-Docker-Github-Action/issues/33 --- entrypoint.sh | 20 ++++++++++++-------- test.bats | 26 +++++++++++++++++++++----- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 0cd1f19..1ba723b 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -8,6 +8,10 @@ function main() { sanitize "${INPUT_USERNAME}" "username" sanitize "${INPUT_PASSWORD}" "password" + if uses "${INPUT_REGISTRY}" && ! isPartOfTheName; then + INPUT_NAME="${INPUT_REGISTRY}/${INPUT_NAME}" + fi + translateDockerTag DOCKERNAME="${INPUT_NAME}:${TAG}" @@ -46,6 +50,10 @@ function sanitize() { fi } +function isPartOfTheName() { + [ $(echo "${INPUT_NAME}" | sed -e "s/${INPUT_REGISTRY}//g") != "${INPUT_NAME}" ] +} + function translateDockerTag() { local BRANCH=$(echo ${GITHUB_REF} | sed -e "s/refs\/heads\///g" | sed -e "s/\//-/g") if hasCustomTag; then @@ -63,7 +71,7 @@ function translateDockerTag() { } function hasCustomTag() { - [ $(echo ${INPUT_NAME} | sed -e "s/://g") != ${INPUT_NAME} ] + [ $(echo "${INPUT_NAME}" | sed -e "s/://g") != "${INPUT_NAME}" ] } function isOnMaster() { @@ -71,11 +79,11 @@ function isOnMaster() { } function isGitTag() { - [ $(echo ${GITHUB_REF} | sed -e "s/refs\/tags\///g") != ${GITHUB_REF} ] + [ $(echo "${GITHUB_REF}" | sed -e "s/refs\/tags\///g") != "${GITHUB_REF}" ] } function isPullRequest() { - [ $(echo ${GITHUB_REF} | sed -e "s/refs\/pull\///g") != ${GITHUB_REF} ] + [ $(echo "${GITHUB_REF}" | sed -e "s/refs\/pull\///g") != "${GITHUB_REF}" ] } function changeWorkingDirectory() { @@ -100,11 +108,7 @@ function useBuildCache() { } function uses() { - if [ ! -z "${1}" ]; then - return 0 - else - return 1 - fi + [ ! -z "${1}" ] } function pushWithSnapshot() { diff --git a/test.bats b/test.bats index d6f00c0..2c912c5 100755 --- a/test.bats +++ b/test.bats @@ -196,15 +196,31 @@ Called /usr/local/bin/docker logout" [ "$output" = "$expected" ] } -@test "it performs a login to another registry" { - export INPUT_REGISTRY='https://myRegistry' +@test "it pushes to another registry and adds the reference" { + export INPUT_REGISTRY='my.Registry.io' run /entrypoint.sh local expected=" -Called /usr/local/bin/docker login -u USERNAME --password-stdin https://myRegistry -Called /usr/local/bin/docker build -t my/repository:latest . -Called /usr/local/bin/docker push my/repository:latest +Called /usr/local/bin/docker login -u USERNAME --password-stdin my.Registry.io +Called /usr/local/bin/docker build -t my.Registry.io/my/repository:latest . +Called /usr/local/bin/docker push my.Registry.io/my/repository:latest +::set-output name=tag::latest +Called /usr/local/bin/docker logout" + echo $output + [ "$output" = "$expected" ] +} + +@test "it pushes to another registry and is ok when the reference is already present" { + export INPUT_REGISTRY='my.Registry.io' + export INPUT_NAME='my.Registry.io/my/repository' + + run /entrypoint.sh + + local expected=" +Called /usr/local/bin/docker login -u USERNAME --password-stdin my.Registry.io +Called /usr/local/bin/docker build -t my.Registry.io/my/repository:latest . +Called /usr/local/bin/docker push my.Registry.io/my/repository:latest ::set-output name=tag::latest Called /usr/local/bin/docker logout" echo $output