Last active
September 12, 2023 22:54
-
-
Save utarn/1abcd2e474d6807cd0a8c750ca74d459 to your computer and use it in GitHub Desktop.
Auto update docker compose if new image is found for all subdirectories on given base path
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Add to crontab | |
# */5 * * * * /root/autoUpdate.sh | ts | tee -a /var/log/updater/updater-$(date +'\%Y-\%m-\%d').log | |
#!/bin/bash | |
# Define a function for null coalescing | |
coalesce() { | |
if [ -n "$1" ]; then | |
echo "$1" | |
else | |
echo "$2" | |
fi | |
} | |
basePath="/root" | |
file_list=$(find "$basePath" -type f -name "docker-compose.yml" | grep -v var | grep -v baget | grep -v postgresql | grep -v authorized | grep -v archive | grep -v backup) | |
# Find directories containing docker-compose.yml files and store them in an array | |
# directories=($(find "$basePath" -type f -name "docker-compose.yml" -exec dirname {} \;)) | |
for file in $file_list; do | |
dir=$(dirname $file) | |
# Change to the directory | |
cd "$dir" || continue | |
echo "Checking directory: $dir" | |
# Pull the latest image | |
docker compose pull 2> /dev/null | |
result=$(docker compose images --format json | jq '.[].Tag | select(. == "")') | |
result2="no" | |
checkValue=$(coalesce "$result" "$result2") | |
# Check if any Tag property is empty in images | |
if echo "$checkValue" | grep -q '\"\"'; then | |
# If there's an empty Tag, bring up the service | |
docker compose up -d 2> /dev/null | |
echo "New images are running" | |
docker prune -af | |
else | |
echo "No new image" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment