美文网首页Docker容器Hexo程序员
你没见过的 Hexo 全自动部署教程【改进版】

你没见过的 Hexo 全自动部署教程【改进版】

作者: 左蓝 | 来源:发表于2017-02-19 23:38 被阅读2379次

    昨天睡觉之前觉得当天的 Hexo 文章很“滑稽”,为了实现同步居然跑去调用 webhook,于是一大早(上午11 点)就起来把昨天的 Dockerfile 和脚本改了一遍。去掉 webhook,全部使用 shell 搞定,并且把监听脚本移入容器中执行。

    今天增加的功能

    • 全部在 Docker 中完成,没有外部命令。
    • 更自动化的构建,即时显示变动。
    • 构建失败时,不会发布。
    • 更方便安装 Hexo 插件。
    • 允许用户自定义构建完成后执行的命令。

    准备实现的功能包括自动压缩图片和静态资源文件。

    本文镜像可以运行在本地,也可以运行在你的服务器,反正都能实现自动部署。建议部署到服务器。

    首先安装 Docker 和 Compose

    首先安装 Docker:

    curl -sSL https://get.docker.com/ | sh
    

    然后安装 Compose:

    curl -L https://github.com/docker/compose/releases/download/1.10.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
    chmod +x /usr/local/bin/docker-compose
    

    找一个自己喜欢的主题

    在这里找主题:https://hexo.io/themes/
    然后下载到本地或者你的服务器。

    一个文件搞定

    新建一个文件夹hexo,在hexo文件夹里面新建两个文件夹:source 和 themes,然后在 source 里面新建 _posts 文件夹,这里面存放你要发布的文章。

    把你喜欢的主题解压到 themes 目录下面。

    目录结构如下:

    .
    ├── _config.yml
    ├── docker-compose.yml
    ├── source
    │ └── _posts
    │     └── test.md
    └── themes
        └── anatole
            ├── _config.yml
            ├── ... ... ...
             ... ... ...
    
    12 directories, 37 files
    

    其中 _config.yml 文件的模板可以通过下面方式获取:

    docker run -d --name tmp zuolan/hexo-alpine
    docker cp tmp:/hexo/_config.yml .
    docker rm -f tmp
    

    这样当前文件夹就会有一份 _config.yml 模板了。

    接下来最后一个文件,新建一个文件叫做:
    docker-compose.yml
    内容如下:

    version: '2'
    services:
      hexo:
        container_name: hexo_build
        image: zuolan/hexo-alpine
        restart: always
        volumes:
          - ./_config.yml:/hexo/_config.yml:ro
          - ./source:/hexo/source:ro
          - ./themes:/hexo/themes:ro
          - /root/.ssh:/root/.ssh
          - /root/.gitconfig:/root/.gitconfig
          - /root/public:/hexo/public
        ports:
          - "4000:4000"
      nginx:
        container_name: hexo_web
        image: nginx:alpine
        restart: always
        volumes:
          - ~/public:/usr/share/nginx/html:ro
        ports:
          - "4001:80"
    

    万事俱备,然后启动:

    dokcer-compose up -d
    

    查看博客构建状态:

    $ docker logs -f hexo_build
    正在执行初次构建:
    ================================================================
    INFO  Start processing
    INFO  Files loaded in 1.42 s
    INFO  Generated: atom.xml
    INFO  Generated: sitemap.xml
    INFO  Generated: test/index.html
    INFO  Generated: archives/index.html
    INFO  Generated: categories/技术/index.html
    INFO  Generated: categories/技术/Node-js/index.html
    INFO  Generated: index.html
    INFO  Generated: tags/Hexo/index.html
    INFO  Generated: tags/博客/index.html
    INFO  Generated: css/style.scss
    INFO  Generated: images/favicon.png
    INFO  Generated: images/logo.png
    INFO  Generated: images/logo@2x.png
    INFO  Generated: fonts/fontawesome-webfont.woff
    INFO  Generated: fonts/fontawesome-webfont.ttf
    INFO  Generated: css/blog_basic.css
    INFO  Generated: css/style.css
    INFO  Generated: js/daovoice.js
    INFO  Generated: js/jquery-migrate-1.2.1.min.js
    INFO  Generated: js/jquery.appear.js
    INFO  Generated: css/font-awesome.min.css
    INFO  Generated: fonts/fontawesome-webfont.eot
    INFO  Generated: fonts/fontawesome-webfont.svg
    INFO  Generated: js/jquery.js
    INFO  24 files generated in 140 ms
    
    
    
    页面构建完成,正在发布到 Github:
    ================================================================
    Hi izuolan/izuolan.github.io! You've successfully authenticated, but GitHub does not provide shell access.
    
    
    
    页面已经发布,容器进入监视状态。
    

    在这个镜像中已经集成了 Git 发布插件,只需要填写 _config.yml 即可。

    安装插件

    新建一个文件,名为 Dockerfile:

    FROM zuolan/hexo-alpine
    RUN npm install --save 插件名称
    RUN npm install --save 插件名称
    
    CMD ["/monitor.sh"]
    

    构建新的镜像:

    docker build -t user/hexo .
    

    然后修改 Compose 配置文件中的镜像名称,启动即可。


    最近临近开学好忙啊,高薪招远程文案兼职会有人感兴趣吗?开个玩笑。这个系列还没完,明天继续,大概还有两篇吧。

    相关文章

      网友评论

        本文标题:你没见过的 Hexo 全自动部署教程【改进版】

        本文链接:https://www.haomeiwen.com/subject/ufedwttx.html