已經(jīng)把Jenkins+Gitlab自動(dòng)構(gòu)建,
以及Jenkins+Ansible-playbook完成了,現(xiàn)在在做一個(gè)完整的流程
安裝配置篇見: http://www.reibang.com/p/4923348ef3da
現(xiàn)在要做的是Gitlab + Jenkins + Ansible 自動(dòng)部署
計(jì)劃是 Gitlab管理源代碼,每次更新push代碼觸發(fā)Jenkins自動(dòng)構(gòu)建,
自動(dòng)構(gòu)建過程包括一個(gè)Ansible-playbook用來將代碼部署到客戶機(jī)的nginx的網(wǎng)頁目錄.
Gitlab,Jenkins,Ansible部署在10.241.0.1,
Nginx部署在10.241.0.2
客戶機(jī)安裝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
#修改默認(rèn)的index.html文件內(nèi)容
[root@client ~]# echo 'nginx default index html file' > /usr/share/nginx/html/index.html
#到j(luò)enkins主機(jī)訪問客戶機(jī)的首頁文件
[root@deploy code_html]# curl http://10.241.0.2
nginx default index html file
新建一個(gè)Gitlab倉庫
image
到j(luò)enkins的安裝機(jī)初始化項(xiàng)目
#初始化項(xiàng)目
[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
測(cè)試流程
1\. 訪問client主機(jī),查看index.html內(nèi)容
2\. 修改Jenkins主機(jī)上的Gitlab倉庫中index.html的內(nèi)容
3\. git push,更新文件到gitlab
4\. 查看jenkins是否有自動(dòng)構(gòu)建操作
5\. 訪問client主機(jī),查看index.html內(nèi)容是否有變動(dòng)
#訪問client,查看index.html內(nèi)容
[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,可以看到文件內(nèi)容已經(jīng)發(fā)生變化,
[root@deploy code_html]# curl http://10.241.0.2
Gitlab+Jenkins+Ansible-playboot update index html test
可以看到已經(jīng)觸發(fā)了自動(dòng)構(gòu)建
image
image
結(jié)束語
這就非常奶思了,總體來說實(shí)驗(yàn)是成功的