基于docker+gitlab的博客CI/CD的搭建
定义
首先,什么是CI/CD
持续集成(CI-Continuous integration)是指多名开发者在开发不同功能代码的过程当中,可以频繁的将代码行合并到一起并切相互不影响工作;
持续部署(CD-continuous deployment)是基于某种工具或平台实现代码自动化的构建、测试和部署到线上环境以实现交付高质量的产品,持续部署在某种程度上代表了一个开发团队的更新迭代速率;
持续交付(Continuous Delivery)是在持续部署的基础之上,将产品交付到线上环境,因此持续交付是产品价值的一种交付,是产品价值的一种盈利的实现。
部署
首先,创建一个新的gitlab容器和 runner
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 28 29 30 31 32 33 34 35 36 37 38
| docker run \ -itd \ -p 222:22 \ -p 80:80 \ -p 443:443 \ --name gitlab \ --hostname gitlab \ --restart always \ --privileged=true \ -v /home/puff/Desktop/gitlab/etc:/etc/gitlab \ -v /home/puff/Desktop/gitlab/log:/var/log/gitlab \ -v /home/puff/Desktop/gitlab/data:/var/opt/gitlab \ gitlab/gitlab-ce
# docker exec -it gitlab bash # vim /etc/gitlab/gitlab.rb EXTERNAL_URL="https://your_ip" # gitlab-ctl reconfigure # gitlab-ctl restart # 这一步是配置访问gitlab的url
sudo docker run -d --name gitlab-runner --restart always \ -v /srv/gitlab-runner/config:/etc/gitlab-runner \ -v /var/run/docker.sock:/var/run/docker.sock \ gitlab/gitlab-runner:latest # 运行runner容器
docker run --rm -v /srv/gitlab-runner/config:/etc/gitlab-runner gitlab/gitlab-runner register \ --non-interactive \ --executor "docker" \ --docker-image alpine:latest \ --url "http://your_url/" \ --registration-token "token" \ --description "second-register-runner" \ --tag-list "blog_runner" \ --run-untagged="true" \ --locked="false" \ --access-level="not_protected"
|
梳理一下本地hexo步骤
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 28 29 30
| sudo apt-get install nodejs sudo apt-get install npm
npm install -g hexo-cli
# 进入博客的根目录 hexo init MyBlogFile
# 进入这个myblog文件夹 cd MyBlogFile npm install
hexo g hexo server # Hexo is running at http://localhost:4000/ . Press Ctrl+C to stop
git config --global user.name "yourname" git config --global user.email "youremail"
# 修改_config.yml deploy: type: git repo: https://token@github.com/zoulicheng/zoulicheng.github.io.git branch: master
npm install hexo-deployer-git --save hexo clean hexo generate hexo deploy # 成功,访问zoulicheng.github.io.git即可看到博客
|
确认服务器本地有gitlab和runner服务后,接下来,新建一个博客根目录my_blog,在里面新建一个dockerfile,并写一个新的.gitlab-ci.yml,并在本地生成初始的hexo文件夹MyBlogFile,最后对git进行初始化
Dockerfile如下
1 2 3 4 5 6 7 8 9 10 11 12
| #基础镜像信息 FROM alpine
#维护者信息 MAINTAINER puff_zlc
#镜像操作指令 RUN apk add --no-cache nodejs npm RUN npm install -g hexo-cli
# 暴露端口 EXPOSE 4000
|
.gitlab-ci.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 27 28 29 30 31 32 33 34 35
| stages: - compile - build - deploy
compile: stage: compile image: puffblog:v1.0 only: changes: - MyBlogFile/* script: - apk add git - git config --global user.name "xxxx" - git config --global user.email "xxxx@qq.com" - cd MyBlogFile - npm install - npm install hexo-deployer-git --save - hexo generate - hexo deploy
build: stage: build image: docker script: - docker info - docker build -f ./Dockerfile -t user/puffblog:v1.0 . - docker login -u user -p password - docker push user/puffblog:v1.0 deploy: stage: deploy script: - echo "Application deploy complete!"
|
现在,每当在本地的my_blog进行git push后;
gitlab服务器上的runner都会下载最新的puffblog:v1.0环境镜像用于运行hexo的编译,然后将编译好的静态博客页面上传到GitHub Page上;
接着runner自动将此次运行的最新的环境镜像puffblog:v1.0上传到dockerHub;
完成博客的一键更新以及持续流水线CI/CD。
至此,博客网站的CI/CD部署完毕。