diff --git a/DOCS.md b/DOCS.md index 305cb9d..15be8a8 100644 --- a/DOCS.md +++ b/DOCS.md @@ -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. diff --git a/Dockerfile b/Dockerfile index 8faeaad..589abbd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,7 +2,7 @@ FROM alpine:3.4 MAINTAINER Michael de Wit 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"] diff --git a/cacher.sh b/cacher.sh index 2b11936..99372df 100644 --- a/cacher.sh +++ b/cacher.sh @@ -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