Skip to content

Instantly share code, notes, and snippets.

@Typiqally
Created September 1, 2021 10:30
Show Gist options
  • Save Typiqally/30c56e0d08bcfe3d99a01d48757636a0 to your computer and use it in GitHub Desktop.
Save Typiqally/30c56e0d08bcfe3d99a01d48757636a0 to your computer and use it in GitHub Desktop.
Git server deployment post-receive hook
#!/bin/sh
set -eu
NAME="maastelecom"
SERVICE="kestrel-$NAME"
GIT_DIRECTORY="/home/deploy/$NAME.git"
WORK_TREE="/home/deploy/$NAME"
OUTPUT_DIRECTORY="/var/www/$NAME"
BRANCH="master"
# Prepare the work tree
prepare() {
echo "Creating work tree"
mkdir $WORK_TREE
echo "Checking out $BRANCH branch to work tree..."
git --work-tree=$WORK_TREE --git-dir=$GIT_DIRECTORY checkout -f $BRANCH
}
# Publish the source code and restart the service
publish() {
echo "Publishing to $OUTPUT_DIRECTORY..."
dotnet publish $WORK_TREE -o $OUTPUT_DIRECTORY
echo "Restarting $SERVICE..."
sudo /bin/systemctl restart $SERVICE
}
# Cleanup the work tree
clean() {
echo "Cleaning up..."
rm -r -f $WORK_TREE
}
deploy() {
prepare && publish && clean
echo "Deployment finished"
}
while read oldrev newrev ref
do
echo "Reference $ref received."
if [ "$ref" = "refs/heads/$BRANCH" ];
then
deploy
else
echo "Doing nothing, only the $BRANCH branch will be deployed on this server."
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment