Skip to content

Instantly share code, notes, and snippets.

@jedihe
Created April 11, 2025 22:57
Show Gist options
  • Save jedihe/3ae72effacb6eec0eb8e0f3f6f6c4ae7 to your computer and use it in GitHub Desktop.
Save jedihe/3ae72effacb6eec0eb8e0f3f6f6c4ae7 to your computer and use it in GitHub Desktop.
Automatic updates of Drupal modules, with confirmation step to allow inspection/tweaks of the changes to be committed
#!/bin/bash
# Usage:
# cd /path/to/dir-containing-composer-json
# ./path/to/drupal-composer-auto-updates.sh
# Fetch outdated Drupal packages excluding dev versions
outdated_packages=$(composer outdated 'drupal/*' | grep -v ' dev-' | grep -v 'drupal/core' | awk '{print $1":"$2":"$4}')
# Check if there are any packages to update
if [[ -z "$outdated_packages" ]]; then
echo "No applicable updates found."
exit 0
fi
# Display the packages to be updated and wait for confirmation
echo "The following package updates will be processed:"
echo "$outdated_packages"
echo "Press any key to continue..."
read -r -n 1 -s -p $'Press any key to continue...\n' < /dev/tty
# Loop through each package to update
echo "$outdated_packages" | while IFS= read -r package_version; do
if [[ -z "$package_version" ]]; then continue; fi
# Split package and version
IFS=':' read -r package oldversion version <<< "$package_version"
echo "Updating $package from $oldversion to $version ..."
# Update the package; try up to 3 ways, attempting to minimize changes to composer.json
composer require "$package:$version" || \
composer require "$package:$version" -w || \
composer require "$package:$version" -W
# Run database updates and export configuration
drush updb -y
drush config:export --diff -y
# Add changes if there are any
git add .
# Show diff and wait for inspection
git diff --cached
read -r -n 1 -s -p $'Press any key to continue after inspection...\n' < /dev/tty
# Check for working tree changes
if ! git diff --cached --quiet; then
# Commit changes
commit_message="composer require $package:$version (was: $oldversion)"
if git status | grep -q 'config/'; then
commit_message+="; update exported config"
fi
git commit -m "$commit_message"
else
echo "No changes to commit for $package."
fi
done
echo "Updates completed."
@jedihe
Copy link
Author

jedihe commented Apr 11, 2025

WARNING: always make sure to test updates in a non-production environment before deploying to production.

Since Drupal core is excluded from the script, I suggest starting the updates by manually updating drupal/core* packages (check which ones are defined in the root composer.json for your site), e.g.:

composer require drupal/core:10.4.6 drupal/core-recommended:10.4.6 drupal/core-composer-scaffold:10.4.6
drush updb -y
drush config:export -y
git add .
git add -u .
git commit -m 'Updating to Drupal core 10.4.6'

If it fails, retry appending "-w", if it fails again retry with "-W" instead.

Then, run drupal-composer-auto-updates.sh

Happy drupaling!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment