基于github pages + hexo的blog(二)博客源码同步

一,上传到github

  • 在你的github上建立博客源码目录,比如:yourname.github.io.source
  • 将之前本地的hexo源码上传到该目录下
  • 如果有使用第三方主题的
    • 将之前安装的主题目录删除
    • 将需要安装的主题fork到自己的github上,方便自己修改配置
    • 使用 git submodule add 代替 git clone
    • 此时会自动生成一个.gitmodules文件
  • 提交上述修改

二,修改主题配置

  • 直接在 themes/yourthemes 目录下修改,并直接提交推送到你自己的仓库中

三,如何使用

  • 已拉取过该项目的机子:
    • git pull 拉取更新
    • git submodule update 拉取子模块更新
  • 未拉取过该项目的机子:
    • git clone 源码项目
    • git submodule update –init 初始化子模块
  • 安装依赖包,开启本地调试,写文章等操作。

四,如何持续化集成发布文章

市面上提供持续化集成的网站很多,此处选择使用AppVeyor

  • 先注册一个AppVeyor账号,可以直接使用github账号
  • 在github中生成一个token: 点击头像 -> setting -> Developer settings -> Personal access tokens -> Generate new token 选择条件生成即可。记住生成的token值。
  • 在AppVeyor中 点击 settings -> Encrypt YAML 将GitHub的token再加密一次,获得最终的token(该token用于最后的appveyor.yml文件)。
  • 新建一个project,直接关联到github中博客源码项目
  • 配置该project的环境变量(点击该project最右边的设置按钮,选择Enviroment)分别为:
    • GIT_USER_EMAIL 你的github邮箱
    • GIT_USER_NAME 你的github账号名称
    • STATIC_SITE_REPO 你的github博客项目(不是源码项目)地址
    • TARGET_BRANCH 你的github博客项目(不是源码项目)主要分支
  • 在博客源码添加appveyor.yml文件,文件内容如最后所示
  • 将appveyor.yml提交至github即可。
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
appveyor.yml脚本内容:

clone_depth: 10
environment:
access_token:
secure: 刚才的token
install:
- npm install
- npm install -g hexo-cli
- git submodule update --init
build_script:
- hexo generate
artifacts:
- path: public
on_success:
- git config --global credential.helper store
- ps: Add-Content "$env:USERPROFILE\.git-credentials" "https://$($env:access_token):x-oauth-basic@github.com`n"
- git config --global user.email "%GIT_USER_EMAIL%"
- git config --global user.name "%GIT_USER_NAME%"
- git clone --depth 5 -q --branch=%TARGET_BRANCH% %STATIC_SITE_REPO% %TEMP%\static-site
- cd %TEMP%\static-site
- del * /f /q
- for /d %%p IN (*) do rmdir "%%p" /s /q
- SETLOCAL EnableDelayedExpansion & robocopy "%APPVEYOR_BUILD_FOLDER%\public" "%TEMP%\static-site" /e & IF !ERRORLEVEL! EQU 1 (exit 0) ELSE (IF !ERRORLEVEL! EQU 3 (exit 0) ELSE (exit 1))
- git add -A
- if "%APPVEYOR_REPO_BRANCH%"=="master" if not defined APPVEYOR_PULL_REQUEST_NUMBER (git diff --quiet --exit-code --cached || git commit -m "Update Static Site" && git push origin %TARGET_BRANCH% && appveyor AddMessage "Static Site Updated")

参考文章

https://www.jianshu.com/p/5abacac4133c
https://formulahendry.github.io/2016/12/04/hexo-ci/
https://blog.csdn.net/u011475210/article/details/79023429