使用AppVeyor持续集成博客

建立AppVeyor帐号

进入AppVeyor官网,游客会跳转到 /login 页,这里能够注册,也可使用 GitHub 帐号受权登录。node

建立CI项目

/projects 页面选择你的博客源码仓库git

配置CI项目

点击项目中 SETTINGS 选项卡,若是项目分支不是默认的,修改 Default branchgithub

再点击 Environment 栏目,设置4个环境变量:npm

name value
STATIC_SITE_REPO 静态页面的仓库地址
TARGET_BRANCH 编译后文件存放的分支
GIT_USER_EMAIL Github用户邮箱
GIT_USER_NAME Github用户名

设置好后点击 Save 保存。hexo

获取AccessToken

打开 GitHub 我的设置app

点击 Developer settings 栏目,再点击 Personal access tokens 选项卡,能够看到已有的Token,这里点击 Generate new token 按钮建立一个博客专用的token。ui

能够参考官方文档加密

加密AccessToken

因为这个AccessToken能够直接操做你的仓库的,并且配置文件是公开的,因此这时就要求对AccessToken进行加密。可到AppVeyor Token加密页面进行加密。把加密后的字符串填入下一步中的配置文件里。spa

配置CI

在项目中新建 appveyor.yml 文件,用于配置持续集成的命令3d

clone_depth: 5

environment:
    access_token:
        secure: # 本身的加密token
install:
    - ps: Install-Product node 6.9 # 默认node版本太老
    - node --version
    - npm --version
    - npm install
    - npm install hexo-cli -g
    
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
    - git commit -m "Update Static Site"
    - git push origin %TARGET_BRANCH%
- appveyor AddMessage "Static Site Updated"

大体的意思是从github仓库的当前分支拉取下来,编译成静态文件后,在push到目标分支。因为AppVeyor环境中是经过Access Token访问咱们的仓库的,而Hexo自带的部署则在访问的过程当中须要咱们输入账号密码,因此 Hexo g -d 的命令就不适合在这里使用。须要先编译成静态文件,再把public文件夹的静态文件push到目标分支。

完成

相关文章
相关标签/搜索