Advertisement
J2897

update_n8n

Sep 8th, 2024 (edited)
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.79 KB | None | 0 0
  1. #! /usr/bin/bash
  2.  
  3. # Automatic Update Script for n8n
  4. # =================================
  5. # This script checks for updates to the n8n Docker image and updates it if a newer version is available.
  6. #
  7. # It uses the Docker Hub API to retrieve the latest image digest and compares it to the digest of the currently running image.
  8. # If an update is available, the script stops the running container, pulls the latest image, and starts a new container with the updated image.
  9. #
  10. # Dependencies:
  11. #   - Docker
  12. #
  13. # Usage:
  14. #   Simply run the script to check for updates and update the n8n container if a newer version is available.
  15. #
  16. # To automate this script to run weekly, you can add it as a cron job. Here's how:
  17. #   1. Open the crontab editor by running the command `crontab -e` in your terminal.
  18. #   2. Add the following line to the end of the file: `0 18 * * 1 /path/to/update_n8n.sh`
  19. #   3. Replace `/path/to/update_n8n.sh` with the actual path to this script.
  20. #   4. Save and exit the editor.
  21. #
  22. # This will schedule the script to run every Monday at 6pm. You can adjust the schedule as needed by modifying the cron expression.
  23. #
  24. # Here's a breakdown of the cron expression:
  25. #   - `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)".
  26.  
  27. # Variables
  28. IMAGE='n8nio/n8n'  # The Docker image
  29. CONTAINER_NAME='n8n'
  30.  
  31. # Function to get the latest image digest from Docker Hub
  32. get_latest_image_digest() {
  33.   docker pull $IMAGE > /dev/null 2>&1
  34.   docker inspect --format='{{index .RepoDigests 0}}' $IMAGE | awk -F@ '{print $2}'
  35. }
  36.  
  37. # Function to get the current running image's digest
  38. get_current_image_digest() {
  39.   docker inspect --format='{{.Image}}' $(docker ps -q --filter "name=$CONTAINER_NAME") | xargs docker inspect --format='{{index .RepoDigests 0}}' | awk -F@ '{print $2}'
  40. }
  41.  
  42. # Get the latest digest from the repository
  43. LATEST_DIGEST=$(get_latest_image_digest)
  44.  
  45. # Get the currently running image's digest
  46. CURRENT_DIGEST=$(get_current_image_digest)
  47.  
  48. # Check if the digests are different, indicating an update is needed
  49. if [ "$LATEST_DIGEST" != "$CURRENT_DIGEST" ]; then
  50.   echo "$(date) - A newer version of n8n is available. Updating..."
  51.  
  52.   # Stop the running container, pull the latest image, and start the updated container
  53.   docker stop $(docker ps -q --filter "name=$CONTAINER_NAME")
  54.   docker pull $IMAGE
  55.   docker run -d --rm --name $CONTAINER_NAME -p 5678:5678 \
  56.     -e GENERIC_TIMEZONE="Europe/London" -e TZ="Europe/London" \
  57.     -v n8n_data:/home/node/.n8n $IMAGE
  58.  
  59.   echo "$(date) - Update complete."
  60. else
  61.   echo "$(date) - n8n is up to date."
  62. fi
  63.  
  64. # Remove unused Docker images
  65. #docker image prune -f
  66.  
  67. # Remove unused Docker volumes
  68. #docker volume prune -f
  69.  
  70. # Remove stopped Docker containers
  71. #docker container prune -f
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement