1效果展示
修改代码。push到远程仓库,等待片刻,刷新浏览器,内容更新,持续集成springboot项目成功。    
2安装(centos 7)
有三种安装方法 1docker安装 (不推荐,会出很多问题) 2war包安装 (需要手动下载上传war包) 3wget命令安装(推荐)
1安装jdk,git,maven,docker配置环境变量
2安装jenkins
第一步 安装
wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
yum -y install jenkins
第二步 启动服务
启动服务
service jenkins start
停止服务
service jenkins stop
启动之后的界面 
第三步 获取密码
使用cat命令读取上图中的文件获取密码
cat /var/lib/jenkins/secrets/initialAdminPassword
进入jenkins工作目录
cd /var/lib/jenkins/secrets/initialAdminPassword/updates
将镜像源修改
sed -i 's/http:\/\/updates.jenkins-ci.org\/download/https:\/\/mirrors.tuna.tsinghua.edu.cn\/jenkins/g' default.json && sed -i 's/http:\/\/www.google.com/https:\/\/www.baidu.com/g' default.json
第四步 安装插件
将第三步得到的密码输入到上图管理员密码中,来到安装插件界面,选择安装推荐的插件 
第五步 创建管理员账户,随便填即可

第六步 配置
 
获取jdk路径
which java
获取git路径
which git
获取maven路径
which mvn

  
第七步 配置全局环境变量
 
获取PATH的值
echo $PATH

3使用jenkins
使用gitee进行演示
1安装插件
   搜索gitee安装相关插件
2创建仓库并提交代码
1项目根目录下新建Dockerfile
FROM openjdk:8-jdk-alpine
VOLUME /tmp
COPY ./target/demojenkins.jar demojenkins.jar
ENTRYPOINT ["java","-jar","/demojenkins.jar", "&"]
2修改pom文件
<packaging>jar</packaging>
<build>
<finalName>demojenkins</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
3提交代码到gitee
3构建持续化集成任务
1新建任务
 
2配置git仓库
 
 选择账户,输入git仓库地址
3设置push到gitee仓库自动构建项目部署
 
接下来将生成的密码添加到gitee仓库   
4编写shell,打包部署项目
 需要根据自己项目修改的地方是service_name和service_prot ,记得打开服务器的对应的端口
#!/bin/bash
#maven打包
mvn clean package
echo 'package ok!'
echo 'build start!'
cd ./
service_name="demojenkins"
service_prot=8002
#查看镜像id
IID=$(docker images | grep "$service_name" | awk '{print $3}')
echo "IID $IID"
if [ -n "$IID" ]
then
echo "exist $SERVER_NAME image,IID=$IID"
#删除镜像
docker rmi -f $service_name
echo "delete $SERVER_NAME image"
#构建
docker build -t $service_name .
echo "build $SERVER_NAME image"
else
echo "no exist $SERVER_NAME image,build docker"
#构建
docker build -t $service_name .
echo "build $SERVER_NAME image"
fi
#查看容器id
CID=$(docker ps | grep "$SERVER_NAME" | awk '{print $1}')
echo "CID $CID"
if [ -n "$CID" ]
then
echo "exist $SERVER_NAME container,CID=$CID"
#停止
docker stop $service_name
#删除容器
docker rm $service_name
else
echo "no exist $SERVER_NAME container"
fi
#启动
docker run -d --name $service_name --net=host -p $service_prot:$service_prot $service_name
#查看启动日志
#docker logs -f $service_name
最后点击保存即可
|