天天看點

(轉)Spring Boot(十六):使用 Jenkins 部署 Spring Boot

enkins 是 Devops 神器,本篇文章介紹如何安裝和使用 Jenkins 部署 Spring Boot 項目

Jenkins 搭建、部署分為四個步驟;

第一步,Jenkins 安裝

第二步,插件安裝和配置

第三步,Push SSH

第四步,部署項目

準備環境:

JDK:1.8

Jenkins:2.83 Centos:7.3

maven 3.5

Jdk 預設已經安裝完成

版本要求 Maven3.5.0

軟體下載下傳

安裝

修改環境變量, 在​<code>​/etc/profile​</code>​中添加以下幾行

記得執行​<code>​source /etc/profile​</code>​使環境變量生效。

驗證 最後運作​<code>​mvn -v​</code>​驗證maven是否安裝成功

關閉防護牆

兩個指令同時運作,運作完成後檢視防火牆關閉狀态

下載下傳

啟動服務

Jenkins 就啟動成功了!它的war包自帶Jetty伺服器

第一次啟動 Jenkins 時,出于安全考慮,Jenkins 會自動生成一個随機的按照密碼。注意控制台輸出的密碼,複制下來,然後在浏覽器輸入密碼:

通路 浏覽器通路:​<code>​http://localhost:8080/​</code>​

(轉)Spring Boot(十六):使用 Jenkins 部署 Spring Boot

輸入:0cca37389e6540c08ce6e4c96f46da0f

進入使用者自定義插件界面,建議選擇安裝官方推薦插件,因為安裝後自己也得安裝:

(轉)Spring Boot(十六):使用 Jenkins 部署 Spring Boot

接下來是進入插件安裝進度界面:

(轉)Spring Boot(十六):使用 Jenkins 部署 Spring Boot

插件一次可能不會完全安裝成功,可以點選Retry再次安裝。直到全部安裝成功

(轉)Spring Boot(十六):使用 Jenkins 部署 Spring Boot

等待一段時間之後,插件安裝完成,配置使用者名密碼:

(轉)Spring Boot(十六):使用 Jenkins 部署 Spring Boot

輸入:admin/admin

系統管理-》全局工具配置 jdk路徑,

(轉)Spring Boot(十六):使用 Jenkins 部署 Spring Boot

有很多插件都是選擇的預設的安裝的,是以現在需要我們安裝的插件不多,Git plugin 和 Maven Integration plugin,publish over SSH。

插件安裝:系統管理 &gt; 插件管理 &gt; 可選插件,勾選需要安裝的插件,點選直接安裝或者下載下傳重新開機後安裝

(轉)Spring Boot(十六):使用 Jenkins 部署 Spring Boot

系統管理 &gt; 全局工具配置

JDK

配置本地 JDK 的路徑,去掉勾選自動安裝

(轉)Spring Boot(十六):使用 Jenkins 部署 Spring Boot

Maven

配置本地maven的路徑,去掉勾選自動安裝

(轉)Spring Boot(十六):使用 Jenkins 部署 Spring Boot

其它内容可以根據自己的情況選擇安裝。

ssh 的配置可使用密鑰,也可以使用密碼,這裡我們使用密鑰來配置,在配置之前先配置好jenkins伺服器和應用伺服器的密鑰認證 Jenkins伺服器上生成密鑰對,使用​<code>​ssh-keygen -t rsa​</code>​指令

輸入下面指令 一直回車,一個矩形圖形出現就說明成功,在~/.ssh/下會有私鑰id_rsa和公鑰id_rsa.pub

将jenkins伺服器的公鑰​<code>​id_rsa.pub​</code>​中的内容複制到應用伺服器 的~/.ssh/下的 ​<code>​authorized_keys​</code>​檔案

在應用伺服器上重新開機 ssh 服務,​<code>​service sshd restart​</code>​現在 Jenkins 伺服器可免密碼直接登陸應用伺服器

之後在用 ssh B嘗試能否免密登入 B 伺服器,如果還是提示需要輸入密碼,則有以下原因

a. 非 root 賬戶可能不支援 ssh 公鑰認證(看伺服器是否有限制)

b. 傳過來的公鑰檔案權限不夠,可以給這個檔案授權下 chmod 644 authorized_keys

c. 使用 root 賬戶執行 ssh-copy-id -i ~/.ssh/id_rsa.pub 這個指令的時候如果需要輸入密碼則要配置sshd_config

修改完後要重新開機 sshd 服務

最後,如果可以 SSH IP 免密登入成功說明 SSH 公鑰認證成功。

上面這種方式比較複雜,其實在 Jenkins 背景直接添加操作即可,參考下面方式

(1)點選”進階”展開配置

(轉)Spring Boot(十六):使用 Jenkins 部署 Spring Boot

(2)配置SSH的登陸密碼

(轉)Spring Boot(十六):使用 Jenkins 部署 Spring Boot

配置完成後可點選“Test Configuration”測試到目标主機的連接配接,出現”success“則成功連接配接,如果有多台應用伺服器,可以點選”增加“,配置多個“SSH Servers” 點選“儲存”以儲存配置。

系統管理 &gt; 系統設定

選擇 Publish over SSH

(轉)Spring Boot(十六):使用 Jenkins 部署 Spring Boot

Passphrase 不用設定 Path to key 寫上生成的ssh路徑:​<code>​/root/.ssh/id_rsa​</code>​

下面的 SSH Servers 是重點

Name 随意起名代表這個服務,待會要根據它來選擇

Hostname 配置應用伺服器的位址

Username 配置 linux 登陸使用者名

Remote Directory 不填

點選下方增加可以添加多個應用伺服器的位址

首頁點選建立:輸入項目名稱

(轉)Spring Boot(十六):使用 Jenkins 部署 Spring Boot

下方選擇建構一個 Maven 項目,點選确定。

勾選丢棄舊的建構,選擇是否備份被替換的舊包。我這裡選擇備份最近的10個

(轉)Spring Boot(十六):使用 Jenkins 部署 Spring Boot

源碼管理,選擇 SVN,配置 SVN 相關資訊,點選 add 可以輸入 SVN 的賬戶和密碼

(轉)Spring Boot(十六):使用 Jenkins 部署 Spring Boot

SVN 位址:http://192.168.0.xx/svn/xxx@HEAD,​<code>​@HEAD​</code>​意思取最新版本

建構環境中勾選“Add timestamps to the Console Output”,代碼建構的過程中會将日志列印出來

(轉)Spring Boot(十六):使用 Jenkins 部署 Spring Boot

在 Build 中輸入打包前的 mvn 指令,如:

意思是:排除測試的包内容,使用字尾為 test 的配置檔案。

(轉)Spring Boot(十六):使用 Jenkins 部署 Spring Boot

Post Steps 選擇 Run only if build succeeds

(轉)Spring Boot(十六):使用 Jenkins 部署 Spring Boot

點選Add post-build step,選擇 Send files or execute commands over SSH

(轉)Spring Boot(十六):使用 Jenkins 部署 Spring Boot

Name 選擇上面配置的 Push SSH

(轉)Spring Boot(十六):使用 Jenkins 部署 Spring Boot

Source files配置:target/xxx-0.0.1-SNAPSHOT.jar 項目jar包名 Remove prefix:target/ Remote directory:Jenkins-in/ 代碼應用伺服器的目錄位址, Exec command:Jenkins-in/xxx.sh 應用伺服器對應的腳本。

需要在應用伺服器建立檔案夾:Jenkins-in,在檔案夾中複制一下腳本内容:xxx.sh

這段腳本的意思,就是 kill 舊項目,删除舊項目,啟動新項目,備份老項目。

全文完。