前言

本文介绍使用Docker+Nginx+Jenkins将Hexo博客部署至云服务器。

hexo博客提交云服务器流程:

  1. 本地电脑Hexo三连,或者Githubaction三连部署到Github。
  2. Jenkins监测到github项目主页更新,拉取更新后的文件到VPS的Jenkins工作目录。Jenkins(概念篇):Jenkins 简介_ron jenkins 什么意思
  3. 把Jenkins的工作目录映射到Nginx站点目录

开始之前确保以下几个条件:

  • 可以部署到Githubpage的hexo博客;

  • VPS云服务器;

  • VPS安装好Docker,Docker-compose

  • 安装Git

1
sudo apt-get install git

安装Jenkins和Nginx

  • 创建目录和文件
1
2
3
4
5
cd /root/data/docker_data/jenkins
touch docker-compose.yml
mkdir -p jenkins_home nginx/{html/hexo,conf.d,logs,cache}
touch nginx/nginx.conf
touch nginx/conf.d/hexo.conf

文件结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
├── docker-compose.yml
├── jenkins_home
│   ├── com.cloudbees.hudson.plugins.folder.config.AbstractFolderConfiguration.xml
│   ├── config.xml
│   ├── copy_reference_file.log
│   ├── credentials.xml
│   ├── fingerprints
│   ├── github-plugin-configuration.xml
│   ├── hudson.model.UpdateCenter.xml
│   ├── hudson.plugins.build_timeout.global.GlobalTimeOutConfiguration.xml
│   ├── hudson.plugins.build_timeout.operations.BuildStepOperation.xml
└── nginx
├── cache
├── conf.d
├── html
├── logs
└── nginx.conf
  • docker-compose.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
version: '3'
services:
docker_jenkins:
user: root
restart: always
image: jenkins/jenkins:lts
container_name: jenkins
ports:
- 8888:8080
volumes:
- ./jenkins_home/:/var/jenkins_home
- ./nginx/html/hexo:/var/jenkins_home/workspace/blog

docker_nginx_dev:
restart: always
image: nginx
container_name: nginx_dev
ports:
- 8871:8001
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf # 主配置文件
- ./nginx/conf.d:/etc/nginx/conf.d # 站点配置
- .nginx/html:/usr/share/nginx/html # 静态资源
- ./nginx/logs:/var/log/nginx # 日志目录
- ./nginx/cache:/var/cache/nginx # 缓存目录

  • Nginx主配置文件:Nginx.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
user  nginx;
worker_processes 1;

error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

events {
worker_connections 1024;
}

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;

sendfile on;
keepalive_timeout 65;
gzip on;

# 引入 conf.d 目录中的站点配置
include /etc/nginx/conf.d/*.conf;
}
  • Hexo站点配置文件:hexo.conf
1
2
3
4
5
6
7
8
9
10
server {
listen 8001;
server_name localhost;

location / {
root /usr/share/nginx/html/hexo;
index index.html;
try_files $uri $uri/ /index.html;
}
}
  • 运行容器
1
docker-compose up -d
  • 验证nginx

nginx/html/hexo目录下新建index.html,文件内容如下

1
2
3
4
5
6
7
8
9
10
11
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>welcome to Nginx</h1>
</body>
</html>
  • 浏览器访问服务器ip:8871

返回页面

配置Jenkins

  • 浏览器输入服务器ip:8888

根据提示到jenkins_home目录找管理员密码来登陆

后面的安装和配置建议看从零开始搭建JENKINS+GITHUB持续集成环境

这篇文章看Jenkins的使用验证构建。主要步骤看建议的文章,下面列出有出入的地方

  • 新建任务时,任务名填blog。

任务名

  • 启用github插件,github plugin虽然默认安装了,但是并没有启用。需要取消启用github-branch这个插件,再开启github plugin。这两个插件只能启用一个,所以需要取消一个。

github插件

后续配置看从零开始搭建JENKINS+GITHUB持续集成环境

版本原因会有所不同,但大致意思是能看出的

配置github凭据

验证凭据

构建测试

项目配置

参考资料

从零开始搭建JENKINS+GITHUB持续集成环境

Docker Compose安装部署Jenkins

Jenkins修改显示语言为中文显示(亲测有效)_jenkins 中文-CSDN博客

Jenkins配置任务时无 send files execute commands over SSH

Docker + Jenkins + Nginx实现前端自动化部署