
This is a script I made for updating all of my Bedrock WordPress sites hosted on Digital Ocean. It’s useful because it removes the need go to every site, update composer, commit changes, and deploy manually. There’s not much error checking though, so you should make sure the updates were successful. In the future, it would be nice to implement some sort of uptime monitoring to make sure the updates don’t make the sites go down.
Usage of the script:
# Update all sites ./update-prod.sh -a # Update selected sites ./update-prod -s site1 site2 site3 # Help ./update-prod -h
Staging Sites or Sites with Non-.com Domain
If I have a staging site or a site without a .net or .org suffix, then I would create conditionals inside the script for each non-.com.
if [[ "$SITE" = "example" ]]; then
./bin/deploy.sh production staging."$SITE".com
else
./bin/deploy.sh production "$SITE".com
fi
The Script
Save this to update-prod.sh and give it 777 permissions.
#!/bin/bash
## Update all sites
update_all () {
read -r -p "Would you really like to update all production sites? [y/N] " response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then
update_list site1 site2 site3
fi
}
## Update list of sites
update_list () {
for SITE in "$@"
do
cd ~/Sites/Development/DO-"$SITE"/"$SITE" &&
composer update &&
git commit composer.lock -m "wordpress updates" &&
git push
cd ../trellis &&
./bin/deploy.sh production "$SITE".com
### Open site in browser to make sure nothing is broken
open https://"$SITE".com
done
}
## Loop through script arguments
unset -v sub
while getopts "has:" OPTION
do
case $OPTION in
a)
update_all
echo "All sites finished updating"
exit
;;
s)
sub=("$OPTARG")
until [[ $(eval "echo \${$OPTIND}") =~ ^-.* ]] || [ -z $(eval "echo \${$OPTIND}") ]; do
sub+=($(eval "echo \${$OPTIND}"))
OPTIND=$((OPTIND + 1))
done
update_list ${sub[@]}
exit
;;
h)
echo "-a : updates all sites"
echo "-s : update selected sites (usage: -s site1 site2)"
exit
;;
\?)
echo "Use the -h flag for the help menu"
exit
;;
esac
done
More Complete Version
To work in all scenarios, we would change our script to read the full domain name and appropriately name your project folder and bedrock folder. For example:
– Project folder: ~/Sites/Development/DO-example.com
– Bedrock folder: ~/Sites/Development/DO-example.com/example.com
– URL: https://example.com