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 的全自動博客構建部署系統

相關文章
相關標籤/搜索