#! /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
Comments
0 B
|π
/π
0 B
|π
/π
0 B
|π
/π
0 B
|π
/π
0 B
|π
/π
0 B
|π
/π
0 B
|π
/π
0 B
|π
/π
0 B
|π
/π