搭建 Jenkins-2.83 服務,部署 spring boot 項目

Jenkins 是什麼?

Jenkins 是一個可擴展的持續集成引擎。java

主要用於:node

持續、自動地構建/測試軟件項目。
監控一些定時執行的任務。git

Jenkins 擁有的特性包括:github

易於安裝-只要把jenkins.war部署到servlet容器,不須要數據庫支持。
易於配置-全部配置都是經過其提供的web界面實現。
集成RSS/E-mail經過RSS發佈構建結果或當構建完成時經過e-mail通知。
生成JUnit/TestNG測試報告。
分佈式構建支持Jenkins可以讓多臺計算機一塊兒構建/測試。
文件識別:Jenkins可以跟蹤哪次構建生成哪些jar,哪次構建使用哪一個版本的jar等。
插件支持:支持擴展插件,你能夠開發適合本身團隊使用的工具。web

準備工做

環境

JDK:1.8  
Jenkins:2.83
Centos:7.3  
複製代碼

安裝

下載

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

啓動

關閉防禦牆spring

systemctl stop firewalld.service
複製代碼

啓動服務shell

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 首頁:

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

構建項目

新建項目

這裏,選擇構建一個自由風格的軟件項目;

源碼管理

Jenkins支持多種源碼管理服務器;

構建配置

選擇 Execute shell 構建 輸入一下命令而且保存

mvn clean package
複製代碼

當即構建

查看日誌

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

部署項目

部署腳本

把腳本放到 /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
複製代碼

部署語法

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:關注公衆號,搜雲庫,專一於開發技術的研究與知識分享

關注公衆號-搜雲庫
搜雲庫
相關文章
相關標籤/搜索