hexo使用jenkins自动部署到阿里云

hexo使用jenkins自动部署到阿里云

本地安装hexo

npm install hexo-cli -g
hexo init blog
cd blog
npm install
hexo server
复制代码

使用github pages服务部署hexo

咱们用来托管博客的服务叫作 Github Pages,它是 Github 用来提供给我的/组织或者项目的网页服务,只须要部署到你的 Github Repository,推送代码,即可以实时呈现。php

首先,你须要有一个 Github 的帐号。而后建立一个名称为 .github.io 的仓库来托管网页便可。css

以个人 Github 为例,个人用户名是 dumingcode,因此建立一个名为 dumingcode.github.io 的仓库,建立的仓库地址即是:https://github.com/dumingcode/dumingcode.github.io.git 建立完后,咱们能够暂时不用管它,不须要往仓库里面 push 任何的东西。html

hexo部署配置

接着,咱们来配置一下本地的 Hexo。java

在博客的根目录下有一个名为 _config.yml 的文件,这是博客的主配置文件。前面的其余部分咱们先不理会,后文再谈,咱们先看最后的 Deployment 配置项:nginx

# Deployment
## Docs: https://hexo.io/docs/deployment.html
deploy:
  type:
复制代码

根据官方的文档显示,如今 Hexo 支持 Git、Heroku、Rsync、OpenShift、FTPSync 等部署方式,咱们选择 Git 来部署的话,须要首先安装 hexo-deployer-git 插件: cnpm install hexo-deployer-git --save而后编辑上面的配置文件:git

deploy:
  type: git
  repo: <repository url>
  branch: [branch]
  message: [message]
复制代码

咱们须要把刚才建立的仓库地址添加进来,branch 和 message 项能够不填,默认状况下推送到 master 分支,这里我建议使用 SSH 加密的仓库地址(参看 Github 官方文档配置 SSH 免密操做)。github

保存配置文件以后,咱们在博客的跟目录键入: hexo g -d即可以把博客部署到 Github 了。如今,全部人均可以经过 http://.github.io 来访问本身的博客。web

hexo使用第三方模板

找了半天发现hexo-theme-BlueLake主题很简洁,因而使用下面的命令安装(进入blog根目录执行)。npm

git clone https://github.com/chaooo/hexo-theme-BlueLake.git themes/BlueLake
cnpm install hexo-renderer-jade@0.3.0 --save
cnpm install hexo-renderer-stylus --save
复制代码

本人搭建好的github我的主页:dumingcode.github.io/,欢迎访问。segmentfault

hexo部署到阿里云

虽然说利用github pages服务可以对外发布博客,可是做为一个码农仍是但愿有本身的域名博客,可是我比较懒,不想手动发布博客。我想自动化地既发布到github也能同时发布到我的网站。因此决定采用CICD的方法,CICD工具使用开源的jenkins,jenkins也搭建在阿里云我的服务器上。

下载并运行jenkins

注意端口使用的是8081

mkdir /usr/local/jenkins
wget http://mirrors.jenkins.io/war-stable/latest/jenkins.war
nohup java -jar jenkins.war --ajp13Port=-1 --httpPort=8081 &
复制代码

安装nginx

  1. 安装nginx依赖
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
wget http://nginx.org/download/nginx-1.13.10.tar.gz
tar xvf nginx-1.13.10.tar.gz
./configure --prefix=/usr/local/nginx
make
make install
cd /usr/local/nginx/sbin
./nginx -s reload
nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or direc
须要设置nginx.conf
./nginx -c /usr/local/nginx/conf/nginx.conf
复制代码
  1. nginx.conf配置
upstream jenkins {
    server 127.0.0.1:8081;
    keepalive 64;
}
server {
       listen       80;
       server_name  jenkins.buyasset.com;
       client_max_body_size 60M;
       client_body_buffer_size 512k;
       location / {
              port_in_redirect on;
              proxy_pass              http://jenkins/;
              proxy_set_header        Host $host:$server_port;
              proxy_set_header        X-Real-IP $remote_addr;
              proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
              proxy_set_header        X-Forwarded-Proto $scheme;
      }

    }

复制代码

以上经过nginx 反向代理jenkins,在浏览器输入jenkins.buyasset.club就可以进入jenkins管理后台。

配置jenkins

在jenkins页面提示目录中找到默认密码,输入jenkis域名,登录jenkins。

image

安装jenkins社区推荐的插件

image

image

配置github

获取sercret text

登录github网站,进入 github->Settings->Developer settings-> Generate new token,点击生成完毕必定记录下下面的secret text。 secret text必定要记住,忘记的话只能从新生成。

GitHub webhooks 设置

进入GitHub上指定的项目(hexo 仓库) --> setting --> WebHooks&Services --> add webhook --> 输入刚刚部署jenkins的服务器的IP

webhook
图片中标红区域是变化的,后缀都是同样的为github-webhook。

jenkins中的github配置

配置GitHub Plugin

系统管理 --> 系统设置 --> GitHub --> Add GitHub Sever

github server
API URL 输入 https://api.github.com,Credentials点击Add添加,Kind选择Secret Text,具体以下图所示。
image
设置完成后,点击TestConnection,提示 Credentials verified for user UUserName, rate limit: xxx,则代表有效。

建立一个freestyle任务

  1. General 设置
    填写GitHub project URL, 也就是你的项目主页 eg. https://github.com/your_name/your_repo_name
    general
  2. 配置源码管理
    源码管理
  3. 构建触发器,构建环境
    image
  4. 构建
    构建
  5. 构建脚本 将上图的构建脚本替换以下:
cd /var/www/blog(hexo目录)
git pull
hexo clean
hexo g -d
复制代码
  1. 构建后操做

    构建后操做

  2. 构建前clone hexo
    将hexo初始代码拉取到/var/www/blog目录中,之后jenkins会监控github的push操做,一旦发现push会自动更新。 cd /var/www git clone https://github.com/dumingcode/dumingcode.github.io.git blog

nginx反向代理hexo

hexo为静态网站,因此直接用nginx反向代理便可,nginx脚本以下:注意root指向的是hexo部署目录。

server
{
    listen 80;
    server_name blog.buyasset.club;
    index index.html index.htm index.php default.html default.htm default.php;
    root  /var/www/blog;  

    #error_page 404 /404.html;
    location ~ .*\.(ico|gif|jpg|jpeg|png|bmp|swf)$
    {
        access_log   off;
        expires      1d;
    }

    location ~ .*\.(js|css|txt|xml)?$
    {
        access_log   off;
        expires      12h;
    }

    location / {
        try_files $uri $uri/ =404;
    }

}

复制代码

测试CICD效果

进入本地hexo目录,修改发布的博客,而后执行hexo g -d,登录jenkins发现jenkins已经获取到了push操做,而且执行了自动构建任务。如下为jenkins的变动记录

Site updated: 2018-04-21 13:35:51 (commit: 76f3c53) (details)
Commit 76f3c530d077782fd66a8ca375afaa17cd188286 by duming
Site updated: 2018-04-21 13:35:51
 (commit: 76f3c53)
复制代码

参考连接

手把手教你搭建Jenkins+Github持续集成环境
Jenkins+Github持续集成
Jenkins最佳实践 hexo自动部署
基于 Hexo 的全自动博客构建部署系统

相关文章
相关标签/搜索