用了hexo很久, 最近想要不止在自己的电脑上写博客, 每个电脑都能写。 然而hexo不支持, 所以想到了用jenkins配hexo。 当然jenkins可不是专门干这个的, jenkins可还有大用处。 我自己配置的时候遇见了很多坑, 如果遇见了一样的, 可以参考这篇帖子。 希望给你帮助。

hexo配置 Link to heading

由于我不太想要把自己的帖子source都暴露在网上, 所以我用了一个private repo。所有就有了如下的配置图

配置图 Link to heading


hexo/source >------
            commit|
                  |------> jenkins
                    -----< hook && logic
                   |
                   |
hexo/public <-------
             update

jenkins配置 Link to heading

我把jenkins放在了aws上面。 之前很久就想要用ec2, 但是总是没有机会。 通过这次机会, 一并用了。我参考了这个文件 当然, 折腾也开始了。

host name is not ec2-user Link to heading

不要用ssh去ec2-user@xxxxxx.amazon.com, amazon不会创建ec2-user给你,用要用ubuntu

关于 .pem key Link to heading

如果你用的是windows, 很不幸, 一旦文件在window目录下存储过, 那么他的permision就是错的, 你大概会遇到 ... permission denied (public key) ... 这种报错。 尝试在linux下打开, 然后用一下命令 sudo chmod 400 <your_pem_key>。至于linux环境有很多种解决方法。 我现在用的是windows subsystem for linux (WSL)。

关于安装jenkins在aws Link to heading

aws ec2就是一个linux环境,如果你安装的环境不是这文件上的环境, 你可以直接在网上搜索安装方法。 我用的ubuntu, 所以直接按照ubuntu的方法安装了。

创建 jenkins job Link to heading

  1. 创建一个freestyle job
  2. 写一段这个任务的description
  3. 勾选 GitHub project, 填写project url, 但是好像写不写都行。
  4. Source Code Management -> Git -> Repository URL。这里可以填https的clone地址,也可以填ssh的clone地址。我填写的是https的地址。
  5. Credentials 添加github的用户名密码。git的用户名写在Username栏里, git的密码写在Password栏里。 Description 栏里写一些有意义的comments
  6. 勾选 Build Triggers -> GitHub hook trigger for GITScm polling
  7. Build -> Execute Shell

创建 webhook Link to heading

进入存储hexo/source的repo, 选Settings -> Webhooks -> Add Webhooks

Payload URL 填的是 <jenkins_addr>/github-webhook。 注意slash后面一定要填github-webhook

Execute Shell 内容 Link to heading

这个内容的思路就是一个生成hexo public folder然后push到git上的过程。这个需要一点一点试。以下是我的bash script,仅供参考

sudo su ubuntu

cd /home/ubuntu

rm <hexo_source_repo_folder> -r

git clone https://{username}:{password}@github.com/<hexo_source_repo>.git

cp -r <hexo_source_repo_folder>/* <hexo_project_on_ec2>

cd <hexo_project_on_ec2>

hexo d -g

exit

为啥要换用户成ubuntu Link to heading

当jenkins执行scripts的时候会以 user jenkins 执行。这个user是jenkins自动创建的,属于nogroup, 所以执行啥啥都没有权限。 那我就不用这个user。

另一种方法是把jenkins用户加入root组别里面。

hexo g -d permission denied Link to heading

本来做到这,我都觉得已经做完了, 谁知道执行hexo g -d 失败!原因是linux管当前user要验证, 但是jenkins可是不会给你手动输入的。 用ssh-keygen生成一个key给ec2 linux, 部署到hexo public repo里面。

happy blogging~