Docker 持续部署hexo blog到github page上和云虚拟机上

目标:

1.通过Dockerfile创建新的image

2.通过image创建容器去运行jenkins

3.启动nginx 容器挂载hexo blog

4.通过jenkins同时部署blog到github和nginx

5.push 新创建的image到docker hub

步骤:

1.创建一个目录dockerhexo

2.创建一个新目录名为Docker_file,创建Dockerfile:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#jenkins稳定版本
FROM jenkins/jenkins:lts
#切换root用户
USER root
RUN apt-get update \
#安装jsnode12.x版本
&& curl -sL https://deb.nodesource.com/setup_12.x | bash - \
&& apt-get install -y nodejs \
#安装hexo
&& npm install -g hexo-cli \
#安装hexo-deployer
&& npm install hexo-deployer-git
#切换回jenkins用户
User jenkins

2.运行 docker build生成image:

1
2
3
docker build -t zxlman/jenkinshexo .
#如果 需要no cache build image如下:
docker build --no-cache -t jenkinshexo .

3.查看新生成的image:

4.创建一个shellscript用来删除对应的容器,方便测试,容器名jenkinscontainerremove.sh

1
2
3
4
5
6
echo "Stopping container myjenkins"
docker container stop myjenkins
echo "Removing container myjenkins "
docker container rm myjenkins
echo ""Doker container list:
docker container ls

执行 chmod +x enkinscontainerremove.sh 赋予执行权限,如果执行这个脚本就运行sh jenkinscontainerremove.sh

5.创建一个shell脚本启动jenkins容器,如下,`sh jenkinscontainerstartup.sh

1
2
3
4
5
6
7
docker container   run --name myjenkins   \
--restart unless-stopped \
-v /home/kevin/dockerhexo/jenkins_home:/var/jenkins_home \
-v /home/kevin/.ssh:/var/jenkins_home/.ssh \
-v /home/kevin/.gitconfig:/var/jenkins_home/.gitconfig \
-d -p 8081:8080 -p 50000:50000 zxlman/jenkinshexo
docker container ls

ps:

1
2
3
-v  /home/kevin/.ssh:/var/jenkins_home/.ssh \ #用来访问github拉取repository

-v /home/kevin/.gitconfig:/var/jenkins_home/.gitconfig #部署文章到github page需要用到

6.创建一个shell脚本启动nginx容器,如下,`sh nginxstartup.sh

1
2
3
4
docker container stop mynginx
docker container rm mynginx
docker run --name mynginx --restart unless-stopped -v /home/kevin/dockerhexo/jenkins_home/workspace/Blog_Deployment_Pipline/public:/usr/share/nginx/html -d -p 81:80 nginx
docker container ls

7.打开jenkins并且创建pipline job:job Blog_Deployment_Pipline

8.配置jenkins job:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
pipeline {
agent any

stages {
stage('Git CheckOut') {
steps {
git 'git@github.com:nightwish2016/HexoBlog.git'
}
}
stage('Build') {
steps {
sh label: '', script: 'hexo g'
}
}
stage('Deployment') {
steps {
sh label: '', script: 'hexo d'
}
}

}

}

7.Trigger job,但是发现报了如下的错误:

那是因为github上clone下来的代码缺少hexo相关的文件,执行如下脚本:

1
2
cd /home/kevin/dockerhexo/jenkins_home/workspace/Blog_Deployment_Pipline/
npm install hexo --save

重新trigger这个job

8.查看blog是否部署到github page和nginx web服务器,可以看到通过如下网址均可以访问blog,并且部署成功了

http://kevin.southeastasia.cloudapp.azure.com:81/

https://nightwish2016.github.io/

9.push这个image 到docker hub docker push zxlman/jenkinshexo https://hub.docker.com/repository/docker/zxlman/jenkinshexo

Tips:

Start containers automatically:

https://docs.docker.com/config/containers/start-containers-automatically/#:~:text=Docker%20provides%20restart%20policies%20to,started%20in%20the%20correct%20order

使用root用户进入jenkins:

docker exec -it –user root 69d /bin/bash

window 10 启动jenkins容器

1
docker container   run --name jenkinshexocontainer    --restart unless-stopped  -v /c/Users/xxx/jenkins_home2:/var/jenkins_home   -v /c/Users/xxx/.ssh:/var/jenkins_home/.ssh    -v  /c/Users/xxx/.gitconfig:/var/jenkins_home/.gitconfig  -d -p  8083:8080 jenkinshexo

Issues:

1
2
3
$ docker container   run --name myjenkins   --restart unless-stopped  -v /home/kevin/jenkins_home:/var/jenkins_home -v  /home/kevin/.ssh:/var/jenkins_home/.ssh   -p  82:8080 zxlman/jenkinshexo
touch: cannot touch '/var/jenkins_home/copy_reference_file.log': Permission denied
Can not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions?

Solution

jenkins的默认用户没有对/home/kevin/dockerhexo/jenkins_home的写权限

sudo chown 1000 /home/kevin/dockerhexo/jenkins_home