-
Notifications
You must be signed in to change notification settings - Fork 1
/
cache_run
executable file
·44 lines (30 loc) · 1.16 KB
/
cache_run
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#! /bin/bash
# shellcheck disable=SC2086
set -o errexit
MD5_CMD=${MD5_CMD:-md5sum}
CACHE_KEY_PREFIX=${CACHE_KEY_PREFIX:-}
CACHE_ON_DIR=${CACHE_ON_DIR:-}
# should be space separated
CACHE_OUT_DIRS=${CACHE_OUT_DIRS:-}
COMMAND=$1
if [[ ! "$COMMAND" || ! "$CACHE_KEY_PREFIX" || ! "$CACHE_ON_DIR" || ! "$CACHE_OUT_DIRS" ]]; then
echo "cache_run: error: missing required input"
exit 1
fi
CHK_SUM=$(find $CACHE_ON_DIR -type f -exec $MD5_CMD {} \; | $MD5_CMD | cut -f1 -d ' ')
echo "cache_run: calculated checksum on dir $CACHE_ON_DIR : $CHK_SUM"
if cachr exists "$CACHE_KEY_PREFIX/$CHK_SUM"; then
echo "cache_run: cache already exists! skipping..."
exit 0
fi
echo "cache_run: checkout prev commit"
git checkout HEAD~1 > /dev/null
PREV_CHK_SUM=$(find $CACHE_ON_DIR -type f -exec $MD5_CMD {} \; | $MD5_CMD | cut -f1 -d ' ')
echo "cache_run: getting cache: $PREV_CHK_SUM for commit: $(git rev-parse HEAD)"
cachr get "$CACHE_KEY_PREFIX/$PREV_CHK_SUM" || true
echo "cache_run: checkout current commit"
git checkout - > /dev/null
echo "cache_run: running command"
bash -c "$COMMAND"
echo "cache_run: saving cache to s3"
cachr save "$CACHE_KEY_PREFIX/$CHK_SUM" $CACHE_OUT_DIRS