Len's Study-Log

集中一点,登峰造极!

0%

Hexo博客搭建2.0

因为之前的腾讯云服务器到期了,就又重新买了一个腾讯的轻量应用服务器,现在要重新再搭建一次博客,主要是服务器端的搭建。整个博客的写作发布流程还是和原来一样。

本地 Typora 写作,再由 Hexo 服务器编译生成静态文件,并通过 Git 上传 public 目录到 Gitee 上。Gitee 收到推送会通知 Jenkins 来拉数据到轻量应用服务器(通过 WebHook)。云服务器上 Nginx 作为 Web服务器 做请求转发,把对域名 lenqq.cn 的请求转换成访问 Hexo 博客首页。

Typora:这个软件目前要收费了,如果有需要,可以下载最后一个免费版本然后改一下弹窗的代码。具体可参考之前的文章 https://lenqq.cn/p/74c41cf7.html

PicGo:写作过程中需要用到图片的,就用ta上传到对应的图床中。PicGo 的 GitHub 主页:PicGo,作者博客:MARKSZのBlog

Hexo:安装和配置文档可以看 Hexo官网

Git:安装和配置方法自行百度。

安装 Nginx

看这里 https://lenqq.cn/p/NginxSetup.html

安装 Jenkins

看这里 https://lenqq.cn/p/JenkinsSetup.html

以上软件都安装完成并且确认正常运行之后,接下来就可以做相关的配置了。

配置

  1. 首先就是利用 Nginx 把域名映射博客首页了:

因为http请求已经重定向到https了,所以我们只要在监听 443 端口的 server 中加入如下 location:

1
2
3
location / {
root /opt/myproject/blog;
}
  1. 写一个脚本 blog_script.sh 放到 /opt/script/ 中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/bin/bash
#操作/项目路径
BASE_PATH=/opt/myproject/blog

SOURCE_PATH=/var/lib/jenkins/workspace/blog/hexo/public

DATE='date + %Y%m%d%H%M'

function transfer(){
echo "复制文件中 ..."
sudo rm -rf $BASE_PATH/*
sudo cp -r $SOURCE_PATH/. $BASE_PATH
echo "复制完成"

}
function run(){
transfer
}

run

  1. 到 Jenkins 控制台创建任务

    • 新建任务

  • 配置 Gitee 项目地址、Gitee 帐号密码

  • 配置钩子

  <u>**在 Gitee 项目中配置 WebHooks 的时候,输入上图显示的 URL,以及下图生成的 密码。**</u>

  ![](https://img.lenqq.cn/image-20221205012505531.png)
  • 增加构建步骤,执行 shell 脚本

  • 最后保存即可。

遇到问题:

1、这是忘记给 Jenkins 配置 Git 了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Running as SYSTEM
Building in workspace /var/lib/jenkins/workspace/blog
The recommended git tool is: NONE
using credential gitee_login
Cloning the remote Git repository
Cloning repository https://gitee.com/***/blog.git
> git init /var/lib/jenkins/workspace/blog # timeout=10
ERROR: Error cloning remote repo 'origin'
hudson.plugins.git.GitException: Could not init /var/lib/jenkins/workspace/blog
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$5.execute(CliGitAPIImpl.java:1049)
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$2.execute(CliGitAPIImpl.java:802)
at hudson.plugins.git.GitSCM.retrieveChanges(GitSCM.java:1225)
at hudson.plugins.git.GitSCM.checkout(GitSCM.java:1308)
at hudson.scm.SCM.checkout(SCM.java:540)
at hudson.model.AbstractProject.checkout(AbstractProject.java:1239)
at hudson.model.AbstractBuild$AbstractBuildExecution.defaultCheckout(AbstractBuild.java:647)
at jenkins.scm.SCMCheckoutStrategy.checkout(SCMCheckoutStrategy.java:85)
at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:519)
at hudson.model.Run.execute(Run.java:1899)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:44)
at hudson.model.ResourceController.execute(ResourceController.java:107)
at hudson.model.Executor.run(Executor.java:449)

2、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
First time build. Skipping changelog.
[blog] $ /bin/sh -xe /tmp/jenkins14858128988888610579.sh
+ cd /opt/script/
+ sudo ./blog_script.sh

We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.

sudo: no tty present and no askpass program specified
Build step 'Execute shell' marked build as failure
Finished: FAILURE

Jenkins 执行脚本的时候,是以帐号 jenkins 去执行的。具体的用命令 cat /etc/passwd 看看。

上面的报错是说jenkins这个帐号没有权限执行该脚本。我们加上就行了:

  1. sudo visudo
  2. 在文件的末尾加上一行:jenkins ALL=(ALL) NOPASSWD: /opt/script/blog_script.sh
  3. 重启Jenkins服务:/etc/init.d/jenkins restart