Last active
March 4, 2021 17:56
-
-
Save giomurru/d916669fd31565ea31f42bf756c96c0e to your computer and use it in GitHub Desktop.
steps to deploy website through git
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# steps to deploy website through git | |
# login via ssh to your website | |
# create a folder where you want to store your bare repository | |
mkdir -p /home/username/gitrepos/myrepository.git | |
cd /home/username/gitrepos/myrepository.git | |
git init --bare | |
# modify the hook script | |
cd hooks | |
# create post-receive script | |
nano post-receive | |
# Inside the post-receive script you have to put everything you want to execute after the push is executed | |
# For example you may want to copy the repository content into your website published directory | |
# WARNING be sure to understand what the following script is doing before using it! The paths of the folder are just an example and you should adapt them to your situation. | |
# WARNING: this is just an example of how the script post-receive could be and I am not responsible for any loss of data you could encounter. Please be sure to understand the script before using it. | |
##### START OF example of post-receive ####### | |
#!/bin/sh | |
# The production dir | |
REPONAME="myrepository" | |
TARGET="/home/username/www/${REPONAME}" | |
# A temp directory for deployment | |
TMP="/home/username/srv_tmp" | |
TMP_DEPLOY="${TMP}/${REPONAME}" | |
# The git repo | |
REPO="/home/username/srv_git/${REPONAME}.git" | |
# Deploy the content to the temporary directory | |
mkdir -p $TMP_DEPLOY | |
git --work-tree=$TMP_DEPLOY --git-dir=$REPO checkout -f | |
# Then replace all the files in the target | |
rm -rf $TARGET | |
mv $TMP_DEPLOY $TARGET | |
# Clean the temporary folder | |
rm -rf $TMP | |
##### END OF example of post-receive ####### | |
# press ctrl+o and hit enter to save the script, close with ctrl+x | |
# make the script executable | |
chmod +x post-receive | |
#now back on the local machine you have to add a remote to your repository | |
git remote add deploy ssh://username@yourdomain.com:2222/home/username/gitrepos/myrepository.git | |
# WARNING: after you push the post-receive script will be executed. Be sure to understand what the script is doing before pushing. | |
# Now if you are super confident that your post-receive script is good to go, to deploy you can simply do a push like this: | |
git push deploy master |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment