摘要

今天上班的时候想,我要不在服务器部署Git服务器,然后直接部署博客,不是更方便吗?

没想到啊没想到,居然花了三四个小时!

正文

Hexo使用Git部署到Windows,分为三步

  1. 安装Git服务器
  2. 配置Hexo部署方式
  3. 配置Git自动部署

一、安装Git服务器

本文使用的是Gitblit。

参考文章

  1. Windows平台下搭建自己的Git服务器
  2. Windows搭建Git服务器

点击进入Gitblit官网下载,下载解压之后,找到defaults.properties,修改server.httpPort为你想设置的端口。

双击gitblit.cmd启动Git服务器。

1.png

2.png

至于登录账号和密码,在data目录下的users.conf查看

如果想要将gitblit添加到服务中,可以通过执行安装路径下的installService.cmd

shell
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
@REM Install Gitblit as a Windows service.

@REM gitblitw.exe (prunmgr.exe) is a GUI application for monitoring 
@REM and configuring the Gitblit procrun service.
@REM
@REM By default this tool launches the service properties dialog
@REM but it also has some other very useful functionality.
@REM
@REM http://commons.apache.org/daemon/procrun.html

@REM arch = x86, amd64, or ia32
SET ARCH=amd64

@REM Be careful not to introduce trailing whitespace after the ^ characters.
@REM Use ; or # to separate values in the --StartParams parameter.
"%CD%\%ARCH%\gitblit.exe"  //IS//gitblit ^
		 --DisplayName="gitblit" ^
		 --Description="a pure Java Git solution" ^
		 --Startup=auto ^
		 --LogPath="%CD%\logs" ^
		 --LogLevel=INFO ^
		 --LogPrefix=gitblit ^
		 --StdOutput=auto ^
		 --StdError=auto ^
		 --StartPath="%CD%" ^
		 --StartClass=com.gitblit.GitBlitServer ^
		 --StartMethod=main ^
		 --StartParams="--storePassword;gitblit;--baseFolder;%CD%\data" ^
		 --StartMode=jvm ^
		 --StopPath="%CD%" ^
		 --StopClass=com.gitblit.GitBlitServer ^
		 --StopMethod=main ^
		 --StopParams="--stop;--baseFolder;%CD%\data" ^
		 --StopMode=jvm ^
		 --Classpath="%CD%\gitblit.jar;%CD%\ext\*" ^
		 --Jvm=auto ^
		 --JvmMx=1024
		 

二、配置Hexo部署方式

参考文章

  1. One-Command Deployment

编辑_config.yml

yaml
1
2
3
deploy:
  type: git
  repo: http://admin:password@mygit.com/r/blog.git

repo的链接是需要自己去获取的,如图。

3.png

如果不想要输入密码,就在http上面admin@mygit.com改成admin:password@mygit.com即可

运行命令,一键生成并部署

shell
1
hexo g && hexo d

我自己搞了个更方便的直接在桌面创建.bat脚本

shell
1
2
3
4
D:
cd D:\Develop\www\hexoBlog\blog
hexo g&&hexo d
pause

pause加不加都行,不加就是执行完了自动关闭窗口而已。

三、配置Git自动部署

参考文章

  1. 关于git的pre-receive与post-receive

首先在gitblit的安装目录的data下,找到groovy,复制一份localclone.groovy出来重命名为auto-deploy.groovy。修改其中的rootFolder路径

groovy
1
2
3
4
5
6
7
8
// Indicate we have started the script
logger.info("localclone hook triggered by ${user.username} for ${repository.name}")

def rootFolder = 'D:/Develop/youarebitch' //修改成你的项目路径
def bare = false
def cloneAllBranches = true
def cloneBranch = 'refs/heads/master'
def includeSubmodules = true

进入gitblit网站,点击blog仓库,点击右侧编辑,点击receive。

在post-receive脚本下,将刚复制出来的auto-deploy.groovy添加到selected,如图

4.png

如此,在push(hexo d)到git完毕之后,会自动调用auto-deploy.groovy脚本,克隆仓库!