Gitlab + Jenkins + Ansible 自动部署
2018-10-09 本文已影响1005人
baiyongjie
已经把Jenkins+Gitlab自动构建,
以及Jenkins+Ansible-playbook完成了,现在在做一个完整的流程
安装配置篇见: https://www.jianshu.com/p/4923348ef3da
现在要做的是Gitlab + Jenkins + Ansible 自动部署
计划是 Gitlab管理源代码,每次更新push代码触发Jenkins自动构建,
自动构建过程包括一个Ansible-playbook用来将代码部署到客户机的nginx的网页目录.
Gitlab,Jenkins,Ansible部署在10.241.0.1,
Nginx部署在10.241.0.2
客户机安装nginx
[root@client ~]# yum -y install nginx
[root@client ~]# systemctl start nginx
[root@client ~]# netstat -nplt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 9072/nginx: master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 683/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 769/master
tcp6 0 0 :::80 :::* LISTEN 9072/nginx: master
tcp6 0 0 :::22 :::* LISTEN 683/sshd
tcp6 0 0 ::1:25 :::* LISTEN 769/master
#修改默认的index.html文件内容
[root@client ~]# echo 'nginx default index html file' > /usr/share/nginx/html/index.html
#到jenkins主机访问客户机的首页文件
[root@deploy code_html]# curl http://10.241.0.2
nginx default index html file
新建一个Gitlab仓库
5b6578ba0a4be.png到jenkins的安装机初始化项目
#初始化项目
[root@deploy ~]# mkdir -p /data/update_nginx_html
[root@deploy ~]# cd /data/update_nginx_html/
[root@deploy update_nginx_html]# git clone ssh://git@101.89.82.106:59888/root/update_nginx_html.git code_html
Cloning into 'code_html'...
warning: You appear to have cloned an empty repository.
[root@deploy update_nginx_html]# cd code_html/
[root@deploy code_html]# echo "this is test jenkins+gitlab+ansible-playboos html file" > index.html
[root@deploy code_html]# git add .
[root@deploy code_html]# git commit -m 'new html file'
[master (root-commit) b61e9d5] new html file
1 file changed, 1 insertion(+)
create mode 100644 index.html
[root@deploy code_html]# git push origin master
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 268 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To ssh://git@101.89.82.106:59888/root/update_nginx_html.git
* [new branch] master -> master
#编写更新html文件的Ansible-playbook
[root@deploy code_html]# cd /data/Ansible-playbook/
[root@deploy Ansible-playbook]# vim update_nginx_html.yml
---
- hosts: client
gather_facts: no
tasks:
- name: update_nginx_index_html_file
copy: src=/data/update_nginx_html/code_html/index.html dest=/usr/share/nginx/html/index.html
配置Jenkins
image配置Gitlab web钩子
image测试流程
1. 访问client主机,查看index.html内容
2. 修改Jenkins主机上的Gitlab仓库中index.html的内容
3. git push,更新文件到gitlab
4. 查看jenkins是否有自动构建操作
5. 访问client主机,查看index.html内容是否有变动
#访问client,查看index.html内容
[root@deploy ~]# curl http://10.241.0.2
nginx default index html file
#更新index.html文件
[root@deploy ~]# cd /data/update_nginx_html/code_html/
[root@deploy code_html]# echo "Gitlab+Jenkins+Ansible-playboot update index html test" > index.html
[root@deploy code_html]# git add .
[root@deploy code_html]# git commit -m 'auto update index html'
[master 801cbf3] auto update index html
1 file changed, 1 insertion(+), 1 deletion(-)
[root@deploy code_html]# git push origin master
Counting objects: 5, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 304 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To ssh://git@101.89.82.106:59888/root/update_nginx_html.git
b61e9d5..801cbf3 master -> master
#再次访问client,可以看到文件内容已经发生变化,
[root@deploy code_html]# curl http://10.241.0.2
Gitlab+Jenkins+Ansible-playboot update index html test
image
image
结束语
这就非常奶思了,总体来说实验是成功的
接下来就可以去利用Jenkins+Ansible-playbook去做很多事情了,笔芯.