From 2b131c135516a0c16463f18b882519df972ee7d5 Mon Sep 17 00:00:00 2001 From: Lars Gohr Date: Fri, 29 Nov 2019 14:08:47 +0100 Subject: [PATCH] Add a way to change the Docker build context --- README.md | 11 +++++++++++ entrypoint.sh | 8 ++++++-- test.bats | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index bc38581..f1596b4 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,17 @@ with: workdir: mySubDirectory ``` +### context +Use `context` when you would like to change the Docker build context. + +```yaml +with: + name: myDocker/repository + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + context: myContextDirectory +``` + ### buildargs Use `buildargs` when you want to pass a list of environment variables as [build-args](https://docs.docker.com/engine/reference/commandline/build/#set-build-time-variables---build-arg). Identifiers are separated by comma. All `buildargs` will be masked, so that they don't appear in the logs. diff --git a/entrypoint.sh b/entrypoint.sh index 1f26f5b..d90c39d 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -23,6 +23,7 @@ function main() { echo ${INPUT_PASSWORD} | docker login -u ${INPUT_USERNAME} --password-stdin ${INPUT_REGISTRY} BUILDPARAMS="" + CONTEXT="." if uses "${INPUT_DOCKERFILE}"; then useCustomDockerfile @@ -30,6 +31,9 @@ function main() { if uses "${INPUT_BUILDARGS}"; then addBuildArgs fi + if uses "${INPUT_CONTEXT}"; then + CONTEXT="${INPUT_CONTEXT}" + fi if usesBoolean "${INPUT_CACHE}"; then useBuildCache fi @@ -123,14 +127,14 @@ function pushWithSnapshot() { local SHORT_SHA=$(echo "${GITHUB_SHA}" | cut -c1-6) local SNAPSHOT_TAG="${TIMESTAMP}${SHORT_SHA}" local SHA_DOCKER_NAME="${INPUT_NAME}:${SNAPSHOT_TAG}" - docker build $BUILDPARAMS -t ${DOCKERNAME} -t ${SHA_DOCKER_NAME} . + docker build $BUILDPARAMS -t ${DOCKERNAME} -t ${SHA_DOCKER_NAME} ${CONTEXT} docker push ${DOCKERNAME} docker push ${SHA_DOCKER_NAME} echo ::set-output name=snapshot-tag::"${SNAPSHOT_TAG}" } function pushWithoutSnapshot() { - docker build $BUILDPARAMS -t ${DOCKERNAME} . + docker build $BUILDPARAMS -t ${DOCKERNAME} ${CONTEXT} docker push ${DOCKERNAME} } diff --git a/test.bats b/test.bats index 59b4316..1bca2b3 100755 --- a/test.bats +++ b/test.bats @@ -412,6 +412,39 @@ teardown() { [ "$status" -eq 2 ] } +@test "it can set a custom context" { + export GITHUB_REF='refs/heads/master' + export INPUT_CONTEXT='/myContextFolder' + + run /entrypoint.sh + + expectMockCalled "/usr/local/bin/docker login -u USERNAME --password-stdin +/usr/local/bin/docker build -t my/repository:latest /myContextFolder +/usr/local/bin/docker push my/repository:latest +/usr/local/bin/docker logout" +} + +@test "it can set a custom context when building snapshot" { + export GITHUB_REF='refs/heads/master' + export INPUT_CONTEXT='/myContextFolder' + export GITHUB_SHA='12169ed809255604e557a82617264e9c373faca7' + export INPUT_SNAPSHOT='true' + + declare -A -p MOCK_RETURNS=( + ['/usr/local/bin/docker']="" + ['/usr/bin/date']="197001010101" + ) > mockReturns + + run /entrypoint.sh + + expectMockCalled "/usr/local/bin/docker login -u USERNAME --password-stdin +/usr/bin/date +%Y%m%d%H%M%S +/usr/local/bin/docker build -t my/repository:latest -t my/repository:19700101010112169e /myContextFolder +/usr/local/bin/docker push my/repository:latest +/usr/local/bin/docker push my/repository:19700101010112169e +/usr/local/bin/docker logout" +} + function expectStdOut() { echo "Expected: |$1| Got: |$output|"