diff --git a/DOCS.md b/DOCS.md new file mode 100644 index 0000000..3aa33ce --- /dev/null +++ b/DOCS.md @@ -0,0 +1,37 @@ +Use the Volume-cache plugin to preserve files and directories between builds. +Because it uses a mounted volume, it requires repositories using it to enable the *"Trusted"* flag. + +## Config +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 to cache + +## Examples +```yaml +pipeline: + restore-cache: + image: drillster/drone-cache + restore: true + mount: + - ./node_modules + # Mount the cache volume, needs "Trusted" + volumes: + - /tmp/cache:/cache + + build: + image: node + commands: + - npm install + + rebuild-cache: + image: drillster/drone-cache + rebuild: true + mount: + - ./node_modules + # Mount the cache volume, needs "Trusted" + volumes: + - /tmp/cache:/cache +``` + +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. diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a89e439 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,8 @@ +FROM alpine:3.4 +MAINTAINER Michael de Wit + +RUN mkdir /cache && apk add --no-cache bash rsync +COPY cacher.sh /usr/local/ +VOLUME /cache + +ENTRYPOINT ["/usr/local/cacher.sh"] diff --git a/README.md b/README.md new file mode 100644 index 0000000..1b4a223 --- /dev/null +++ b/README.md @@ -0,0 +1,24 @@ +# drone-volume-cache +This is a pure Bash [Drone](https://github.com/drone/drone) 0.5 plugin to cache files and/or folders to a locally mounted volume. + +## Docker +Build the docker image by running: + +```bash +docker build --rm=true -t drillster/drone-volume-cache . +``` + +## Usage +Execute from the working directory: + +```bash +docker run --rm \ + -e PLUGIN_REBUILD=true \ + -e PLUGIN_MOUNT="./node_modules" \ + -e DRONE_REPO_OWNER="foo" \ + -e DRONE_REPO_NAME="bar" \ + -v $(pwd):$(pwd) \ + -v /tmp/cache:/cache \ + -w $(pwd) \ + drillster/drone-volume-cache +``` diff --git a/cacher.sh b/cacher.sh new file mode 100644 index 0000000..1c45e25 --- /dev/null +++ b/cacher.sh @@ -0,0 +1,27 @@ +#!/bin/bash + +if [ -z "$PLUGIN_MOUNT" ]; then + echo "Specify folders to cache in the mount property! Plugin won't do anything!" + exit 0; +fi + +IFS=','; read -ra SOURCES <<< "$PLUGIN_MOUNT" +if [ -n "$PLUGIN_REBUILD" ]; 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 "$source/" "/cache/$DRONE_REPO_OWNER/$DRONE_REPO_NAME/$source" + done +elif [ -n "$PLUGIN_RESTORE" ]; then + # Restore from cache + for source in "${SOURCES[@]}"; do + if [ -d "/cache/$DRONE_REPO_OWNER/$DRONE_REPO_NAME/$source" ]; then + echo "Restoring cache for $source..." + mkdir -p "$source" && rsync -aHAX "/cache/$DRONE_REPO_OWNER/$DRONE_REPO_NAME/$source/" "$source" + else + echo "No cache for $source" + fi + done +else + echo "No restore or rebuild flag specified, plugin won't do anything!" +fi