Add option to specify a Dockerfile

This commit is contained in:
Lars Gohr
2019-08-21 22:41:46 +02:00
parent 2c7a6fb102
commit eb9cfa9de0
3 changed files with 55 additions and 2 deletions

View File

@@ -36,6 +36,15 @@ with:
snapshot: true snapshot: true
``` ```
Use `dockerfile` when you would like to explicitly build a Dockerfile.
This might be useful when you have multiple DockerImages.
```yaml
with:
args: myDocker/repository
dockerfile: MyDockerFileName
```
### Old workflow ### Old workflow
```hcl ```hcl

View File

@@ -13,14 +13,20 @@ if [ $(echo ${GITHUB_REF} | sed -e "s/refs\/tags\///g") != ${GITHUB_REF} ]; then
fi; fi;
DOCKERNAME="${DOCKER_REPOSITORY}:${BRANCH}" DOCKERNAME="${DOCKER_REPOSITORY}:${BRANCH}"
CUSTOMDOCKERFILE=""
if [ ! -z "${INPUT_dockerfile}" ]; then
CUSTOMDOCKERFILE="-f ${INPUT_dockerfile}"
fi
if [ "${INPUT_snapshot}" == "true" ]; then if [ "${INPUT_snapshot}" == "true" ]; then
SHA=$(env | grep ^github\\.sha= | cut -d= -f2-) # Thank you Github for using dots in variables SHA=$(env | grep ^github\\.sha= | cut -d= -f2-) # Thank you Github for using dots in variables
SHA_DOCKER_NAME="${DOCKER_REPOSITORY}:${SHA}" SHA_DOCKER_NAME="${DOCKER_REPOSITORY}:${SHA}"
docker build -t ${DOCKERNAME} -t ${SHA_DOCKER_NAME} . docker build $CUSTOMDOCKERFILE -t ${DOCKERNAME} -t ${SHA_DOCKER_NAME} .
docker push ${DOCKERNAME} docker push ${DOCKERNAME}
docker push ${SHA_DOCKER_NAME} docker push ${SHA_DOCKER_NAME}
else else
docker build -t ${DOCKERNAME} . docker build $CUSTOMDOCKERFILE -t ${DOCKERNAME} .
docker push ${DOCKERNAME} docker push ${DOCKERNAME}
fi fi

38
test.sh
View File

@@ -1,5 +1,10 @@
#!/bin/sh -e #!/bin/sh -e
function cleanEnvironment() {
unset INPUT_snapshot
unset INPUT_dockerfile
}
function itPushesMasterBranchToLatest() { function itPushesMasterBranchToLatest() {
export GITHUB_REF='refs/heads/master' export GITHUB_REF='refs/heads/master'
local result=$(exec /entrypoint.sh 'my/repository') local result=$(exec /entrypoint.sh 'my/repository')
@@ -36,6 +41,20 @@ Called mock with: push my/repository:latest"
fi fi
} }
function itPushesSpecificDockerfileReleasesToLatest() {
export GITHUB_REF='refs/tags/myRelease'
export INPUT_dockerfile='MyDockerFileName'
local result=$(exec /entrypoint.sh 'my/repository')
local expected="Called mock with: build -f MyDockerFileName -t my/repository:latest .
Called mock with: push my/repository:latest"
if [ "$result" != "$expected" ]; then
echo "Expected: $expected
Got: $result"
exit 1
fi
cleanEnvironment
}
function itPushesBranchByShaInAddition() { function itPushesBranchByShaInAddition() {
export GITHUB_REF='refs/tags/myRelease' export GITHUB_REF='refs/tags/myRelease'
export INPUT_snapshot='true' export INPUT_snapshot='true'
@@ -48,9 +67,28 @@ Called mock with: push my/repository:COMMIT_SHA"
Got: $result" Got: $result"
exit 1 exit 1
fi fi
cleanEnvironment
}
function itPushesBranchByShaInAdditionWithSpecificDockerfile() {
export GITHUB_REF='refs/tags/myRelease'
export INPUT_snapshot='true'
export INPUT_dockerfile='MyDockerFileName'
local result=$(exec env 'github.sha'=COMMIT_SHA /entrypoint.sh 'my/repository')
local expected="Called mock with: build -f MyDockerFileName -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
cleanEnvironment
} }
itPushesMasterBranchToLatest itPushesMasterBranchToLatest
itPushesBranchAsNameOfTheBranch itPushesBranchAsNameOfTheBranch
itPushesReleasesToLatest itPushesReleasesToLatest
itPushesSpecificDockerfileReleasesToLatest
itPushesBranchByShaInAddition itPushesBranchByShaInAddition
itPushesBranchByShaInAdditionWithSpecificDockerfile