Add TTL feature for cached files and folders

This commit is contained in:
Michael de Wit
2017-07-03 09:25:05 +02:00
parent 65ee2c6d06
commit d0637d0fba
3 changed files with 30 additions and 1 deletions
+19
View File
@@ -6,6 +6,7 @@ The following parameters are used to configure the plugin:
- **restore** - instruct plugin to restore cache, can be `true` or `false`
- **rebuild** - instruct plugin to rebuild cache, can be `true` or `false`
- **mount** - list of folders or files to cache
- **ttl** - maximum cache lifetime in days
## Examples
```yaml
@@ -36,6 +37,24 @@ pipeline:
The example above illustrates a typical Node.js project Drone configuration. It caches the `./node_modules` directory to a mounted volume on the host system: `/tmp/cache`. This prevents `npm` from downloading and installing the dependencies for every build.
## Using cache lifetime
It is possible to limit the lifetime of cached files and folders.
```yaml
pipeline:
restore-cache:
image: drillster/drone-volume-cache
restore: true
mount:
- ./node_modules
# Mount the cache volume, needs "Trusted"
volumes:
- /tmp/cache:/cache
ttl: 7
```
The example above shows a situation where cached items older than 7 days will not be restored (they will be removed instead). Only the restore step needs the `ttl` parameter.
## Clearing Cache
Should you want to clear the cache for a project, you can do so by including `[CLEAR CACHE]` in the commit message. The entire cache folder for the project will be cleared before it is restored. The rebuilding of cache will proceed as normal.
+1 -1
View File
@@ -2,7 +2,7 @@ FROM alpine:3.4
MAINTAINER Michael de Wit <michael@drillster.com>
COPY cacher.sh /usr/local/
RUN mkdir /cache && apk add --no-cache bash rsync && chmod 755 /usr/local/cacher.sh
RUN mkdir /cache && apk add --no-cache bash rsync findutils && chmod 755 /usr/local/cacher.sh
VOLUME /cache
ENTRYPOINT ["/usr/local/cacher.sh"]
+10
View File
@@ -36,6 +36,16 @@ if [[ -n "$PLUGIN_REBUILD" && "$PLUGIN_REBUILD" == "true" ]]; then
fi
done
elif [[ -n "$PLUGIN_RESTORE" && "$PLUGIN_RESTORE" == "true" ]]; then
# Remove files older than TTL
if [[ -n "$PLUGIN_TTL" && "$PLUGIN_TTL" > "0" ]]; then
if [[ $PLUGIN_TTL =~ ^[0-9]+$ ]]; then
echo "Removing files and (empty) folders older than $PLUGIN_TTL days..."
find "/cache/$DRONE_REPO_OWNER/$DRONE_REPO_NAME/$DRONE_JOB_NUMBER" -type f -ctime +$PLUGIN_TTL -delete
find "/cache/$DRONE_REPO_OWNER/$DRONE_REPO_NAME/$DRONE_JOB_NUMBER" -type d -ctime +$PLUGIN_TTL -empty -delete
else
echo "Invalid value for ttl, please enter a positive integer. Plugin will ignore ttl."
fi
fi
# Restore from cache
for source in "${SOURCES[@]}"; do
if [ -d "/cache/$DRONE_REPO_OWNER/$DRONE_REPO_NAME/$DRONE_JOB_NUMBER/$source" ]; then