DevOps-Gitlab分支管理、代碼合並及實現develop與master分支代碼部署


 代碼自動部署流程 

 gitlab 新建 develop 分支

gitlab 定義分支名稱並創建

由於之前授予了laomao和laoxiao用戶對各自項目的Developer權限,所以只擁有代碼的更新權限,而並沒有代碼的合並權限。下面模擬分支develop的代碼更新,經測試后合並入master分支

clone linux39的develop分支代碼並更新重新上傳

報錯key校驗失敗
[root@master metrics]# git clone -b develop https://github.com/kubernetes-sigs/metrics-server.git
重新上傳公鑰到gitlab,上傳私鑰到jenkins [root@jenkins ~]# cat ~/.ssh/ authorized_keys id_rsa id_rsa.pub known_hosts [root@jenkins ~]# cat ~/.ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc2FFKRcZ13SgU/zNQUcJvP+5a4Rt8KYVPU9DCCl root@jenkins [root@jenkins ~]# cat ~/.ssh/id_rsa -----BEGIN RSA PRIVATE KEY----- MIIEpQIBAAKCAQEAwng9wP5drQoCJT7X6g1L3QICRu0wXCk4pJNQAUws9L8Ei6le M87lm8b7OnugJAxQ4sH93TaoOCh25iaO7mCWafqy/k3KHKf4iU291fgBAMHPXmEg zPg+0Demd+Ag8fOkC/Qx5JC8qls+1FSoRGDw6YodSAlz0vl+d8YfRd1SdqX9visD uVmah0w1H/B5ngnMCuw4/NEw1d1RpRsSlvsrvTwI4OOImMqsBIvMPFo= -----END RSA PRIVATE KEY-----
[root@jenkins ~]# git clone -b develop git@159.138.5.152:linux39/web1.git //重新clone Cloning into 'web1'... The authenticity of host '159.138.5.152 (159.138.5.152)' can't be established. ECDSA key fingerprint is SHA256:aq/XMS7sSqpxsDdUaS1+kFzeYWNxoc8mdK5+CCoj+Yg. ECDSA key fingerprint is MD5:79:2b:4c:44:ec:66:e1:d1:d9:00:1b:5d:a7:33:cc:b9. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '159.138.5.152' (ECDSA) to the list of known hosts. remote: Enumerating objects: 3, done. remote: Counting objects: 100% (3/3), done. remote: Total 3 (delta 0), reused 0 (delta 0) Receiving objects: 100% (3/3), done. [root@jenkins ~]# ll total 4 drwxr-xr-x 3 root root 4096 Dec 27 22:50 web1 [root@jenkins ~]# cd web1/ [root@jenkins web1]# ll total 4 -rw-r--r-- 1 root root 37 Dec 27 22:50 index.html [root@jenkins web1]# cat index.html <h1>this is linux39 test page v1</h1>

[root@jenkins web1]# echo "<h1>this is linux39 test page v2</h1>" >> index.html [root@jenkins web1]# cat index.html <h1>this is linux39 test page v1</h1><h1>this is linux39 test page v2</h1> //這里發現居然追加到了末尾

  ]# echo '<h1>this is linux39 test page v3</h1>' >> index.html    //使用單引號發現追加完成
  [root@jenkins web1]# cat index.html
  <h1>this is linux39 test page v1</h1>
  <h1>this is linux39 test page v2</h1>
  <h1>this is linux39 test page v3</h1>

[root@jenkins web1]# git add .
[root@jenkins web1]# git status
# On branch develop
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   index.html
#
[root@jenkins web1]# git commit -m "develop v2"
[develop c07a3ea] develop v2
 1 file changed, 2 insertions(+), 1 deletion(-)
[root@jenkins web1]# 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)

Counting objects: 5, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 278 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote:
remote: To create a merge request for develop, visit:
remote:   http://159.138.5.152/linux39/web1/merge_requests/new?merge_request%5Bsource_branch%5D=develop
remote:
To git@159.138.5.152:linux39/web1.git
   b8aeb8d..c07a3ea  develop -> develop

進入gitlab查看已經更新完成

測試如何將灰度環境的代碼和master的代碼部署到不同的服務器

web1 master

web2 develop

jenkins創建工程

linux38-web1-develop

分支選擇 */develop

#cd /var/lib/jenkins/workspace/linux39-web1   //使用jenkins拉取代碼默認工作路徑,當項目名稱發生變化,對應的代碼工作目錄會發生變化
cd /var/lib/jenkins/workspace/linux38-web1-develop

 tar -zcvf myapp.tar.gz ./*

 scp myapp.tar.gz www@web2:/data/tomcat/tomcat_appdir

 ssh www@web2 "bash /opt/apps/tomcat.sh stop"

 ssh www@web2 "cd /data/tomcat/tomcat_appdir ; rm -rf /data/tomcat/tomcat_webapps/myapp/* ; tar -xvf /data/tomcat/tomcat_appdir/myapp.tar.gz -C /data/tomcat/tomcat_webapps/myapp"

 ssh www@web2 "bash /opt/apps/tomcat.sh start"

構建完成測試訪問,發現已經部署完成

[root@jenkins workspace]# curl http://192.168.0.79:8080/myapp/index.html
<h1>this is linux39 test page v1</h1>
<h1>this is linux39 test page v2</h1>

 在服務器上如何查看項目配置

]# cat  /var/lib/jenkins/jobs/linux38-web1-develop/config.xml

當develop分支代碼測試沒問題后,將代碼部署到現網環境,這里為web1服務器

 使用laomao用戶提交合並develop分支代碼到master的申請,由項目主管審批

jenkins新建master分支部署項目,當代碼分支合並完成后部署到現網web1 tomcat服務器

項目名稱: linux38-web1-master

cd /var/lib/jenkins/workspace/linux38-web1-master

tar -zcvf myapp.tar.gz ./*

scp myapp.tar.gz www@web1:/data/tomcat/tomcat_appdir

ssh www@web1 "bash /opt/apps/tomcat.sh stop"

ssh www@web1  "cd /data/tomcat/tomcat_appdir ; rm -rf /data/tomcat/tomcat_webapps/myapp/* ; tar -xvf /data/tomcat/tomcat_appdir/myapp.tar.gz -C  /data/tomcat/tomcat_webapps/myapp"

ssh www@web1 "bash /opt/apps/tomcat.sh start"

部署完成后測試tomcat

[root@jenkins ~]# curl http://web1:8080/myapp/index.html
<h1>this is linux39 test page v1</h1>
<h1>this is linux39 test page v2</h1>
<h1>this is linux39 test page v3</h1>

[root@jenkins ~]# curl http://web1:8080/myapp/index.html
<h1>this is linux39 test page v1</h1>
<h1>this is linux39 test page v2</h1>

如何使用haproxy代理將測試環境服務器和現網服務器區分訪問

listen web_port
  bind 192.168.0.100:80   //keepalived虛擬地址
  mode http
  log global
  server web1 192.168.0.142:8080 check inter 3000 fall 2 rise 5

listen web_port
  bind 192.168.64.101:80 //keepalived虛擬地址  
  mode http
  log global
  server web2 192.168.0.79:8080 check inter 3000 fall 2 rise 5

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM