deploy 2.83 KB
Newer Older
1
#!/bin/sh
2 3 4 5 6 7 8 9 10 11

#
# Deploys content changes to production.
# (Auto-generates tag label and pushes tags to live.)
#
# (Make sure you have commited your changes and saved them to production using the ./save script first.)
#

set -e

12 13 14 15 16 17 18 19
# Aborts the script and decrements the deployment counter
abortGracefully(){
	currentDayDeploymentCount=$(( currentDayDeploymentCount - 1 ))
	echo "$currentDayDeploymentCount" > "$counterFilePath"

	exit 1
}

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
dateStamp=$(date "+%Y%m%d")

counterFolderPath="/tmp/better-content"
counterFilePath="${counterFolderPath}/$dateStamp"

#
# Set up the counter, if necessary.
#
mkdir -p "$counterFolderPath"

if ! [ -f "$counterFilePath" ]; then
	# Counter does not exist for this day, create it.
	echo 0 > "$counterFilePath"
fi

#
# Increment the counter.
#
currentDayDeploymentCount=$(( $(cat "$counterFilePath") + 1 ))

# Persist the counter.
echo "$currentDayDeploymentCount" > "$counterFilePath"

# Format the counter (padded to three spaces).
printf -v formattedCurrentDayDeploymentCount "%03d" $currentDayDeploymentCount

# Create the git tag label
gitTagLabel="${dateStamp}-${formattedCurrentDayDeploymentCount}"

49
echo "\nTagging: ${gitTagLabel}"
50

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
#
# Create a default tag message based on the last commit message.
#

tempTagMessageDirectory=/tmp/better-content/
tempTagMessageFilePath="${tempTagMessageDirectory}/tag-commit-message.txt"

mkdir -p tempTagMessageDirectory

# Not sure if there’s an easier way to get this.
gitDefaultEditor=$(git config -l | grep core.editor | perl -pe 's|^core.editor=||')

lastCommitMessage=$(git log -1 --oneline | perl -pe 's|^.*? ||')
echo "$lastCommitMessage" > "$tempTagMessageFilePath"

# Write out some instructions (so that it also resembles what people normally
# see when git opens up the editor. With an additional line of instructions on
# how to abort the tag.)
echo "\n#\n# Write a message for tag:\n#\t${gitTagLabel}\n# Lines starting with '#' will be ignored.\n#\n# If you’re happy with the default message above, just close this document.\n#\n# To abort deployment, delete all content and save the empty document." >>  "$tempTagMessageFilePath"

# Create and open a temporary file to hold the message to pass to git tag later.
$gitDefaultEditor "$tempTagMessageFilePath"

emptyStringMD5=$(md5 -q -s '')
tagMessageMD5=$(md5 -q "$tempTagMessageFilePath")

# Check for empty tag message (this signals that the deployment should be aborted).
if [ "$emptyStringMD5" = "$tagMessageMD5" ]; then
	# Person deleted the tag message, abort the tag.
	echo "\nAborting deployment due to empty tag message.\n"
Aral Balkan's avatar
Aral Balkan committed
81
	abortGracefully
82
else
83
	echo ''
84 85 86
	cat "$tempTagMessageFilePath"
fi

87
# Create the git tag
88
git tag -s "$gitTagLabel" --file="$tempTagMessageFilePath"
89

90
echo "Deploying…\n"
91

92 93 94 95
# Push the git tags
git push live --tags

echo "\nDeployment complete.\n\nSee https://www.better.fyi and https://data.better.fyi for changes.\n"