enhance deployment script to only copy changed files

This commit is contained in:
OMGeeky
2025-05-17 16:02:36 +02:00
parent 9f96d5c1b9
commit 5ef01830d4

View File

@@ -1,11 +1,11 @@
#pip install adafruit-ampy
CODE_DIR="src/"
LIBS_DIR=deploy/upload
UPLOAD_SOURCE_DIR=deploy/upload
CONFIG_FILE=deploy/config.json
ACTUAL_UPLOAD_SOURCE_DIR=deploy/actual_upload
LAST_UPLOAD_DIR=deploy/last_upload
UPLOAD_SOURCE_DIR=deploy/upload
ACTUAL_UPLOAD_SOURCE_DIR=deploy/actual_upload
UPLOAD_TARGET_DIR=
echo "preparing for deployment"
@@ -28,12 +28,32 @@ fi
# check what files have changed and only upload the changed files (use $ACTUAL_UPLOAD_SOURCE_DIR for the changed files)
echo "Checking for changes in the upload directory"
rm -rf "$ACTUAL_UPLOAD_SOURCE_DIR"
mkdir -p "$ACTUAL_UPLOAD_SOURCE_DIR"
#rsync -a --delete --ignore-existing "$UPLOAD_SOURCE_DIR/" "$LAST_UPLOAD_DIR/" "$ACTUAL_UPLOAD_SOURCE_DIR/"
# TODO: use diff or rsync to check for changes and only upload the changed files (and delete the files, that are no longer there)
cp -r "$UPLOAD_SOURCE_DIR"/* "$ACTUAL_UPLOAD_SOURCE_DIR"
if [[ "$1" == "-f" ]]; then
echo "Force copying all files"
cp -r "$UPLOAD_SOURCE_DIR"/* "$ACTUAL_UPLOAD_SOURCE_DIR"
else
echo "Checking for changes in the upload directory"
# Use diff to find changes and copy only modified or new files
for file in $(find "$UPLOAD_SOURCE_DIR" -type f); do
relative_path="${file#$UPLOAD_SOURCE_DIR/}"
last_file="$LAST_UPLOAD_DIR/$relative_path"
if [[ ! -f "$last_file" ]] || ! diff -q "$file" "$last_file" > /dev/null; then
echo "Copying changed file: $file"
mkdir -p "$(dirname "$ACTUAL_UPLOAD_SOURCE_DIR/$relative_path")"
cp "$file" "$ACTUAL_UPLOAD_SOURCE_DIR/$relative_path"
fi
done
# TODO: remove files that are no longer in the upload directory but were in the last upload directory
# for file in $(find "$LAST_UPLOAD_DIR" -type f); do
# relative_path="${file#$LAST_UPLOAD_DIR/}"
# if [[ ! -f "$UPLOAD_SOURCE_DIR/$relative_path" ]]; then
# echo "Removing file: $file"
# fi
# done
fi
if [ -z "$ESP_PORT" ]; then
@@ -42,4 +62,5 @@ if [ -z "$ESP_PORT" ]; then
fi
echo "Deploying to ESP32 on port '$ESP_PORT'"
# TODO: clear storage, if -f flag is passed in?
ampy --port "$ESP_PORT" put "$ACTUAL_UPLOAD_SOURCE_DIR"/ "$UPLOAD_TARGET_DIR"/