搭建 Jenkins-2.83 服务,部署 spring boot 项目

2017/10/10 Jenkins

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

全局工具配置.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

  • 作者:鹏磊
  • 出处:http://www.ymq.io
  • Email:admin@souyunku.com
  • 版权归作者所有,转载请注明出处
  • Wechat:关注公众号,搜云库,专注于开发技术的研究与知识分享

关注公众号-搜云库

Show Disqus Comments

Search

    关注 - 微信公众号

    搜云库

    Table of Contents