mirror of
https://github.com/elgohr/Publish-Docker-Github-Action.git
synced 2026-03-12 18:07:12 -04:00
Add a way to change the Docker build context
This commit is contained in:
11
README.md
11
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.
|
||||
|
||||
@@ -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}
|
||||
}
|
||||
|
||||
|
||||
33
test.bats
33
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|"
|
||||
|
||||
Reference in New Issue
Block a user