#! /usr/bin/bash # Automatic Update Script for n8n # ================================= # This script checks for updates to the n8n Docker image and updates it if a newer version is available. # # It uses the Docker Hub API to retrieve the latest image digest and compares it to the digest of the currently running image. # If an update is available, the script stops the running container, pulls the latest image, and starts a new container with the updated image. # # Dependencies: # - Docker # # Usage: # Simply run the script to check for updates and update the n8n container if a newer version is available. # # To automate this script to run weekly, you can add it as a cron job. Here's how: # 1. Open the crontab editor by running the command `crontab -e` in your terminal. # 2. Add the following line to the end of the file: `0 18 * * 1 /path/to/update_n8n.sh` # 3. Replace `/path/to/update_n8n.sh` with the actual path to this script. # 4. Save and exit the editor. # # This will schedule the script to run every Monday at 6pm. You can adjust the schedule as needed by modifying the cron expression. # # Here's a breakdown of the cron expression: # - `0 18 * * 1`: This means "run at minute 0, hour 18 (6pm), every day of the month, every month, on Monday (day of the week 1)". # Variables IMAGE='n8nio/n8n' # The Docker image CONTAINER_NAME='n8n' # Function to get the latest image digest from Docker Hub get_latest_image_digest() { docker pull $IMAGE > /dev/null 2>&1 docker inspect --format='{{index .RepoDigests 0}}' $IMAGE | awk -F@ '{print $2}' } # Function to get the current running image's digest get_current_image_digest() { docker inspect --format='{{.Image}}' $(docker ps -q --filter "name=$CONTAINER_NAME") | xargs docker inspect --format='{{index .RepoDigests 0}}' | awk -F@ '{print $2}' } # Get the latest digest from the repository LATEST_DIGEST=$(get_latest_image_digest) # Get the currently running image's digest CURRENT_DIGEST=$(get_current_image_digest) # Check if the digests are different, indicating an update is needed if [ "$LATEST_DIGEST" != "$CURRENT_DIGEST" ]; then echo "$(date) - A newer version of n8n is available. Updating..." # Stop the running container, pull the latest image, and start the updated container docker stop $(docker ps -q --filter "name=$CONTAINER_NAME") docker pull $IMAGE docker run -d --rm --name $CONTAINER_NAME -p 5678:5678 \ -e GENERIC_TIMEZONE="Europe/London" -e TZ="Europe/London" \ -v n8n_data:/home/node/.n8n $IMAGE echo "$(date) - Update complete." else echo "$(date) - n8n is up to date." fi # Remove unused Docker images #docker image prune -f # Remove unused Docker volumes #docker volume prune -f # Remove stopped Docker containers #docker container prune -f