Jenkins一键打包部署SpringBoot应用
_kairui 人气:0一般而言,一个项目部署的由:拉取代码->构建->测试->打包->部署等过程组成,如果我们经常需要部署项目,特别是在微服务时代,服务特别多的情况下,不停的测试打包部署,那估计得有个人一整天专门做这事了,而这事又是繁琐的重复无意义的,所以就需要一套能够持续集成、持续交付、持续部署的自动化构建流程。
Jenkins是开源CI&CD软件领导者,提供超过1000个插件来支持构建、部署、自动化,满足任何项目的需要。我们可以用Jenkins来构建和部署我们的项目,比如说从我们的代码仓库获取代码,然后将我们的代码打包成可执行的文件,之后通过远程的ssh工具执行脚本来运行我们的项目
一、准备工作
真正的生产环境上,可能是有多台服务器,但是我是用来练手,手头没有多余的服务器,只有一台。所以我就将 Jenkins 和我的 Spring Boot 项目都部署到一台服务器上。
由于本次是使用Docker安装Jenkins,所以需要服务器上提前安装好JDK,Maven,Doker三个必备的环境配置。这里就不过多赘述这三个环境的安装了,可以自行查询资料安装
1、检查 JDK 环境
java -version [root@liulihui home]# java -version java version "1.8.0_231" Java(TM) SE Runtime Environment (build 1.8.0_231-b11) Java HotSpot(TM) 64-Bit Server VM (build 25.231-b11, mixed mode) [root@liulihui home]#
2、检查 Maven 环境
[root@liulihui home]# mvn -v Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f) Maven home: /usr/local/maven/apache-maven-3.6.3 Java version: 1.8.0_231, vendor: Oracle Corporation, runtime: /usr/local/java/jdk/jdk1.8.0_231/jre Default locale: en_US, platform encoding: UTF-8 OS name: "linux", version: "3.10.0-1160.80.1.el7.x86_64", arch: "amd64", family: "unix" [root@liulihui home]#
3、检查 Docker 环境
[root@liulihui home]# docker version Client: Docker Engine - Community Version: 20.10.21 API version: 1.41 Go version: go1.18.7 Git commit: baeda1f Built: Tue Oct 25 18:04:24 2022 OS/Arch: linux/amd64 Context: default Experimental: true Server: Docker Engine - Community Engine: Version: 20.10.21 API version: 1.41 (minimum version 1.12) Go version: go1.18.7 Git commit: 3056208 Built: Tue Oct 25 18:02:38 2022 OS/Arch: linux/amd64 Experimental: false containerd: Version: 1.6.12 GitCommit: a05d175400b1145e5e6a735a6710579d181e7fb0 runc: Version: 1.1.4 GitCommit: v1.1.4-0-g5fd4c4d docker-init: Version: 0.19.0 GitCommit: de40ad0 [root@liulihui home]#
二、Jenkins的安装及配置
1、Docker 搜索 jenkins 镜像
[root@liulihui home]# docker search jenkins NAME DESCRIPTION STARS OFFICIAL AUTOMATED jenkins DEPRECATED; use "jenkins/jenkins:lts" instead 5585 [OK] jenkins/jenkins The leading open source automation server 3330 jenkins/jnlp-slave a Jenkins agent which can connect to Jenkins… 153 [OK] jenkins/inbound-agent 81 bitnami/jenkins Bitnami Docker Image for Jenkins 58 [OK] jenkins/agent 49 jenkins/slave base image for a Jenkins Agent, which includ… 48 [OK] jenkins/ssh-slave A Jenkins slave using SSH to establish conne… 38 [OK] jenkins/ssh-agent Docker image for Jenkins agents connected ov… 28 jenkins/jnlp-agent-docker 8 jenkins/jnlp-agent-maven A JNLP-based agent with Maven 3 built in 7 jenkins/pct Plugin Compat Tester 5 [OK] jenkins/evergreen An automatically self-updating Jenkins distr… 5 jenkins/jnlp-agent-python A JNLP-based agent with Python built in 3 jenkins/jenkins-experimental Experimental images of Jenkins. These images… 3 [OK] jenkins/jnlp-agent-alpine 2 jenkins/jnlp-agent-coresdk 2 rancher/jenkins-jenkins 1 jenkins/jnlp-agent-node 1 jenkins/jenkinsfile-runner Jenkinsfile Runner packages 1 jenkins/core-pr-tester Docker image for testing pull-requests sent … 1 jenkins/ath Jenkins Acceptance Test Harness 1 [OK] jenkins/jnlp-agent-ruby 1 rancher/jenkins-slave Jenkins Build Slave 0 [OK] rancher/jenkins-jnlp-slave
2、Docker 下载jenkins 镜像
[root@liulihui home]# docker pull jenkins/jenkins:lts lts: Pulling from jenkins/jenkins 647acf3d48c2: Pull complete 832e288237bc: Pull complete ea194d1bd1da: Pull complete 98569593b9fd: Pull complete dfe249e8cdf2: Pull complete 0f6f2e6d37bb: Pull complete 07fcb98649b6: Pull complete 1e33c26b1882: Pull complete 7b24f0e29fe1: Pull complete 4d90ef3a8f3b: Pull complete d98c8e675bea: Pull complete b45fc2bf9fd7: Pull complete d6e1fda9effc: Pull complete 3635a44047f2: Pull complete 486e1776d3bc: Pull complete 41233d151800: Pull complete 14a67803c2b3: Pull complete Digest: sha256:c1d02293a08ba69483992f541935f7639fb10c6c322785bdabaf7fa94cd5e732 Status: Downloaded newer image for jenkins/jenkins:lts docker.io/jenkins/jenkins:lts
# 查看镜像 [root@liulihui home]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE jenkins/jenkins lts 2a4bbe50c40b 13 months ago 441MB java 8 d23bdf5b1b1b 5 years ago 643MB [root@liulihui home]#
3、Docker容器中运行Jenkins
# 命令如下: docker run -p 9080:8080 --name liulihui-jenkins \ -u root \ -v /mydata/jenkins_home:/var/jenkins_home \ -v /usr/local/java/jdk/jdk1.8.0_231:/usr/local/jdk1.8 \ -v /usr/local/maven/apache-maven-3.6.3:/usr/local/apache-maven-3.6.3 \ -v $(which docker):/usr/bin/docker \ -v /var/run/docker.sock:/var/run/docker.sock \ -d jenkins/jenkins:lts
# 参数说明: -p 9080:8080 端口映射(将容器的8080端口【后面的8080】映射到服务器的9080端口【前面的9080】,云服务器需要开通9080端口供外网访问) --name 容器名字 -u root 用户名 -v /mydata/jenkins_home:/var/jenkins_home 将配置文件夹挂在到主机(:前面的是主机目录,后面的是容器目录) -v /usr/local/java/jdk/jdk1.8.0_231:/usr/local/jdk1.8 是把linux下的jdk和容器内的关联(配置Jenkins时使用,:前面的是主机目录,后面的是容器目录) -v /usr/local/maven/apache-maven-3.6.3:/usr/local/apache-maven-3.6.3 是把linux下的maven和容器内的关联(配置Jenkins时使用,:前面的是主机目录,后面的是容器目录) -v $(which docker):/usr/bin/docker 是可以在Jenkins容器里使用我们Linux下的docker -v /var/run/docker.sock:/var/run/docker.sock 是可以在Jenkins容器里使用我们Linux下的docker -d jenkins/jenkins:lts 后台启动 Jenkins镜像(最新版)
# 查看docker 正在运行的容器 [root@liulihui home]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9363f69cc773 jenkins/jenkins:lts "/sbin/tini -- /usr/…" 19 seconds ago Up 17 seconds 50000/tcp, 0.0.0.0:9080->8080/tcp, :::9080->8080/tcp liulihui-jenkins
4、Jenkins的配置
运行成功后访问该地址登录Jenkins,第一次登录需要输入管理员密码:http://你的ip:9080/
使用管理员密码进行登录,可以使用以下命令从容器启动日志中获取管理密码:
# 查看日志,获取管理员密码 [root@liulihui home]# docker logs liulihui-jenkins Running from: /usr/share/jenkins/jenkins.war webroot: EnvVars.masterEnvVars.get("JENKINS_HOME") 2023-01-05 06:02:04.057+0000 [id=1] INFO org.eclipse.jetty.util.log.Log#initialized: Logging initialized @707ms to org.eclipse.jetty.util.log.JavaUtilLog 2023-01-05 06:02:04.141+0000 [id=1] INFO winstone.Logger#logInternal: Beginning extraction from war file 2023-01-05 06:02:09.723+0000 [id=1] WARNING o.e.j.s.handler.ContextHandler#setContextPath: Empty contextPath 2023-01-05 06:02:09.829+0000 [id=1] INFO org.eclipse.jetty.server.Server#doStart: jetty-9.4.43.v20210629; built: 2021-06-30T11:07:22.254Z; git: 526006ecfa3af7f1a27ef3a288e2bef7ea9dd7e8; jvm 11.0.13+8 2023-01-05 06:02:10.641+0000 [id=1] INFO o.e.j.w.StandardDescriptorProcessor#visitServlet: NO JSP Support for /, did not find org.eclipse.jetty.jsp.JettyJspServlet 2023-01-05 06:02:10.757+0000 [id=1] INFO o.e.j.s.s.DefaultSessionIdManager#doStart: DefaultSessionIdManager workerName=node0 2023-01-05 06:02:10.757+0000 [id=1] INFO o.e.j.s.s.DefaultSessionIdManager#doStart: No SessionScavenger set, using defaults 2023-01-05 06:02:10.758+0000 [id=1] INFO o.e.j.server.session.HouseKeeper#startScavenging: node0 Scavenging every 660000ms 2023-01-05 06:02:11.848+0000 [id=1] INFO hudson.WebAppMain#contextInitialized: Jenkins home directory: /var/jenkins_home found at: EnvVars.masterEnvVars.get("JENKINS_HOME") 2023-01-05 06:02:12.155+0000 [id=1] INFO o.e.j.s.handler.ContextHandler#doStart: Started w.@216914{Jenkins v2.319.1,/,file:///var/jenkins_home/war/,AVAILABLE}{/var/jenkins_home/war} 2023-01-05 06:02:12.204+0000 [id=1] INFO o.e.j.server.AbstractConnector#doStart: Started ServerConnector@72cf2de5{HTTP/1.1, (http/1.1)}{0.0.0.0:8080} 2023-01-05 06:02:12.204+0000 [id=1] INFO org.eclipse.jetty.server.Server#doStart: Started @8856ms 2023-01-05 06:02:12.219+0000 [id=23] INFO winstone.Logger#logInternal: Winstone Servlet Engine running: controlPort=disabled 2023-01-05 06:02:12.576+0000 [id=30] INFO jenkins.InitReactorRunner$1#onAttained: Started initialization 2023-01-05 06:02:12.620+0000 [id=28] INFO jenkins.InitReactorRunner$1#onAttained: Listed all plugins WARNING: An illegal reflective access operation has occurred WARNING: Illegal reflective access by com.google.inject.internal.cglib.core.$ReflectUtils$2 (file:/var/jenkins_home/war/WEB-INF/lib/guice-4.0.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) WARNING: Please consider reporting this to the maintainers of com.google.inject.internal.cglib.core.$ReflectUtils$2 WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations WARNING: All illegal access operations will be denied in a future release 2023-01-05 06:02:14.226+0000 [id=28] INFO jenkins.InitReactorRunner$1#onAttained: Prepared all plugins 2023-01-05 06:02:14.231+0000 [id=28] INFO jenkins.InitReactorRunner$1#onAttained: Started all plugins 2023-01-05 06:02:14.249+0000 [id=28] INFO jenkins.InitReactorRunner$1#onAttained: Augmented all extensions 2023-01-05 06:02:15.935+0000 [id=30] INFO jenkins.InitReactorRunner$1#onAttained: System config loaded 2023-01-05 06:02:15.936+0000 [id=30] INFO jenkins.InitReactorRunner$1#onAttained: System config adapted 2023-01-05 06:02:15.936+0000 [id=30] INFO jenkins.InitReactorRunner$1#onAttained: Loaded all jobs 2023-01-05 06:02:15.937+0000 [id=30] INFO jenkins.InitReactorRunner$1#onAttained: Configuration for all jobs updated 2023-01-05 06:02:16.146+0000 [id=44] INFO hudson.model.AsyncPeriodicWork#lambda$doRun$1: Started Download metadata 2023-01-05 06:02:16.212+0000 [id=44] INFO hudson.util.Retrier#start: Attempt #1 to do the action check updates server 2023-01-05 06:02:19.033+0000 [id=28] INFO jenkins.install.SetupWizard#init: ************************************************************* ************************************************************* ************************************************************* Jenkins initial setup is required. An admin user has been created and a password generated. Please use the following password to proceed to installation: f8bf269a288f4cdd92710f2d41eed6a1 This may also be found at: /var/jenkins_home/secrets/initialAdminPassword ************************************************************* ************************************************************* ************************************************************* 2023-01-05 06:03:41.112+0000 [id=44] INFO h.m.DownloadService$Downloadable#load: Obtained the updated data file for hudson.tasks.Maven.MavenInstaller 2023-01-05 06:03:41.113+0000 [id=44] INFO hudson.util.Retrier#start: Performed the action check updates server successfully at the attempt #1 2023-01-05 06:03:41.115+0000 [id=44] INFO hudson.model.AsyncPeriodicWork#lambda$doRun$1: Finished Download metadata. 84,965 ms 2023-01-05 06:03:41.912+0000 [id=30] INFO jenkins.InitReactorRunner$1#onAttained: Completed initialization 2023-01-05 06:03:41.930+0000 [id=22] INFO hudson.WebAppMain$3#run: Jenkins is fully up and running [root@liulihui home]#
输入管理员密码后,就进入安装界面,选择安装插件方式,这里我们直接安装推荐的插件
安装完成后,创建管理员账号:
进入插件安装界面,联网等待插件安装:
进行实例配置:
点击保存并完成,Jenkins就安装已完成
进入Jenkins,点击系统管理->插件管理,进行一些自定义的插件安装
修改插件的站点,把原站点:https://updates.jenkins.io/update-center.json 改为:https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json 可以更快的下载插件
确保以下插件被正确安装,未安装的在可选插件中搜索安装即可:
(1)根据角色管理权限的插件:Role-based Authorization Strategy
(2)把 Jenkins 打包好的 jar 上传到应用服务器上:Publish Over SSH
已安装插件Role-based Authorization Strategy
已安装插件Publish Over SSH
安装完成后需要重启一下jenkins,在连接后面加restart就可以重启jenkins了,或者使用命令:docker restart 容器名字
http://ip:9080/restart 或者 docker restart 容器名字(这里是 http://ip:9080/restart )
5、配置JDK和Maven
通过系统管理->全局工具配置来进行全局工具的配置,路径都是jenkins里面的路径;
jdk配置
maven配置
系统管理->系统配置,找到 Publish Over SSH,配置好要连接的应用服务器(在后面的操作中需要使用jenkins远程ssh连接到应用服务器,进行构建后的应用部署运行。用户名密码会导致 jar 包上传失败,要在应用服务器上生成 ssh 密钥对。)
Centos 7 上创建SSH密钥查看:https:
Jenkins使用ssh就可以执行远程的linux脚本了:
进入系统管理->凭据中,添加Gitee登录账号凭据。接下来配置 Gitee 的凭证,要根据这些凭证,才能从 Gitee 上拉取代码下来
填写完成后选择该凭据,就可以正常连接git仓库了;
所有配置工作都做完了,接下来我们就可以开始构建一个项目了。
三、打包部署SpringBoot应用
1、在Jenkins中创建执行任务
设置任务名称后,选择构建一个自由风格的软件项目:
2、添加git凭据
添加gitee登录账号凭据。接下来配置 gitee 的凭证,要根据这些凭证,才能从 gitee 上拉取代码下来。(如果前面添加了凭证,这里直接选择就行)
填写完成后选择该凭据,就可以正常连接git仓库了;
3、添加maven构建
maven 构建
选择我们的maven版本,然后设置maven命令和指定pom文件位置:
增加 构建后操作步骤:
因为前面运行jenkins的时候已经做了目录的映射,所以jenkins打完包后其实是会把包同步到服务映射的目录里的。所以这里不需要上传jar包,直接执行xshell命令,进入jar包的目录,执行jar包即可
shell脚本:
#第一步是进入到服务器中生成好的jar包的目录下 cd /mydata/jenkins_home/workspace/demo/target/ #第二步是根据jar包的名字获取运行的pid,并且将该进程杀死 ps -ef | grep demo-0.0.1-SNAPSHOT.jar | grep -v grep | awk '{printf $2}' | xargs kill -9 #执行 前加载一下环境变量,否则不会执行java -jar 命令 source /etc/profile #最后一步就是将这个jar后台启动了,并且将日志输出到warpper.log中。 nohup java -jar demo-0.0.1-SNAPSHOT.jar >demo.log &2>1 & #睡眠1秒 sleep 1 #输出内容,可不加 echo "启动完成>>>>"
四、立即构建
配置完成后,点击立即构建,可以看到控制台输出成功
选择项目打包分支:
分支配置:
服务部署成功,测试接口返回成功:
加载全部内容