前言
git 是一个分布式版本控制系统,我们可以在本机、Linux云主机、github等上进行对软件开发版本的控制,而且我们即使离线也是可以继续编码工作,比SVN更加方便安全。
github 是基于git的网页版仓库托管系统,它在版本的基础上,增加了分布式仓库、备份、评论和博客等更多功能。但是github是公开在公网的,也有类似的开源产品如gitlab和gitee。
gitbook 最早是为了软件开发文档编写方便的编辑器,其实和所谓git 、github没多大相似,但是它方便的编辑和美观的markdown形式,可以做很多文字内容的编辑工作, 结合git并放入github仓库,可以带来更好的写文体验。
jenkins 是开源的持续集成的软件,可以在编辑代码后自动部署到服务器,集成测试等,让我们的编写工作更加快捷方便。
这里只是简单了解,更具体使用可以参考各个相关项目资料
这里我们要基于gitbook、git、Jenkins搭建快捷的文档编写发布流程。废话少说,开始吧
搭建gitlab
在服务器拉取gitlab镜像,并启动
[root@master ~]# docker run --detach --hostname gitlab.bill.com --name gitlab --publish 443:443 --publish 8080:80 --publish 12222:22 --name gitlab.bill.com --restart always --volume ~/gitlab/config:/etc/gitlab --volume ~/gitlab/logs:/var/log/gitlab --volume ~/gitlab/data:/var/opt/gitlab gitlab/gitlab-ce:latest
因为部署在本地,又指定了gitlab.bill.com作为域名,所以在/etc/hosts配置下,这样可以通过域名访问gitlab。
127.0.0.1 gitlab.bill.com
初次登录可以修改密码,或者重新注册一个用户,我注册了一新用户devops,并在gitlab中创建了一个demo项目
<meta charset="utf-8">
从后台修改密码
[root@master ~]# docker exec -it gitlab.bill.com /bin/bash
root@gitlab:/# gitlab-rails console production
Loading production environment (Rails 4.2.8)
irb(main):001:0> user = User.where(id: 1).first
=> #<User id:1 @root>
irb(main):002:0> user.password=12345678
=> 12345678
irb(main):003:0> user.password_confirmation=12345678
=> 12345678
irb(main):004:0> user.save!
Enqueued ActionMailer::DeliveryJob (Job ID: ea8d4a43-05ef-4a05-819f-cafe8b9186e9) to Sidekiq(mailers) with arguments: "DeviseMailer", "password_change", "deliver_now", gid://gitlab/User/1
=> true
irb(main):005:0> quit
创建了一个空项目后,然后根据github上的提示,将本地的git项目推送到gitlab
Git global setup
git config --global user.name "devops"
git config --global user.email "459952507@qq.com"
Create a new repository
git clone http://gitlab.bill.com/devops/demo.git
cd demo
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master
在windows创建密钥 创建ssh的密钥,然后上传到gitlab
右键进入git bash
把代码复制到目录,并上传
wangyaohui@wangyaohui-HP MINGW32 /g/Projects/vuejs/demo (master)
$ git add .
wangyaohui@wangyaohui-HP MINGW32 /g/Projects/vuejs/demo (master)
$ git commit -m "demo init"
[master (root-commit) cf8eab3] demo init
3 files changed, 40 insertions(+)
create mode 100644 test.css
create mode 100644 test.html
create mode 100644 test.js
wangyaohui@wangyaohui-HP MINGW32 /g/Projects/vuejs/demo (master)
$ git push
Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 784 bytes | 0 bytes/s, done.
Total 5 (delta 0), reused 0 (delta 0)
To http://120.24.197.36:8080/devops/demo.git
* [new branch] master -> master
上传完后Gitlab项目如下:
这里可以选用已有的github的项目,带有一个单元测试和集成测试
https://github.com/onedaywillcome1/ContinuousIntegrationAndContinuousDeliveryApp.git
搭建jenkins
使用docker 下载jenkins镜像 jenkinsci/jenkins,并启动
[root@master ~]# docker run -d -p 8081:8080 -p 50000:50000 --name jenkins --link gitlab.bill.com:gitlab.bill.com -u root -v ~/jenkins:/var/jenkins_home -v /usr/local/java:/usr/local/jdk -v /usr/local/maven3:/usr/local/maven jenkinsci/jenkins:latest
7173ebd2ee9f291bafeb7a81a216e090b4a645263a4fa6faedbacd5be7ada282
8080端口是jenkins的端口,5000端口是master和slave通信端口(没错,jenkins可以部署集群,在本次中没有配置)
并将宿主机maven和jdk映射到对应的容器目录上,同时通过配置--link连接gitlab,因为要从gitlab下代码。
初次启动的时候,可以通过docker logs -f jenkins查看控制台的密码,通过这个密码登录系统,执行创建用户等操作
安装插件
启动项目后,下载所需的插件:系统管理-管理插件-可选插件(尤其要下载Git Plugin和Gitlab Hook Plugin插件以及Build Authorization Token Root Plugin),
1、点击 系统管理 :
2、点击 管理插件 :
3、点击 可选插件 选择你需要安装的插件
生成随机token:
root@278f1927ea8d:/# openssl rand -hex 12
5e97dbfdd159ff88f7b6afef
配置项目
新建一个项目,输入项目名称Books-test ,
创建项目触发器:
项目-配置-构建触发器:
得到url地址 http://www.haleyprince.com:8081/project/Books-test
然后编写设置代码发送时候自动推送的脚步,也就是简单的程序让系统自动做了些平时打包,复制上传等功能。
3、在gitlab上添加钩子
钩子就像钓鱼的钩子意思,鱼来了通知你去做拉的动作,你来了新变更行动,就会自动触发钩子然后去做通知Jenkins做推送动作。
添加成功后,点击此webhook后面的test进行测试
第一次测试webhook会报错,提示先提交一次,字面意思,就先push一次
先提交个文件
wangyaohui@wangyaohui-HP MINGW32 /g/Projects
$ git clone http://devops:87654321@120.24.197.36:8080/devops/Books-test.git
Cloning into 'Books-test'...
warning: You appear to have cloned an empty repository.
Checking connectivity... done.
wangyaohui@wangyaohui-HP MINGW32 /g/Projects
$ cd Books-test/
wangyaohui@wangyaohui-HP MINGW32 /g/Projects/Books-test (master)
$ touch README.md
wangyaohui@wangyaohui-HP MINGW32 /g/Projects/Books-test (master)
$ git add README.md
wangyaohui@wangyaohui-HP MINGW32 /g/Projects/Books-test (master)
$ git commit -m 'add readme'
[master (root-commit) aa817f1] add readme
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 README.md
wangyaohui@wangyaohui-HP MINGW32 /g/Projects/Books-test (master)
$ git push http://devops:87654321@120.24.197.36:8080/devops/Books-test.git
Counting objects: 3, done.
Writing objects: 100% (3/3), 212 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To http://120.24.197.36:8080/devops/Books-test.git
* [new branch] master -> master
测试
系统管理 -> 系统设置 -> 去掉 Enable authentication for ‘/project’ end-point
编写README.md 加入test gitbook push
wangyaohui@wangyaohui-HP MINGW32 /g/Projects/Books-test (master)
$ vim README.md
wangyaohui@wangyaohui-HP MINGW32 /g/Projects/Books-test (master)
$ git add README.md
warning: LF will be replaced by CRLF in README.md.
The file will have its original line endings in your working directory.
wangyaohui@wangyaohui-HP MINGW32 /g/Projects/Books-test (master)
$ git commit -m 'gitbook test'
[master 9c15aa5] gitbook test
1 file changed, 1 insertion(+)
$ git push http://devops:87654321@120.24.197.36:8080/devops/Books-test.git
Counting objects: 3, done.
Writing objects: 100% (3/3), 255 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To http://120.24.197.36:8080/devops/Books-test.git
aa817f1..9c15aa5 master -> master
配置git
再在gitlab测试
root@278f1927ea8d:/# ls /var/jenkins_home/workspace/Books-test/
README.md
现在jenkins有提交了
用nginx查看
在本机编写增加 gitbook 的SUMMARY.md 并构建gitbook
wangyaohui@wangyaohui-HP MINGW32 /g/Projects/Books-test (master)
$ gitbook build
info: 7 plugins are installed
info: 6 explicitly listed
info: loading plugin "highlight"... OK
info: loading plugin "search"... OK
info: loading plugin "lunr"... OK
info: loading plugin "sharing"... OK
info: loading plugin "fontsettings"... OK
info: loading plugin "theme-default"... OK
info: found 1 pages
info: found 0 asset files
info: >> generation finished with success in 11.6s !
wangyaohui@wangyaohui-HP MINGW32 /g/Projects/Books-test (master)
$ git add _book
wangyaohui@wangyaohui-HP MINGW32 /g/Projects/Books-test (master)
$ git add SUMMARY.md
wangyaohui@wangyaohui-HP MINGW32 /g/Projects/Books-test (master)
$ git commit -m 'build book'
[master a9b56bc] build book
wangyaohui@wangyaohui-HP MINGW32 /g/Projects/Books-test (master)
$ git push http://devops:87654321@120.24.197.36:8080/devops/Books-test.git
Counting objects: 36, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (33/33), done.
Writing objects: 100% (36/36), 624.69 KiB | 0 bytes/s, done.
Total 36 (delta 1), reused 0 (delta 0)
To http://120.24.197.36:8080/devops/Books-test.git
9c15aa5..a9b56bc master -> master
push后可以在网页打开了