Add caching

This commit is contained in:
Lars Gohr
2019-08-29 08:34:53 +02:00
parent f7980fbb91
commit c665a8a990
3 changed files with 94 additions and 5 deletions

View File

@@ -63,3 +63,14 @@ with:
password: ${{ secrets.DOCKER_PASSWORD }}
dockerfile: MyDockerFileName
```
Use `cache` when you have big images, that you would only like to build partially (changed layers).
> CAUTION: This will cache the non changed parts forever. If you use this option, make sure that these parts will be updated by another job!
```yaml
with:
name: myDocker/repository
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
cache: true
```

View File

@@ -27,24 +27,29 @@ if [ $(echo ${GITHUB_REF} | sed -e "s/refs\/tags\///g") != ${GITHUB_REF} ]; then
BRANCH="latest"
fi;
echo ${INPUT_PASSWORD} | docker login -u ${INPUT_USERNAME} --password-stdin ${INPUT_REGISTRY}
DOCKERNAME="${INPUT_NAME}:${BRANCH}"
CUSTOMDOCKERFILE=""
BUILDPARAMS=""
if [ ! -z "${INPUT_DOCKERFILE}" ]; then
CUSTOMDOCKERFILE="-f ${INPUT_DOCKERFILE}"
BUILDPARAMS="$BUILDPARAMS -f ${INPUT_DOCKERFILE}"
fi
echo ${INPUT_PASSWORD} | docker login -u ${INPUT_USERNAME} --password-stdin ${INPUT_REGISTRY}
if [ ! -z "${INPUT_CACHE}" ]; then
docker pull ${DOCKERNAME}
BUILDPARAMS="$BUILDPARAMS --cache-from ${DOCKERNAME}"
fi
if [ "${INPUT_SNAPSHOT}" == "true" ]; then
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 build $BUILDPARAMS -t ${DOCKERNAME} -t ${SHA_DOCKER_NAME} .
docker push ${DOCKERNAME}
docker push ${SHA_DOCKER_NAME}
else
docker build $CUSTOMDOCKERFILE -t ${DOCKERNAME} .
docker build $BUILDPARAMS -t ${DOCKERNAME} .
docker push ${DOCKERNAME}
fi

73
test.sh
View File

@@ -3,6 +3,8 @@
function cleanEnvironment() {
unset INPUT_SNAPSHOT
unset INPUT_DOCKERFILE
unset INPUT_REGISTRY
unset INPUT_CACHE
unset GITHUB_SHA
}
@@ -98,6 +100,30 @@ Called mock with: logout"
cleanEnvironment
}
function itCachesImageFromFormerBuildAndUsesItForSnapshotIfConfigured() {
export GITHUB_REF='refs/tags/myRelease'
export INPUT_USERNAME='USERNAME'
export INPUT_PASSWORD='PASSWORD'
export INPUT_NAME='my/repository'
export GITHUB_SHA='12169ed809255604e557a82617264e9c373faca7'
export MOCK_DATE='197001010101'
export INPUT_SNAPSHOT='true'
export INPUT_CACHE='true'
local result=$(exec /entrypoint.sh)
local expected="Called mock with: login -u USERNAME --password-stdin
Called mock with: pull my/repository:latest
Called mock with: build --cache-from my/repository:latest -t my/repository:latest -t my/repository:19700101010112169e .
Called mock with: push my/repository:latest
Called mock with: push my/repository:19700101010112169e
Called mock with: logout"
if [ "$result" != "$expected" ]; then
echo "Expected: $expected
Got: $result"
exit 1
fi
cleanEnvironment
}
function itPushesBranchByShaAndDateInAdditionWithSpecificDockerfile() {
export GITHUB_REF='refs/tags/myRelease'
export INPUT_SNAPSHOT='true'
@@ -121,6 +147,31 @@ Called mock with: logout"
cleanEnvironment
}
function itCachesImageFromFormerBuildAndUsesItForSnapshotIfConfiguredIsOkWithSpecificDockerfile() {
export GITHUB_REF='refs/tags/myRelease'
export INPUT_USERNAME='USERNAME'
export INPUT_PASSWORD='PASSWORD'
export INPUT_NAME='my/repository'
export GITHUB_SHA='12169ed809255604e557a82617264e9c373faca7'
export MOCK_DATE='197001010101'
export INPUT_SNAPSHOT='true'
export INPUT_CACHE='true'
export INPUT_DOCKERFILE='MyDockerFileName'
local result=$(exec /entrypoint.sh)
local expected="Called mock with: login -u USERNAME --password-stdin
Called mock with: pull my/repository:latest
Called mock with: build -f MyDockerFileName --cache-from my/repository:latest -t my/repository:latest -t my/repository:19700101010112169e .
Called mock with: push my/repository:latest
Called mock with: push my/repository:19700101010112169e
Called mock with: logout"
if [ "$result" != "$expected" ]; then
echo "Expected: $expected
Got: $result"
exit 1
fi
cleanEnvironment
}
function itLogsIntoAnotherRegistryIfConfigured() {
export GITHUB_REF='refs/tags/myRelease'
export INPUT_USERNAME='USERNAME'
@@ -140,6 +191,26 @@ Called mock with: logout"
cleanEnvironment
}
function itCachesImageFromFormerBuildIfConfigured() {
export GITHUB_REF='refs/tags/myRelease'
export INPUT_USERNAME='USERNAME'
export INPUT_PASSWORD='PASSWORD'
export INPUT_NAME='my/repository'
export INPUT_CACHE='true'
local result=$(exec /entrypoint.sh)
local expected="Called mock with: login -u USERNAME --password-stdin
Called mock with: pull my/repository:latest
Called mock with: build --cache-from my/repository:latest -t my/repository:latest .
Called mock with: push my/repository:latest
Called mock with: logout"
if [ "$result" != "$expected" ]; then
echo "Expected: $expected
Got: $result"
exit 1
fi
cleanEnvironment
}
function itErrorsWhenNameWasNotSet() {
unset INPUT_NAME
local result=$(exec /entrypoint.sh)
@@ -181,8 +252,10 @@ itPushesBranchAsNameOfTheBranch
itPushesReleasesToLatest
itPushesSpecificDockerfileReleasesToLatest
itPushesBranchByShaAndDateInAddition
itCachesImageFromFormerBuildAndUsesItForSnapshotIfConfigured
itPushesBranchByShaAndDateInAdditionWithSpecificDockerfile
itLogsIntoAnotherRegistryIfConfigured
itCachesImageFromFormerBuildIfConfigured
itErrorsWhenNameWasNotSet
itErrorsWhenUsernameWasNotSet
itErrorsWhenPasswordWasNotSet