Add a way to change the Docker build context

This commit is contained in:
Lars Gohr
2019-11-29 14:08:47 +01:00
parent 6b75d10322
commit 2b131c1355
3 changed files with 50 additions and 2 deletions

View File

@@ -89,6 +89,17 @@ with:
workdir: mySubDirectory 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 ### 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. 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. All `buildargs` will be masked, so that they don't appear in the logs.

View File

@@ -23,6 +23,7 @@ function main() {
echo ${INPUT_PASSWORD} | docker login -u ${INPUT_USERNAME} --password-stdin ${INPUT_REGISTRY} echo ${INPUT_PASSWORD} | docker login -u ${INPUT_USERNAME} --password-stdin ${INPUT_REGISTRY}
BUILDPARAMS="" BUILDPARAMS=""
CONTEXT="."
if uses "${INPUT_DOCKERFILE}"; then if uses "${INPUT_DOCKERFILE}"; then
useCustomDockerfile useCustomDockerfile
@@ -30,6 +31,9 @@ function main() {
if uses "${INPUT_BUILDARGS}"; then if uses "${INPUT_BUILDARGS}"; then
addBuildArgs addBuildArgs
fi fi
if uses "${INPUT_CONTEXT}"; then
CONTEXT="${INPUT_CONTEXT}"
fi
if usesBoolean "${INPUT_CACHE}"; then if usesBoolean "${INPUT_CACHE}"; then
useBuildCache useBuildCache
fi fi
@@ -123,14 +127,14 @@ function pushWithSnapshot() {
local SHORT_SHA=$(echo "${GITHUB_SHA}" | cut -c1-6) local SHORT_SHA=$(echo "${GITHUB_SHA}" | cut -c1-6)
local SNAPSHOT_TAG="${TIMESTAMP}${SHORT_SHA}" local SNAPSHOT_TAG="${TIMESTAMP}${SHORT_SHA}"
local SHA_DOCKER_NAME="${INPUT_NAME}:${SNAPSHOT_TAG}" 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 ${DOCKERNAME}
docker push ${SHA_DOCKER_NAME} docker push ${SHA_DOCKER_NAME}
echo ::set-output name=snapshot-tag::"${SNAPSHOT_TAG}" echo ::set-output name=snapshot-tag::"${SNAPSHOT_TAG}"
} }
function pushWithoutSnapshot() { function pushWithoutSnapshot() {
docker build $BUILDPARAMS -t ${DOCKERNAME} . docker build $BUILDPARAMS -t ${DOCKERNAME} ${CONTEXT}
docker push ${DOCKERNAME} docker push ${DOCKERNAME}
} }

View File

@@ -412,6 +412,39 @@ teardown() {
[ "$status" -eq 2 ] [ "$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() { function expectStdOut() {
echo "Expected: |$1| echo "Expected: |$1|
Got: |$output|" Got: |$output|"