搭建 Jenkins-2.83 服務,部署 spring boot 專案

搜雲庫技術團隊發表於2017-10-12

Jenkins 是什麼?

Jenkins 是一個可擴充套件的持續整合引擎。

主要用於:

持續、自動地構建/測試軟體專案。
監控一些定時執行的任務。

Jenkins 擁有的特性包括:

易於安裝-只要把jenkins.war部署到servlet容器,不需要資料庫支援。
易於配置-所有配置都是通過其提供的web介面實現。
整合RSS/E-mail通過RSS釋出構建結果或當構建完成時通過e-mail通知。
生成JUnit/TestNG測試報告。
分散式構建支援Jenkins能夠讓多臺計算機一起構建/測試。
檔案識別:Jenkins能夠跟蹤哪次構建生成哪些jar,哪次構建使用哪個版本的jar等。
外掛支援:支援擴充套件外掛,你可以開發適合自己團隊使用的工具。

準備工作

環境

JDK:1.8  
Jenkins:2.83
Centos:7.3  
複製程式碼

安裝

下載

cd /opt
wget http://mirrors.jenkins.io/war/2.83/jenkins.war
複製程式碼

啟動

關閉防護牆

systemctl stop firewalld.service
複製程式碼

啟動服務

java -jar jenkins.war
複製程式碼

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

訪問

瀏覽器訪問:http://localhost:8080/

第一次啟動Jenkins時,出於安全考慮,Jenkins會自動生成一個隨機的按照口令。

注意控制檯輸出的口令,複製下來,然後在瀏覽器輸入密碼:

*************************************************************
Jenkins initial setup is required. An admin user has been created and a password generated.
Please use the following password to proceed to installation:

84a2f219030f45568c89e6602af60877 

This may also be found at: /root/.jenkins/secrets/initialAdminPassword

*************************************************************
複製程式碼

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

搭建 Jenkins-2.83 服務,部署 spring boot 專案

接下來是進入外掛安裝進度介面:

搭建 Jenkins-2.83 服務,部署 spring boot 專案

配置使用者名稱密碼:

搭建 Jenkins-2.83 服務,部署 spring boot 專案

初始化成功後進入 Jenkins 首頁:

搭建 Jenkins-2.83 服務,部署 spring boot 專案

全域性工具配置.Jdk,Mavem,git Docker,等配置,安裝

搭建 Jenkins-2.83 服務,部署 spring boot 專案

構建專案

新建專案

搭建 Jenkins-2.83 服務,部署 spring boot 專案

這裡,選擇構建一個自由風格的軟體專案;

搭建 Jenkins-2.83 服務,部署 spring boot 專案

原始碼管理

Jenkins支援多種原始碼管理伺服器;

搭建 Jenkins-2.83 服務,部署 spring boot 專案

構建配置

選擇 Execute shell 構建 輸入一下命令並且儲存

mvn clean package
複製程式碼

搭建 Jenkins-2.83 服務,部署 spring boot 專案

立即構建

搭建 Jenkins-2.83 服務,部署 spring boot 專案

檢視日誌

搭建 Jenkins-2.83 服務,部署 spring boot 專案

搭建 Jenkins-2.83 服務,部署 spring boot 專案

Started by user ymq
Building in workspace /root/.jenkins/workspace/file-manager
Cloning the remote Git repository
Cloning repository https://github.com/souyunku/file-manager.git
 > git init /root/.jenkins/workspace/file-manager # timeout=10
Fetching upstream changes from https://github.com/souyunku/file-manager.git
 > git --version # timeout=10
 > git fetch --tags --progress https://github.com/souyunku/file-manager.git +refs/heads/*:refs/remotes/origin/*
 > git config remote.origin.url https://github.com/souyunku/file-manager.git # timeout=10
 > git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git config remote.origin.url https://github.com/souyunku/file-manager.git # timeout=10
Fetching upstream changes from https://github.com/souyunku/file-manager.git
 > git fetch --tags --progress https://github.com/souyunku/file-manager.git +refs/heads/*:refs/remotes/origin/*
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision 3ef1aa74305bdfcbc5b79bb4d7b1eeaea62a959f (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 3ef1aa74305bdfcbc5b79bb4d7b1eeaea62a959f
Commit message: "*"
First time build. Skipping changelog.
[file-manager] $ /bin/sh -xe /tmp/jenkins3743583121531645127.sh
+ mvn clean package
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building file-manager 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.6.1:clean (default-clean) @ file-manager ---
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ file-manager ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 138 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ file-manager ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 29 source files to /root/.jenkins/workspace/file-manager/target/classes
[WARNING] /root/.jenkins/workspace/file-manager/src/main/java/io/ymq/fm/web/CloudStorageController.java: Some input files use unchecked or unsafe operations.
[WARNING] /root/.jenkins/workspace/file-manager/src/main/java/io/ymq/fm/web/CloudStorageController.java: Recompile with -Xlint:unchecked for details.
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ file-manager ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /root/.jenkins/workspace/file-manager/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ file-manager ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ file-manager ---
[INFO] Tests are skipped.
[INFO] 
[INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ file-manager ---
[INFO] Building jar: /root/.jenkins/workspace/file-manager/target/file-manager-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.654s
[INFO] Finished at: Tue Oct 10 14:23:39 CST 2017
[INFO] Final Memory: 25M/61M
[INFO] ------------------------------------------------------------------------
Finished: SUCCESS
複製程式碼

檢視 jar

搭建 Jenkins-2.83 服務,部署 spring boot 專案

部署專案

部署指令碼

把指令碼放到 /etc/rc.d/init.d 下賦許可權 chmod 777 spring-boot.sh

#!/bin/bash

SpringBoot=$2

if [ "$1" = "" ];
then
    echo -e "\033[0;31m 未輸入操作名 \033[0m  \033[0;34m {start|stop|restart|status} \033[0m"
    exit 1
fi

if [ "$SpringBoot" = "" ];
then
    echo -e "\033[0;31m 未輸入應用名 \033[0m"
    exit 1
fi

function start()
{
	count=`ps -ef |grep java|grep $SpringBoot|grep -v grep|wc -l`
	if [ $count != 0 ];then
		echo "$SpringBoot is running..."
	else
		echo "Start $SpringBoot success..."
		BUILD_ID=dontKillMe nohup java -jar $SpringBoot > /dev/null 2>&1 &
	fi
}

function stop()
{
	echo "Stop $SpringBoot"
	boot_id=`ps -ef |grep java|grep $SpringBoot|grep -v grep|awk '{print $2}'`
	count=`ps -ef |grep java|grep $SpringBoot|grep -v grep|wc -l`

	if [ $count != 0 ];then
	    kill $boot_id
    	count=`ps -ef |grep java|grep $SpringBoot|grep -v grep|wc -l`

		boot_id=`ps -ef |grep java|grep $SpringBoot|grep -v grep|awk '{print $2}'`
		kill -9 $boot_id
	fi
}

function restart()
{
	stop
	sleep 2
	start
}

function status()
{
    count=`ps -ef |grep java|grep $SpringBoot|grep -v grep|wc -l`
    if [ $count != 0 ];then
        echo "$SpringBoot is running..."
    else
        echo "$SpringBoot is not running..."
    fi
}

case $1 in
	start)
	start;;
	stop)
	stop;;
	restart)
	restart;;
	status)
	status;;
	*)

	echo -e "\033[0;31m Usage: \033[0m  \033[0;34m sh  $0  {start|stop|restart|status}  {SpringBootJarName} \033[0m\033[0;31m Example: \033[0m\033[0;33m sh  $0  start esmart-test.jar \033[0m"
esac
複製程式碼

部署語法

搭建 Jenkins-2.83 服務,部署 spring boot 專案

mvn clean package

cp /etc/rc.d/init.d/spring-boot.sh /root/.jenkins/workspace/file-manager/target

cd /root/.jenkins/workspace/file-manager/target

./spring-boot.sh restart file-manager-1.0-SNAPSHOT.jar
複製程式碼

請注意配置構建指令碼的時候的寫法,沒有BUILD_ID=dontKillMe 是不可以的

BUILD_ID=dontKillMe nohup java -jar $SpringBoot > /dev/null 2>&1 &
複製程式碼

立即構建

+ cp /etc/rc.d/init.d/spring-boot.sh /root/.jenkins/workspace/file-manager/target
+ cd /root/.jenkins/workspace/file-manager/target
+ ./spring-boot.sh restart file-manager-1.0-SNAPSHOT.jar
Stop file-manager-1.0-SNAPSHOT.jar
Start file-manager-1.0-SNAPSHOT.jar success...
Finished: SUCCESS
複製程式碼

檢視程式

[root@node1 init.d]# ps -ef | grep java
root       2803   2779  0 Oct10 pts/0    00:04:24 java -jar jenkins.war
root      21239      1  6 11:22 pts/0    00:00:13 java -jar file-manager-1.0-SNAPSHOT.jar
root      21304  14988  0 11:26 pts/1    00:00:00 grep --color=auto java
複製程式碼

Contact

  • 作者:鵬磊
  • 出處:www.ymq.io
  • Email:admin@souyunku.com
  • 版權歸作者所有,轉載請註明出處
  • Wechat:關注公眾號,搜雲庫,專注於開發技術的研究與知識分享

關注公眾號-搜雲庫
搜雲庫

相關文章