From 9585fa84d17df23d9dc564eebd787467e34eb8ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Verg=C3=A9?= Date: Tue, 13 Dec 2016 15:07:42 +0100 Subject: [PATCH] Use `rsync -aHAX --delete` When creating/restoring cache, the user wants the created files and dir to *replace* existing ones. This also implies deleting leftover files. To be clearer, let's take an example with a JS / TypeScript project: - A project needs to install some "typings" named `footypes` in `node_modules`. - `footypes` becomes deprecated and should be replaced by `footypes2`. The developer then replaces `footypes` with `footypes2` in his `package.json`. - On next builds, old cache is restored (including `node_modules/footypes`). Then `footypes2` is also installed in `node_modules`. - Builds now fail because there are conflicting typings for library `foo`: both `footypes` and `footypes2` are present. With the `--delete` flag added to `rsync`, when creating new caches, files that are not present in source dir are deleted from target dir. --- cacher.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cacher.sh b/cacher.sh index 1c45e25..f14be91 100644 --- a/cacher.sh +++ b/cacher.sh @@ -10,14 +10,16 @@ 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" + mkdir -p "/cache/$DRONE_REPO_OWNER/$DRONE_REPO_NAME/$source" && \ + rsync -aHAX --delete "$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" + mkdir -p "$source" && \ + rsync -aHAX --delete "/cache/$DRONE_REPO_OWNER/$DRONE_REPO_NAME/$source/" "$source" else echo "No cache for $source" fi