From 508405533072049050cfbeb96bcaee35cecc6036 Mon Sep 17 00:00:00 2001 From: Lars Gohr Date: Wed, 21 Aug 2019 22:23:44 +0200 Subject: [PATCH] Add snapshot image push --- .dockerignore | 1 + Dockerfile | 2 +- README.md | 11 +++++++++++ entrypoint.sh | 14 +++++++++++--- test.sh | 27 +++++++++++++++++++++------ 5 files changed, 45 insertions(+), 10 deletions(-) create mode 100644 .dockerignore diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..6b8710a --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +.git diff --git a/Dockerfile b/Dockerfile index f5fbead..b02b610 100644 --- a/Dockerfile +++ b/Dockerfile @@ -18,7 +18,7 @@ FROM runtime as test ADD test.sh /test.sh ADD mock.sh /fake_bin/docker # Use mock instead of real docker -ENV PATH="/bin:/fake_bin" +ENV PATH="usr/bin:/bin:/fake_bin" RUN /test.sh FROM runtime diff --git a/README.md b/README.md index 125defe..543c080 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,17 @@ jobs: run: docker logout ``` +#### Optional Arguments + +Push an additional image, which is tagged with the git sha. +When you would like to think about versioning images, this might be useful. +```yaml +with: + args: myDocker/repository + snapshot: true +``` + + ### Old workflow ```hcl workflow "Publish Docker" { diff --git a/entrypoint.sh b/entrypoint.sh index 6c99106..1aba008 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -3,7 +3,7 @@ DOCKER_REPOSITORY=$* BRANCH=$(echo ${GITHUB_REF} | sed -e "s/refs\/heads\///g") -if [ "${BRANCH}" = "master" ]; then +if [ "${BRANCH}" == "master" ]; then BRANCH="latest" fi; @@ -14,5 +14,13 @@ fi; DOCKERNAME="${DOCKER_REPOSITORY}:${BRANCH}" -docker build -t ${DOCKERNAME} . -docker push ${DOCKERNAME} +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 push ${DOCKERNAME} + docker push ${SHA_DOCKER_NAME} +else + docker build -t ${DOCKERNAME} . + docker push ${DOCKERNAME} +fi diff --git a/test.sh b/test.sh index 93f616e..19bc173 100755 --- a/test.sh +++ b/test.sh @@ -2,8 +2,8 @@ function itPushesMasterBranchToLatest() { export GITHUB_REF='refs/heads/master' - result=$(exec /entrypoint.sh 'my/repository') - expected="Called mock with: build -t my/repository:latest . + local result=$(exec /entrypoint.sh 'my/repository') + local expected="Called mock with: build -t my/repository:latest . Called mock with: push my/repository:latest" if [ "$result" != "$expected" ]; then echo "Expected: $expected @@ -14,8 +14,8 @@ Called mock with: push my/repository:latest" function itPushesBranchAsNameOfTheBranch() { export GITHUB_REF='refs/heads/myBranch' - result=$(exec /entrypoint.sh 'my/repository') - expected="Called mock with: build -t my/repository:myBranch . + local result=$(exec /entrypoint.sh 'my/repository') + local expected="Called mock with: build -t my/repository:myBranch . Called mock with: push my/repository:myBranch" if [ "$result" != "$expected" ]; then echo "Expected: $expected @@ -26,8 +26,8 @@ Called mock with: push my/repository:myBranch" function itPushesReleasesToLatest() { export GITHUB_REF='refs/tags/myRelease' - result=$(exec /entrypoint.sh 'my/repository') - expected="Called mock with: build -t my/repository:latest . + local result=$(exec /entrypoint.sh 'my/repository') + local expected="Called mock with: build -t my/repository:latest . Called mock with: push my/repository:latest" if [ "$result" != "$expected" ]; then echo "Expected: $expected @@ -36,6 +36,21 @@ Called mock with: push my/repository:latest" fi } +function itPushesBranchByShaInAddition() { + export GITHUB_REF='refs/tags/myRelease' + export INPUT_snapshot='true' + local result=$(exec env 'github.sha'=COMMIT_SHA /entrypoint.sh 'my/repository') + local expected="Called mock with: build -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 +} + itPushesMasterBranchToLatest itPushesBranchAsNameOfTheBranch itPushesReleasesToLatest +itPushesBranchByShaInAddition