Skip to content

Commit

Permalink
Prefix the name with the custom docker registry if not already present.
Browse files Browse the repository at this point in the history
Fixes #33
  • Loading branch information
Lars Gohr committed Oct 9, 2019
1 parent 2227c0c commit 1133a36
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
20 changes: 12 additions & 8 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"

Expand Down Expand Up @@ -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
Expand All @@ -63,19 +71,19 @@ function translateDockerTag() {
}

function hasCustomTag() {
[ $(echo ${INPUT_NAME} | sed -e "s/://g") != ${INPUT_NAME} ]
[ $(echo "${INPUT_NAME}" | sed -e "s/://g") != "${INPUT_NAME}" ]
}

function isOnMaster() {
[ "${BRANCH}" = "master" ]
}

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() {
Expand All @@ -100,11 +108,7 @@ function useBuildCache() {
}

function uses() {
if [ ! -z "${1}" ]; then
return 0
else
return 1
fi
[ ! -z "${1}" ]
}

function pushWithSnapshot() {
Expand Down
26 changes: 21 additions & 5 deletions test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1133a36

Please sign in to comment.