1. 程式人生 > >Jenkins+Git+Gitlab+Ansible實現持續整合自動化部署靜態網站(一)--技術流ken

Jenkins+Git+Gitlab+Ansible實現持續整合自動化部署靜態網站(一)--技術流ken

 

前言

 

在之前已經寫了關於Git,Gitlab以及Ansible的兩篇部落格《Git+Gitlab+Ansible劇本實現一鍵部署Nginx--技術流ken》,《Git+Gitlab+Ansible劇本實現一鍵部署動態網站(二)--技術流ken》,以及關於jenkins的簡單使用《Jenkins持續整合介紹及外掛安裝版本更新演示(一)--技術流ken》。相信大家也已經完全掌握了這三項工具的使用,也可以使用這幾項工具可以部署靜態以及動態網站了。

以前的部落格可以實現一鍵部署網站了,但是並沒有實現持續化整合部署網站。沉重的工作還是落在了可憐的運維工程師上面。

但是你有沒有想過這樣一個問題,假如網站全部部署成功了,現在我們的開發程式設計師隔三差五的修改網站上的某些功能或者修改頁面內容,難道都需要運維人員再手動執行命令嗎?有沒有一種方法使得程式設計師修改完成程式碼之後可以自己測試、部署上線哪?

回答是有的!jenkins就可以完成上述的工作了。

本篇部落格將使用git+gitlab+ansible+jenkins實現真正的持續化一鍵部署靜態網站

 下一篇部落格將介紹如何使用git+gitlab+ansible+jenkins部署一套動態的網站。敬請期待。

 

Gitlab建立專案

 

第一步:gitlab的安裝即配置

請參考我之前的部落格《Gitlab在linux/windows中免密使用(二)--技術流ken

 

第二步:建立專案

如下圖,我建立了一個static_web的專案

Git下載倉庫

 

第一步:建立目錄並下載倉庫

[[email protected] ~]# mkdir /ken
[[email protected] ~]# cd /ken
[[email protected] ken]# git clone http://10.220.5.137/webg1/static_web.git
Cloning into 'static_web'...
Username for 'http://10.220.5.137': root
Password for 'http://[email protected]': 
remote: Counting objects: 3, done.
remote: Total 
3 (delta 0), reused 0 (delta 0) Unpacking objects: 100% (3/3), done.

 

Ansible劇本編寫

 

第一步:進入到上面下載下來的工作目錄中

[[email protected] ken]# ls
static_web
[[email protected] ken]# cd static_web/
[[email protected] static_web]# ls -a
.  ..  .git  README

 

第二步:使用roles來編寫劇本

首先需要建立相關的目錄

[[email protected] static_web]# mkdir roles/httpd/{tasks,vars,files} -p

 

第三步:編寫tasks檔案

[[email protected] static_web]# vim roles/httpd/tasks/main.yml
[[email protected] static_web]# cat roles/httpd/tasks/main.yml
- name: install httpd
  yum: name=httpd state=present
- name: start httpd
  service: name=httpd state=restarted
- name: copy test file to httpd
  copy: src=roles/httpd/files/index.html dest=/var/www/html

 

第四步:編寫file下的測試檔案

[[email protected] static_web]# vim roles/httpd/files/index.html
[[email protected] static_web]# cat roles/httpd/files/index.html
test for static web

 

第五步:編寫主機清單

[[email protected] static_web]# cat inventory/test
[ken]
10.220.5.138

 

第六步:編寫劇本檔案

[[email protected] static_web]# vim ken.yml
[[email protected] static_web]# cat ken.yml
- hosts: ken
  remote_user: root
  roles:
   - httpd

 

第七步:執行劇本

 注意:在執劇本的時候需要使用-i指定你建立的主機列表清單,否則會找不到需要執行的節點

可以看到劇本執行完成也沒有報錯

[[email protected] static_web]# ansible-playbook -i inventory/test ken.yml 

PLAY [ken] ***********************************************************************

TASK [Gathering Facts] ***********************************************************
ok: [10.220.5.138]

TASK [httpd : install httpd] *****************************************************
changed: [10.220.5.138]

TASK [httpd : start httpd] *******************************************************
changed: [10.220.5.138]

TASK [httpd : copy test file to httpd] *******************************************
changed: [10.220.5.138]

PLAY RECAP ***********************************************************************
10.220.5.138               : ok=4    changed=3    unreachable=0    failed=0   

 

 第八步:網頁瀏覽

現在就可以登入該節點進行訪問了

 

檔案提交至gitlab

 

經過上面的步驟之後,已經部署完成了一個靜態頁面

現在把這些檔案提交至gitlab

第一步:檔案提交至倉庫

[[email protected] static_web]# git add .
[[email protected] static_web]# git commit -m "v1"

 

第二步:報錯

提交報了這個錯

*** Please tell me who you are.

Run

  git config --global user.email "[email protected]"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got '[email protected](none)')

直接執行命令

[[email protected] static_web]# git config --global user.email "[email protected]"
[[email protected] static_web]# git config --global user.name "Your Name"

 

第三步:推送至gitlab

[[email protected] static_web]# git push
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

Username for 'http://10.220.5.137': root    ####輸入連線你的gitlab的使用者名稱
Password for 'http://[email protected]':    ###輸入該使用者密碼
Counting objects: 12, done.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (11/11), 815 bytes | 0 bytes/s, done.
Total 11 (delta 0), reused 0 (delta 0)
To http://10.220.5.137/webg1/static_web.git
   c47e814..e2ac703  master -> master

 

第四步:web端檢視結果

發現v1版已經上傳成功

 

jenkins 實現持續整合

 

經過上面的一些操作之後,我們已經完成了靜態網站的部署,已經程式碼的上傳

但是發現還是每次執行需要輸入命令等

現在我們就使用jenkins來實現持續化部署

 

第一步:jenkins中建立任務

我建立了一個自由風格的軟體專案

專案名稱為test_for_static_web

輸入完畢之後選擇確定即可

 

 第二步:新增原始碼管理資訊

這裡的url就是你的專案的地址

下面的憑證,點選Add輸入你的gitlab的密碼和賬號即可

 

 

第三步:選擇構建

增加侯建步驟選擇執行shell

 

第四步:編寫shell

第一行:指定bash為直譯器,和編寫shell一樣

第二行:進入到工作目錄,這裡引用了了一個變數,意思是工作目錄,因為我們的劇本里面都是相對路徑所以我們需要進入到git拉取下來的檔案中進行執行可以點選下面的可用環境列表瞭解更多

第三行: 即執行劇本

以上步驟完成之後點選下方的儲存即可

 

第五步:檢視執行結果

點選立即構建之後,在最下方會出現一個圓圈,這個圓圈紅色代表失敗,藍色代表成功,可以滑鼠放上去顯示的

第六步:檢視失敗原因

點選下方的紅色圓圈即可檢視失敗原因

這裡的失敗原因是因為執行jenkins程式的是jenkins使用者,我們連線節點的祕鑰是root的,所以現在連線不上

 

 第七步:更改jenkins的配置檔案

把執行jenkins的使用者更改為root即可

更改完成之後重啟jenkins

[[email protected] static_web]# sed -i 's/JENKINS_USER="jenkins"/JENKINS_USER="root"/' /etc/sysconfig/jenkins
[[email protected] static_web]# systemctl restart jenkins

 

第八步:再次執行構建

再次點選構建可以發現現在紅色圓圈變成了藍色的成功圓圈

點選一下這個成功的圓圈檢視執行過程

 

 第九步:檢視工作目錄

許多人會疑惑,在這裡使用的git clone,它把檔案下載到哪裡去了那?

其實是放在jenkins的工作目錄之下的你的專案名稱裡面了,還記得我們的專案名稱是test_for_static_web嗎?

[[email protected] static_web]# ls /var/lib/jenkins/workspace/
 test_for_static_web/ 

檢視一下這個目錄下面都有什麼

看到了吧,從gitlab克隆的回來的檔案都放在了這裡即jenkins工作目錄下>你的任務名稱面

[[email protected] static_web]# ls /var/lib/jenkins/workspace/test_for_static_web
inventory  ken.retry  ken.yml  README  roles

 

更改網站資料

 

現在我們的工程師就可以在他的電腦上面更改網站資料,並實現持久整合自動化部署了

第一步:克隆資料

現在我使用的電腦IP 為10.220.5.139,現在假如另外一位工程師的電腦是10.220.5.137上面

[[email protected] tmp]# mkdir p
[[email protected] tmp]# cd p
[[email protected] p]# git clone http://10.220.5.137/webg1/static_web.git
Cloning into 'static_web'...
Username for 'http://10.220.5.137': root
Password for 'http://[email protected]': 
remote: Counting objects: 14, done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 14 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (14/14), done.
[[email protected] p]# 

 

第二步:更改網站資料

[[email protected] static_web]# echo "this is new data for static web">> roles/httpd/files/index.html

 

第三步:提交

[[email protected] static_web]# git add .
[[email protected] static_web]# git commit -m "v2"
[master e5f5d42] v2
 1 file changed, 1 insertion(+)
[[email protected] static_web]# git push
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

Username for 'http://10.220.5.137': root
Password for 'http://[email protected]': 
Counting objects: 11, done.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 443 bytes | 0 bytes/s, done.
Total 6 (delta 1), reused 0 (delta 0)
To http://10.220.5.137/webg1/static_web.git
   e2ac703..e5f5d42  master -> master

 

第三步:jenkins持續化部署

點選構建

發現沒有報錯

 

第四步:網站檢視

發現網站資料已經更新