Hi everyone!
I made a bash script for updating all of my production sites. Maybe you guys will find it useful.
Most importantly though, I’d love to get feedback on how to improve it and/or any downsides you foresee to using this method.
FYI: my clients don’t have the requirement of their sites having 99% uptime: ie. it’s ok for their site to go down for a day (not often) if it saves me time on workflow
Usage
Update all production sites: ./update-prod.sh -a
Update specific sites: ./update-prod.sh -s site1 site2 site3
Notes
The usage for site1 depends heavily on your folder names. All my sites for this example are .com. The parameters site1 site2 etc. are all the root domain, minus the suffix. My folder structure is consistent with this. For example:
- Project folder: ~/Sites/Development/DO-example
- Bedrock folder: ~/Sites/Development/DO-example/example
- URL: https://example.com
To work in all scenarios, you would need to pass in the full domain name and appropriately name your project folder and bedrock folder. For this you would adjust the script slightly. For example:
- Project folder: ~/Sites/Development/DO-example.com
- Bedrock folder: ~/Sites/Development/DO-example.com/example.com
- URL: https://example.com
If I have a staging site, I add an if statement inside the update script. For example:
if [[ "$SITE" = "example" ]]; then
./bin/deploy.sh production staging."$SITE".com
else
./bin/deploy.sh production "$SITE".com
fi
Full Script
#!/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