jenkins git公有倉庫與私有倉庫發佈代碼

1 發佈php代碼php

- jenkins已經搭建完成,如今開始來作一個php發佈代碼的任務html

- 在發佈php代碼時先看看是否有兩個插件git

在系統管理-管理插件- 已安裝插件-
檢查是否有「Git plugin」和「Publish Over SSH」兩個插件,若是沒有,則需點擊「可選插件」,找到它並安裝
[root@chy01 jenkins]# systemctl restart jenkins //重啓服務

- 須要生成一對密鑰對用來登陸遠程機器(遠程機器便是發佈php代碼的機器)web

[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
//我以前有生成過密鑰對在這我能夠拿來直接用,你也能夠根據本身的需求來從新生成

- 以後在設置jenkinsshell

系統管理-系統設置-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?p_w_picpathslim)vim

SSH Server,name自定義,Hostname填寫線上web服務器的ip,Username填寫root,Remote Directory填寫/(根)  centos

若是是多臺web server,繼續點擊「增長」,重複以上操做bash

- 建立新的任務服務器

722c81001d7cf47f08153d4e7b08d301.png-wh_

8f55534c9977d170c6a1ffd832229cf2.png-wh_

Repository URL」填寫你項目的git地址,若是是公共項目能夠不設置下面的參數,直接填寫一個git地址便可,如果私有項目(-none),須要填寫認證信息,好比能夠選擇 「SSH Username whith private key」,而後設置Username以及private key   app

「Branches to build」 默認爲*/master,意思是發佈的分支爲master,保持默認

eb0866031c75800370c37e0280bc46dd.png-wh_

「構建觸發器」和「構建環境」留空

- 「構建」,選擇 「Send files or execute commands over SSH」 

e05726844c8277295bed48d317a4f5b3.png-wh_

Remove prefix能夠指定截掉的前綴目錄,這裏留空便可,Remote directory指定遠程服務器上代碼存放路徑,好比/data/wwwroot/www.aaa.com,Exec command爲文件傳輸完成後要執行的命令,好比能夠是更改文件權限的命令,設置完成後點擊 「Add Transfer Set」,若是還有另外的機器,能夠點擊 「Add Server」重複以上操做

- 開始發佈代碼

1d0ce9e85f96ecba5329b3fc501dd8a5.png-wh_

- 查看發佈的結果

[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 git@192.168.212.12
# 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 git@192.168.212.12:/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 git@192.168.212.12:/data/gitroot/sample.git
   a2ee2f6..d22de14  master -> master
如上是上傳discuz的代碼

git私有倉庫查看是否上傳

[root@chy02 sample.git]# git log --pretty=oneline
b081942e4a9eed04ffe34e7b2a6796270f1fcc69 discuzz 代碼

如今須要在jenkins裏下發discuz的代碼

- 以下是新建一個任務

084d68c29b88580e625595264363e569.png-wh_

- 建立git私有倉庫的信息

ca6b7ffc24bbd56674bd907c480110e6.png-wh_

09cd2ddf4d2094ea2dadc462e0aac8dd.png-wh_

- 以下的就與以前的公有倉庫的操做是一致的,這裏就不細說了

3c22e86b29810e7561d8a896201f62d2.png-wh_

-最後就開始構建 

- 在客戶機上查看是否有構建的任務

[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

- 這裏須要再次注意下,若是是本身搭建的私有倉庫必定要記得每臺機器上要有相應的密鑰。切記切記。  

固然後期只要是構建成功還須要發一封郵件提醒,來提高工做效率!!

相關文章
相關標籤/搜索