jenkins git公有倉庫與私有倉庫發布代碼
1 發布php代碼
- jenkins已經搭建完成,現在開始來做一個php發布代碼的任務
- 在發布php代碼時先看看是否有兩個插件
在系統管理-管理插件- 已安裝插件- 檢查是否有“Git plugin”和“Publish Over SSH”兩個插件,如果沒有,則需點擊“可選插件”,找到它並安裝 [root@chy01 jenkins]# systemctl restart jenkins //重啟服務
- 需要生成一對密鑰對用來登錄遠程機器(遠程機器即是發布php代碼的機器)
[root@chy01 ~]# cd /root/.ssh/ [root@chy01 .ssh]# ls aaa authorized_keys boy.pub id_rsa jump known_hosts lf1.pub aaa.pub boy config id_rsa.pub jump.pub lf1 //我之前有生成過密鑰對在這我可以拿來直接用,你也可以根據自己的需求來重新生成
- 之後在設置jenkins
系統管理-系統設置-Publish over SSH 找到這一項,添加之前生成的密鑰對的私鑰即可 之後在將公鑰放在想要發布php代碼的機器(簡稱遠程機器)我這邊放在了[root@chy ~]# cat .ssh/authorized_keys 這個機器上面 [root@chy01 ~]# ssh 192.168.212.10 測試可以登錄到遠程的機器
![mark](http://oqz6pu8vi.bkt.clouddn.com/blog/20171120/163348636.png?imageslim)
SSH Server,name自定義,Hostname填寫線上web服務器的ip,Username填寫root,Remote Directory填寫/(根)
如果是多臺web server,繼續點擊“增加”,重復以上操作
- 創建新的任務
Repository URL”填寫你項目的git地址,如果是公共項目可以不設置下面的參數,直接填寫一個git地址即可,若是私有項目(-none),需要填寫認證信息,比如可以選擇 “SSH Username whith private key”,然後設置Username以及private key
“Branches to build” 默認為*/master,意思是發布的分支為master,保持默認
“構建觸發器”和“構建環境”留空
- “構建”,選擇 “Send files or execute commands over SSH”
Remove prefix可以指定截掉的前綴目錄,這裏留空即可,Remote directory指定遠程服務器上代碼存放路徑,比如/data/wwwroot/www.aaa.com,Exec command為文件傳輸完成後要執行的命令,比如可以是更改文件權限的命令,設置完成後點擊 “Add Transfer Set”,如果還有另外的機器,可以點擊 “Add Server”重復以上操作
- 開始發布代碼
- 查看發布的結果
[root@chy ~]# ls /tmp/jenkins_test/ D11Z D13Z D15Z D18Z D20Z D22Z README.md D12Z D14Z D17Z D19Z D21Z LICENSE //查看已經發布成功
這裏需要註意下如果是在git上重新更新了一下文件只需要在點下“立即構建”就會更新在git上更新的項目的內容
(如上我們用的是git的公共的倉庫,可是我有一個私有的倉庫應該怎麽做呢?
- 需求:需要將discuz的代碼發布到客戶機上(chy這臺機器上)。
在這我們有三臺機器:
chy(192.168.212.10)--這臺是最後的客戶機查看discuz發布代碼用的機器
chy02(192.168.212.12)--這臺是搭建的git的私有倉庫
chy01(192.168.212.11)--這臺是要將discuz代碼上傳到私有倉庫的一臺機器,我這邊需要上傳到私有倉庫然後jenkins用私有倉庫的地址發布
如上的三臺機器都是centos7
git的私有倉庫192.168.212.12 chy02(快速搭建)
[root@chy02 ~]# yum install -y git [root@chy02 ~]# useradd -s /usr/bin/git-shell git [root@chy02 ~]# cd /home/git/ [root@chy02 git]# mkdir .ssh [root@chy02 git]# touch .ssh/authorized_keys [root@chy02 git]# chmod 600 .ssh/authorized_keys [root@chy02 git]# chown -R git:git .ssh [root@chy02 git]# vi .ssh/authorized_keys 放入公鑰,保證客戶機可以訪問我們的git私有倉庫的服務端 [root@chy01 ~]# ssh [email protected] # cat /etc/motd fatal: Interactive git shell is not enabled. hint: ~/git-shell-commands should exist and have read and execute access. Connection to 192.168.212.12 closed.
到git的客戶機裏開始將discuz代碼上傳到我們的私有倉庫裏
[root@chy01 ~]# cd /tmp You have new mail in /var/spool/mail/root [root@chy01 tmp]# git clone [email protected]:/data/gitroot/sample.git Cloning into ‘sample‘... warning: You appear to have cloned an empty repository. [root@chy01 tmp]# cd sample/ [root@chy01 sample]# ls -la total 12 drwxr-xr-x 3 root root 4096 Nov 22 00:14 . drwxrwxrwt. 15 root root 4096 Nov 22 00:14 .. drwxr-xr-x 7 root root 4096 Nov 22 00:14 .git [root@chy01 sample]# vim discuzz.html [root@chy01 sample]# git add discz.html [root@chy01 sample]# git commit -m "daima" [master d22de14] daima 1 file changed, 470 insertions(+) create mode 100644 discz.html [root@chy01 sample]# git push Counting objects: 3, done. Compressing objects: 100% (2/2), done. Writing objects: 100% (2/2), 252 bytes | 0 bytes/s, done. Total 2 (delta 0), reused 0 (delta 0) To [email protected]:/data/gitroot/sample.git a2ee2f6..d22de14 master -> master 如上是上傳discuz的代碼
git私有倉庫查看是否上傳
[root@chy02 sample.git]# git log --pretty=oneline b081942e4a9eed04ffe34e7b2a6796270f1fcc69 discuzz 代碼
現在需要在jenkins裏下發discuz的代碼
- 如下是新建一個任務
- 創建git私有倉庫的信息
- 如下的就與之前的公有倉庫的操作是一致的,這裏就不細說了
-最後就開始構建
- 在客戶機上查看是否有構建的任務
[root@chy ~]# cd /tmp/jenkins_test/ [root@chy jenkins_test]# ls 1.txt D12Z D14Z D17Z D19Z D21Z discuzz.html LICENSE 習題答案.txt D11Z D13Z D15Z D18Z D20Z D22Z discz.html README.md 查看到已有discuzz
- 這裏需要再次註意下,如果是自己搭建的私有倉庫一定要記得每臺機器上要有相應的密鑰。切記切記。
當然後期只要是構建成功還需要發一封郵件提醒,來提升工作效率!!
jenkins git公有倉庫與私有倉庫發布代碼