Using Git to Deploy Your Codes and Manage Your Site
Since I use hexo as my primary static blog platform, I have tried to using git to deploy my site. And finally, after getting it down, I kept it down for in case.
目录
Also there is a option in config.yml
to let you deploy your blog to github or your personal server via git.
Setup for Our Remote Server
There is a directory named hooks
in a repository, which does something after receiving or before receiving a push or other operation, and that is what we need.
First, we initialize a bare git repository under our home:
mkdir -p ~/web/git/amito/{remote,deploy}
cd ~/web/git/amito/remote && git init --bare
After initializing the repo, we need to something more. Creating a bash file named post-receive
under ~/web/git/amito/remote
and set executable flag to it.
#!/bin/bash
deploy_dir="/home/amito/web/git/amito/deploy"
GIT_WORK_TREE=$deploy_dir git checkout -f master
echo "DEPLOY: master copied to $deploy_dir"
hexo_dir="/home/amito/web/amito/"
cd $hexo_dir
ln -sf $deploy_dir/themes $hexo_dir/
ln -sf $deploy_dir/source $hexo_dir/
ln -sf $deploy_dir/scaffolds $hexo_dir/
ln -sf $deploy_dir/_config.yml $hexo_dir/
hexo --silent g
if [[$? == 0]]; then
echo "Congratulations! Your blog has been correctly deployed"
else
echo "Unfortunately your blog is not been deployed correctly"
fi
Something about the bash file:
- The
deploy_dir
contains all codes needed for our hexo project. - The
hexo_dir
is where our real hexo project is.
Under Our hexo directory ~/web/amito/
, we will initialize a default hexo project,
cd ~/web/amito && hexo init && npm install --save
rm -rf themes source scaffolds _config.yml
After first push, you might delete those ln
lines in post-receive
.
Setup for Our Local Machine
On our local machine, we shall create a local repo to deal with our codes and a hexo project for writing.
mkdir -p ~/git/amtio/amito && mkdir -p ~/git/amito/hexo
### initialize our git repo
cd ~/git/amito/amito/ && git init
### add the remote git repo to our local repo named amito
git remote add amito ssh://amito@example.com:/home/amito/web/git/amito/remote/\
### initialize our hexo project
cd ../hexo && hexo init && npm install --save
### do some linking things
mv _conf.yml source themes scaffolds ../amtio/
ln -sf ../amito/_config.yml .
ln -sf ../amito/source .
ln -sf ../amito/scaffolds .
ln -sf ../amito/themes .
And it should be very clear for these commands beyond. Then we can push our codes to our remote server,
cd ~/git/amito/amito
git add . && git commit -m "first init"
git push amito master
If using public key to log in, it will be simple to push our codes.
If everything right, you will see something like this:
#### Congratulations! Your blog has been correctly deployed
And so far, we have done most things. The remaining we need to do is just writing our post and publish it. Cheers!


