From eb9cfa9de01538dcf12ca6590497afa51b781dfb Mon Sep 17 00:00:00 2001 From: Lars Gohr Date: Wed, 21 Aug 2019 22:41:46 +0200 Subject: [PATCH] Add option to specify a Dockerfile --- README.md | 9 +++++++++ entrypoint.sh | 10 ++++++++-- test.sh | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1f1387f..c6a1bfb 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,15 @@ with: snapshot: true ``` +Use `dockerfile` when you would like to explicitly build a Dockerfile. +This might be useful when you have multiple DockerImages. + +```yaml +with: + args: myDocker/repository + dockerfile: MyDockerFileName +``` + ### Old workflow ```hcl diff --git a/entrypoint.sh b/entrypoint.sh index 1aba008..2800b47 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -13,14 +13,20 @@ if [ $(echo ${GITHUB_REF} | sed -e "s/refs\/tags\///g") != ${GITHUB_REF} ]; then fi; DOCKERNAME="${DOCKER_REPOSITORY}:${BRANCH}" +CUSTOMDOCKERFILE="" + +if [ ! -z "${INPUT_dockerfile}" ]; then + CUSTOMDOCKERFILE="-f ${INPUT_dockerfile}" +fi + if [ "${INPUT_snapshot}" == "true" ]; then SHA=$(env | grep ^github\\.sha= | cut -d= -f2-) # Thank you Github for using dots in variables SHA_DOCKER_NAME="${DOCKER_REPOSITORY}:${SHA}" - docker build -t ${DOCKERNAME} -t ${SHA_DOCKER_NAME} . + docker build $CUSTOMDOCKERFILE -t ${DOCKERNAME} -t ${SHA_DOCKER_NAME} . docker push ${DOCKERNAME} docker push ${SHA_DOCKER_NAME} else - docker build -t ${DOCKERNAME} . + docker build $CUSTOMDOCKERFILE -t ${DOCKERNAME} . docker push ${DOCKERNAME} fi diff --git a/test.sh b/test.sh index 19bc173..342b1e4 100755 --- a/test.sh +++ b/test.sh @@ -1,5 +1,10 @@ #!/bin/sh -e +function cleanEnvironment() { + unset INPUT_snapshot + unset INPUT_dockerfile +} + function itPushesMasterBranchToLatest() { export GITHUB_REF='refs/heads/master' local result=$(exec /entrypoint.sh 'my/repository') @@ -36,6 +41,20 @@ Called mock with: push my/repository:latest" fi } +function itPushesSpecificDockerfileReleasesToLatest() { + export GITHUB_REF='refs/tags/myRelease' + export INPUT_dockerfile='MyDockerFileName' + local result=$(exec /entrypoint.sh 'my/repository') + local expected="Called mock with: build -f MyDockerFileName -t my/repository:latest . +Called mock with: push my/repository:latest" + if [ "$result" != "$expected" ]; then + echo "Expected: $expected + Got: $result" + exit 1 + fi + cleanEnvironment +} + function itPushesBranchByShaInAddition() { export GITHUB_REF='refs/tags/myRelease' export INPUT_snapshot='true' @@ -48,9 +67,28 @@ Called mock with: push my/repository:COMMIT_SHA" Got: $result" exit 1 fi + cleanEnvironment +} + +function itPushesBranchByShaInAdditionWithSpecificDockerfile() { + export GITHUB_REF='refs/tags/myRelease' + export INPUT_snapshot='true' + export INPUT_dockerfile='MyDockerFileName' + local result=$(exec env 'github.sha'=COMMIT_SHA /entrypoint.sh 'my/repository') + local expected="Called mock with: build -f MyDockerFileName -t my/repository:latest -t my/repository:COMMIT_SHA . +Called mock with: push my/repository:latest +Called mock with: push my/repository:COMMIT_SHA" + if [ "$result" != "$expected" ]; then + echo "Expected: $expected + Got: $result" + exit 1 + fi + cleanEnvironment } itPushesMasterBranchToLatest itPushesBranchAsNameOfTheBranch itPushesReleasesToLatest +itPushesSpecificDockerfileReleasesToLatest itPushesBranchByShaInAddition +itPushesBranchByShaInAdditionWithSpecificDockerfile