diff --git a/Dockerfile b/Dockerfile index 83d3f3d..b2f4c86 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,9 +16,10 @@ ENTRYPOINT ["/entrypoint.sh"] FROM runtime as test ADD test.sh /test.sh -ADD mock.sh /fake_bin/docker +ADD stub.sh /fake_bin/docker +ADD mock.sh /fake_bin/date # Use mock instead of real docker -ENV PATH="usr/bin:/bin:/fake_bin" +ENV PATH="/fake_bin:usr/bin:/bin" RUN /test.sh FROM runtime diff --git a/README.md b/README.md index ec05b39..ad5b83a 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,8 @@ with: registry: "docker.pkg.github.com" ``` -Use `snapshot` to push an additional image, which is tagged with the git sha. +Use `snapshot` to push an additional image, which is tagged with {YEAR}{MONTH}{DAY}{HOUR}{MINUTE}{SECOND}{first 6 digits of the git sha}. +The date was inserted to prevent new builds with external dependencies override older builds with the same sha. When you would like to think about versioning images, this might be useful. ```yaml diff --git a/entrypoint.sh b/entrypoint.sh index 4d18355..4a1f00b 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -36,7 +36,9 @@ fi echo ${INPUT_PASSWORD} | docker login -u ${INPUT_USERNAME} --password-stdin ${INPUT_REGISTRY} if [ "${INPUT_SNAPSHOT}" == "true" ]; then - SHA_DOCKER_NAME="${INPUT_NAME}:${GITHUB_SHA}" + timestamp=`date +%Y%m%d%H%M%S` + shortSha=$(echo "${GITHUB_SHA}" | cut -c1-6) + SHA_DOCKER_NAME="${INPUT_NAME}:${timestamp}${shortSha}" docker build $CUSTOMDOCKERFILE -t ${DOCKERNAME} -t ${SHA_DOCKER_NAME} . docker push ${DOCKERNAME} docker push ${SHA_DOCKER_NAME} diff --git a/mock.sh b/mock.sh index 36eb9b2..362256f 100755 --- a/mock.sh +++ b/mock.sh @@ -1,3 +1,5 @@ #!/bin/sh -echo "Called mock with: $@" +if [ ! -z "${MOCK_DATE}" ]; then + echo "${MOCK_DATE}" +fi exit 0 diff --git a/stub.sh b/stub.sh new file mode 100755 index 0000000..36eb9b2 --- /dev/null +++ b/stub.sh @@ -0,0 +1,3 @@ +#!/bin/sh +echo "Called mock with: $@" +exit 0 diff --git a/test.sh b/test.sh index 5440758..7b4db34 100755 --- a/test.sh +++ b/test.sh @@ -76,18 +76,19 @@ Called mock with: logout" cleanEnvironment } -function itPushesBranchByShaInAddition() { +function itPushesBranchByShaAndDateInAddition() { export GITHUB_REF='refs/tags/myRelease' export INPUT_SNAPSHOT='true' - export GITHUB_SHA='COMMIT_SHA' + export GITHUB_SHA='12169ed809255604e557a82617264e9c373faca7' + export MOCK_DATE='197001010101' export INPUT_USERNAME='USERNAME' export INPUT_PASSWORD='PASSWORD' export INPUT_NAME='my/repository' local result=$(exec /entrypoint.sh) local expected="Called mock with: login -u USERNAME --password-stdin -Called mock with: build -t my/repository:latest -t my/repository:COMMIT_SHA . +Called mock with: build -t my/repository:latest -t my/repository:19700101010112169e . Called mock with: push my/repository:latest -Called mock with: push my/repository:COMMIT_SHA +Called mock with: push my/repository:19700101010112169e Called mock with: logout" if [ "$result" != "$expected" ]; then echo "Expected: $expected @@ -97,19 +98,20 @@ Called mock with: logout" cleanEnvironment } -function itPushesBranchByShaInAdditionWithSpecificDockerfile() { +function itPushesBranchByShaAndDateInAdditionWithSpecificDockerfile() { export GITHUB_REF='refs/tags/myRelease' export INPUT_SNAPSHOT='true' export INPUT_DOCKERFILE='MyDockerFileName' - export GITHUB_SHA='COMMIT_SHA' + export GITHUB_SHA='12169ed809255604e557a82617264e9c373faca7' + export MOCK_DATE='197001010101' export INPUT_USERNAME='USERNAME' export INPUT_PASSWORD='PASSWORD' export INPUT_NAME='my/repository' local result=$(exec /entrypoint.sh) local expected="Called mock with: login -u USERNAME --password-stdin -Called mock with: build -f MyDockerFileName -t my/repository:latest -t my/repository:COMMIT_SHA . +Called mock with: build -f MyDockerFileName -t my/repository:latest -t my/repository:19700101010112169e . Called mock with: push my/repository:latest -Called mock with: push my/repository:COMMIT_SHA +Called mock with: push my/repository:19700101010112169e Called mock with: logout" if [ "$result" != "$expected" ]; then echo "Expected: $expected @@ -178,8 +180,8 @@ itPushesMasterBranchToLatest itPushesBranchAsNameOfTheBranch itPushesReleasesToLatest itPushesSpecificDockerfileReleasesToLatest -itPushesBranchByShaInAddition -itPushesBranchByShaInAdditionWithSpecificDockerfile +itPushesBranchByShaAndDateInAddition +itPushesBranchByShaAndDateInAdditionWithSpecificDockerfile itLogsIntoAnotherRegistryIfConfigured itErrorsWhenNameWasNotSet itErrorsWhenUsernameWasNotSet