Skip to content

Instantly share code, notes, and snippets.

@jameswilson
Last active April 29, 2024 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jameswilson/4d89892ec1040f720b620fe30911e716 to your computer and use it in GitHub Desktop.
Save jameswilson/4d89892ec1040f720b620fe30911e716 to your computer and use it in GitHub Desktop.
Create a Drupal release
#!/usr/bin/env bash
###
### Description:
###
### Create and tag a release for deployment to the build system and
### remote hosting environment.
###
### Usage:
###
### ./scripts/create-release.sh [1.2.3]
###
### Specifying a release tag version number, like `1.2.3`, is optional;
### the script will let you know what the prior release tag was by
### reading from existing list of git tags.
###
### Notes:
###
### - You must define the name of the project's main branch (sometimes known
### as the `master`` branch on legacy projects) as well as the development
### branch (typically called "develop") where features and fixes are merged
### for initial integration testing.
###
### - You typicaly want to be on the `deveop`` branch when creating a release.
### You will get a warning to confirm when creating releases from any branch
### other than `develop`. This is sometimes necessary for hotfix releases.
###
### - Your must have a pristine working copy. This means that `git status`
### must report that there are no changes or untracked files.
###
### - A temporary branch `release-1.2.3` will be created and then merged to
### the project's main branch.
###
### - Finally, follow-up commands will be suggested for how to push the tag.
###
set -eu -o pipefail
function show_help() {
sed -n 's/^###//p' ${0}
}
function cancel() {
echo "Canceling. No release was created."
exit 0;
}
while :; do
case ${1:-} in
-h | -\? | --help)
show_help
exit
;;
-y|--yes)
SKIP_CONFIRMATION=true
;;
--) # End of all options.
shift
break
;;
-?*)
printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2
;;
*) # Default case: No more options, so break out of the loop.
break ;;
esac
shift
done
if [[ -n $(git status -s) ]]; then
echo "There are uncommitted changes in your local working tree. Please commit or stash them first."
cancel
fi
# Establish branch and tag name variables
currentBranch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
lastRelease=$(git describe --tags --abbrev=0)
devBranch=develop
masterBranch=main
if [ "$currentBranch" != "$devBranch" ]; then
echo "WARNING: You're not currently on the '${devBranch}' branch."
if [ "${SKIP_CONFIRMATION:-}" != "true" ]; then
echo "Create release from branch '${currentBranch}' anyway? (Y/n)"
read -n 1 -p " > " proceed
echo
if [[ "$proceed" =~ ^[Nn] ]]; then
cancel
fi
fi
fi
if [ $# -eq 0 ]; then
echo "Please specify a release tag in 'X.Y.Z' format:"
echo " > (The previous release was ${lastRelease})"
read -p " > " versionLabel
else
versionLabel=$1
fi
releaseBranch=release-$versionLabel
if [ "${SKIP_CONFIRMATION:-}" != "true" ]; then
echo "Create release tag ${versionLabel}? (Y/n) "
read -n 1 -p " > " proceed
echo
if [[ "$proceed" =~ ^[Nn] ]]; then
cancel
fi
fi
set -x
# Wrapper for git command to ensure hooks do not interfere.
git="git -c core.hooksPath=/dev/null"
# Create the release branch from the -develop branch.
$git checkout -b $releaseBranch $currentBranch
# Update version number in Drupal website.
versionFile="docroot/sites/default/settings.php"
sed -i.backup -E "s/[0-9]+\.[0-9]+\.[0-9]+/$versionLabel/" $versionFile
rm $versionFile.backup
# Update version number in custom modules.
for i in $(cd docroot/modules/custom && ls -d */); do
moduleName=${i%%/}
infoFile=docroot/modules/custom/${moduleName}/${moduleName}.info.yml
if [ -f ${infoFile} ]; then
sed -i.backup -E "s/(version: .+)/version: $versionLabel/" $infoFile $infoFile
rm $infoFile.backup
fi
done
# Update version number in Drupal Theme.
ddev theme version $versionLabel
sleep 10
# Commit version number increment.
$git commit -am "Bump to version $versionLabel"
# Merge release branch with the new version number into master.
$git checkout $masterBranch
$git merge --no-ff $releaseBranch
# Create tag for new version from master.
$git tag $versionLabel
# Merge release branch with the new version number back into develop.
$git checkout $devBranch
$git merge --ff $releaseBranch
# Remove release branch.
$git branch -d $releaseBranch
# Switch back to master branch to review and push the release.
$git checkout $masterBranch
set +x
echo ""
echo ""
echo " Release '$versionLabel' was created. You're now on the '$masterBranch'."
echo " Please review the changes and execute the following commands to trigger CI workflows:"
echo ""
echo " git push origin $versionLabel"
echo " git push origin $masterBranch"
echo " git checkout $devBranch"
echo " git merge $masterBranch"
echo " git push origin $devBranch"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment