From 4ce79429c63d8b24138b3433144f68310dd9fac8 Mon Sep 17 00:00:00 2001 From: Michael de Wit Date: Wed, 1 Feb 2017 14:42:43 +0100 Subject: [PATCH] Add [NO CACHE] and [CLEAR CACHE] to commit message options to control plugin behavior, use separate cache for matrix jobs. Fixes #3 --- DOCS.md | 6 ++++++ README.md | 1 + cacher.sh | 26 +++++++++++++++++++------- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/DOCS.md b/DOCS.md index bf7f917..becf55d 100644 --- a/DOCS.md +++ b/DOCS.md @@ -35,3 +35,9 @@ 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. + +## 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. + +## Skipping Cache +If you want to run a build without using cache, put `[NO CACHE]` in the commit message. Both the restoring and rebuilding of cache will be skipped. Your cache will remain intact. \ No newline at end of file diff --git a/README.md b/README.md index da5db5e..90fff45 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ docker run --rm \ -e PLUGIN_MOUNT="./node_modules" \ -e DRONE_REPO_OWNER="foo" \ -e DRONE_REPO_NAME="bar" \ + -e DRONE_JOB_NUMBER=0 \ -v $(pwd):$(pwd) \ -v /tmp/cache:/cache \ -w $(pwd) \ diff --git a/cacher.sh b/cacher.sh index f14be91..e2f59d3 100644 --- a/cacher.sh +++ b/cacher.sh @@ -2,24 +2,36 @@ if [ -z "$PLUGIN_MOUNT" ]; then echo "Specify folders to cache in the mount property! Plugin won't do anything!" - exit 0; + exit 0 +fi + +if [[ $DRONE_COMMIT_MESSAGE == *"[CLEAR CACHE]"* && -n "$PLUGIN_RESTORE" && "$PLUGIN_RESTORE" == "true" ]]; then + if [ -d "/cache/$DRONE_REPO_OWNER/$DRONE_REPO_NAME" ]; then + echo "Found [CLEAR CACHE] in commit message, clearing cache!" + rm -rf "/cache/$DRONE_REPO_OWNER/$DRONE_REPO_NAME" + fi +fi + +if [[ $DRONE_COMMIT_MESSAGE == *"[NO CACHE]"* ]]; then + echo "Found [NO CACHE] in commit message, skipping cache restore and rebuild!" + exit 0 fi IFS=','; read -ra SOURCES <<< "$PLUGIN_MOUNT" -if [ -n "$PLUGIN_REBUILD" ]; then +if [[ -n "$PLUGIN_REBUILD" && "$PLUGIN_REBUILD" == "true" ]]; then # Create cache for source in "${SOURCES[@]}"; do echo "Rebuilding cache for $source..." - mkdir -p "/cache/$DRONE_REPO_OWNER/$DRONE_REPO_NAME/$source" && \ - rsync -aHAX --delete "$source/" "/cache/$DRONE_REPO_OWNER/$DRONE_REPO_NAME/$source" + mkdir -p "/cache/$DRONE_REPO_OWNER/$DRONE_REPO_NAME/$DRONE_JOB_NUMBER/$source" && \ + rsync -aHAX --delete "$source/" "/cache/$DRONE_REPO_OWNER/$DRONE_REPO_NAME/$DRONE_JOB_NUMBER/$source" done -elif [ -n "$PLUGIN_RESTORE" ]; then +elif [[ -n "$PLUGIN_RESTORE" && "$PLUGIN_RESTORE" == "true" ]]; then # Restore from cache for source in "${SOURCES[@]}"; do - if [ -d "/cache/$DRONE_REPO_OWNER/$DRONE_REPO_NAME/$source" ]; then + if [ -d "/cache/$DRONE_REPO_OWNER/$DRONE_REPO_NAME/$DRONE_JOB_NUMBER/$source" ]; then echo "Restoring cache for $source..." mkdir -p "$source" && \ - rsync -aHAX --delete "/cache/$DRONE_REPO_OWNER/$DRONE_REPO_NAME/$source/" "$source" + rsync -aHAX --delete "/cache/$DRONE_REPO_OWNER/$DRONE_REPO_NAME/$DRONE_JOB_NUMBER/$source/" "$source" else echo "No cache for $source" fi