Gitlab + Jenkins + Ansible 自動部署

已經(jīng)把Jenkins+Gitlab自動構(gòu)建,
以及Jenkins+Ansible-playbook完成了,現(xiàn)在在做一個完整的流程
安裝配置篇見: http://www.itdecent.cn/p/4923348ef3da

現(xiàn)在要做的是Gitlab + Jenkins + Ansible 自動部署
計劃是 Gitlab管理源代碼,每次更新push代碼觸發(fā)Jenkins自動構(gòu)建,
自動構(gòu)建過程包括一個Ansible-playbook用來將代碼部署到客戶機的nginx的網(wǎng)頁目錄.

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文件內(nèi)容
[root@client ~]# echo 'nginx default index html file' > /usr/share/nginx/html/index.html 

#到j(luò)enkins主機訪問客戶機的首頁文件
[root@deploy code_html]# curl http://10.241.0.2
nginx default index html file

新建一個Gitlab倉庫

image

到j(luò)enkins的安裝機初始化項目

#初始化項目
[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內(nèi)容
2\. 修改Jenkins主機上的Gitlab倉庫中index.html的內(nèi)容
3\. git push,更新文件到gitlab
4\. 查看jenkins是否有自動構(gòu)建操作
5\. 訪問client主機,查看index.html內(nèi)容是否有變動

#訪問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ā)了自動構(gòu)建

image

查看自動構(gòu)建的控制臺輸出

image

結(jié)束語

這就非常奶思了,總體來說實驗是成功的

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容