目录
一 原理说明
1 流程说明
2 流程图
二 环境部署说明
三 Jenkins基本配置
1 Configure System (系统设置)
2 Configure Global Security (全局安全配置)
a 配置安全域
b 配置授权策略
3 Global Tool Configuration (全局工具配置 )
a 配置maven的全局settings路径
b 配置jdk
c 配置git
d 配置maven的安装路径
4.配置必要插件
四 Jenkins服务器上创建项目和配置
1 新建工程
2 General
3 源码管理
a 添加凭证
b 添加仓库
4 构建触发器
a 生成gitee的回调地址,gitee配置时会用到这个回调地址
b 生成Gitee WebHook密码,gitee配置时会用到这个密码
5 构建环境(无需配置)
6 构建
7 构建后操作
a stop.sh脚本
b start.sh脚本
五 Linux服务器配置
六 gitee服务器配置
七 测试过程
1 测试代码
2 提交代码到码云,Jenkins上触发了构建
3 控制台日志
4 浏览器访问下,可以访问成功
八 参考
开发者提交代码到码云 -> 码云上配置的回调地址会通知jenkins进行自动部署 -> jenkins 拉取最新的源码,打包成jar或war,将打好的包上传到Linux服务器,最后根据Jenkins上配置的命令进行部署。
服务器 |
部署说明 |
IP |
开发机器 |
IDEA,部署在Windows上。 |
192.168.0.104 |
版本控制服务器 |
源码在码云上部署。 |
码云服务器 |
jenkins服务器 |
jekins自动化部署服务器,部署在windows上。 |
192.168.0.104 |
应用服务器 |
部署在Linux虚拟机上。 |
192.168.0.110 |
在系统设置这里,我们只需要设置最后面的一项,配置远程服务器地址。
即我们代码最终运行的服务器地址信息,就像我们之前手动部署时使用xshell登录Linux服务器一样。
配置完成后点击保存即可,为后面我们配置自动化部署做准备,配置如下图:
确保下面3个插件都安装了。
gitee
Publish over SSH
Deploy to container Plugin
新建springboot_test工程,选择 Freestyle project。
http://localhost:8080/gitee-project/springboot_test 这个地址可借助工具(例如ngrok)映射成一个外网地址。
例如,映射为外网地址为 http://cakin24.free.idcfengye.com/gitee-project/springboot_test 。
构建后操作的意思是,jar打包好后,要将jar发送目的地后进行启动和关闭等操作。
这里需要提前在需要部署的服务器上配置好路径,写好启动和停止项目的脚本,并设置为可以执行的脚本。
其实就是我们平时在Linux上手动部署项目操作的脚本。
#!/bin/bash
echo "Stop Procedure : demo-1.0-SNAPSHOT.jar"
pid=`ps -ef |grep java|grep demo-1.0-SNAPSHOT.jar|awk '{print $2}'`
echo 'old Procedure pid:'$pid
if [ -n "$pid" ]
then
kill -9 $pid
fi
#!/bin/bash
echo 'Start the program : demo-1.0-SNAPSHOT.jar'
chmod 777 /home/ldp/app/demo-1.0-SNAPSHOT.jar
echo '-------Starting-------'
cd /home/ldp/app/
nohup ${JAVA_HOME}/bin/java -jar demo-1.0-SNAPSHOT.jar &
echo 'start success'
在Linux服务上,上传启动和停止脚本。
[root@centos app]# pwd
/home/ldp/app
[root@centos app]# ll
total 8
-rwxrwxr-x 1 root root 239 Aug 9 15:26 start.sh
-rwxrwxr-x 1 root root 198 Aug 9 15:29 stop.sh
在前面,我们已经将 http://localhost:8080/gitee-project/springboot_test 这个地址通过外网映射工具映射成 http://cakin24.free.idcfengye.com/gitee-project/springboot_test,这里填写这个地址即可。
然后将前面Jenkins生成的密码配置在这里。
public class HelloController {
@RequestMapping("/hello")
public String hello() {
System.out.println(123);
return "hello Jenkins!";
}
}
Running as SYSTEM
Building in workspace C:\Users\chengqiuming\.jenkins\workspace\springboot_test
using credential 1786fe45-4aaa-4802-af7e-b8275b0bc409
> D:\Program Files\Git\bin\git.exe rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> D:\Program Files\Git\bin\git.exe config remote.origin.url https://gitee.com/cakin24/IDEA.git # timeout=10
Fetching upstream changes from https://gitee.com/cakin24/IDEA.git
> D:\Program Files\Git\bin\git.exe --version # timeout=10
> git --version # 'git version 2.19.2.windows.1'
using GIT_ASKPASS to set credentials
> D:\Program Files\Git\bin\git.exe fetch --tags --progress -- https://gitee.com/cakin24/IDEA.git +refs/heads/*:refs/remotes/origin/* # timeout=10
skipping resolution of commit remotes/origin/master, since it originates from another repository
> D:\Program Files\Git\bin\git.exe rev-parse "refs/remotes/origin/master^{commit}" # timeout=10
> D:\Program Files\Git\bin\git.exe rev-parse "refs/remotes/origin/origin/master^{commit}" # timeout=10
Checking out Revision a99432ec59d661ccf043a19f0c04b885dc4552e4 (refs/remotes/origin/master)
> D:\Program Files\Git\bin\git.exe config core.sparsecheckout # timeout=10
> D:\Program Files\Git\bin\git.exe checkout -f a99432ec59d661ccf043a19f0c04b885dc4552e4 # timeout=10
Commit message: "提交"
> D:\Program Files\Git\bin\git.exe rev-list --no-walk 128967774c5df82d2f73149819cdf63d9a020fbb # timeout=10
[springboot_test] $ cmd.exe /C "D:\program\apache-maven-3.6.3\bin\mvn.cmd -s C:\Users\chengqiuming\.m2\settings.xml -gs C:\Users\chengqiuming\.m2\settings.xml clean install && exit %%ERRORLEVEL%%"
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------------< com.imooc:demo >---------------------------
[INFO] Building demo 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ demo ---
[INFO] Deleting C:\Users\chengqiuming\.jenkins\workspace\springboot_test\target
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ demo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 2 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ demo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 8 source files to C:\Users\chengqiuming\.jenkins\workspace\springboot_test\target\classes
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ demo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\chengqiuming\.jenkins\workspace\springboot_test\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ demo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:\Users\chengqiuming\.jenkins\workspace\springboot_test\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ demo ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running TestMethod
2020-08-09 16:53:35 INFO SpringBootTestContextBootstrapper:308 - Neither @ContextConfiguration nor @ContextHierarchy found for test class [TestMethod], using SpringBootContextLoader
2020-08-09 16:53:35 INFO AbstractContextLoader:264 - Could not detect default resource locations for test class [TestMethod]: no resource found for suffixes {-context.xml, Context.groovy}.
2020-08-09 16:53:36 INFO SpringBootTestContextBootstrapper:248 - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
2020-08-09 16:53:36 INFO SpringBootTestContextBootstrapper:177 - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@3c22fc4c, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@460d0a57, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@47d90b9e, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@1184ab05, org.springframework.test.context.support.DirtiesContextTestExecutionListener@3aefe5e5, org.springframework.test.context.transaction.TransactionalTestExecutionListener@149e0f5d, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@1b1473ab, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@2f7c2f4f, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@6af93788, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@ef9296d, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@36c88a32, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@7880cdf3]
2020-08-09 16:53:36 INFO Version:21 - HV000001: Hibernate Validator 6.0.14.Final
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.3.RELEASE)
2020-08-09 16:53:37 INFO TestMethod:50 - Starting TestMethod on LAPTOP-BE0LNP1J with PID 13836 (started by chengqiuming in C:\Users\chengqiuming\.jenkins\workspace\springboot_test)
2020-08-09 16:53:37 INFO TestMethod:675 - No active profile set, falling back to default profiles: default
2020-08-09 16:53:37 INFO TestMethod:59 - Started TestMethod in 1.198 seconds (JVM running for 2.905)
{key=hello yaml}
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 3.909 s - in TestMethod
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO]
[INFO] --- maven-jar-plugin:3.1.1:jar (default-jar) @ demo ---
[INFO] Building jar: C:\Users\chengqiuming\.jenkins\workspace\springboot_test\target\demo-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.1.3.RELEASE:repackage (repackage) @ demo ---
[INFO] Replacing main artifact with repackaged archive
[INFO]
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ demo ---
[INFO] Installing C:\Users\chengqiuming\.jenkins\workspace\springboot_test\target\demo-1.0-SNAPSHOT.jar to D:\.m2\repos\com\imooc\demo\1.0-SNAPSHOT\demo-1.0-SNAPSHOT.jar
[INFO] Installing C:\Users\chengqiuming\.jenkins\workspace\springboot_test\pom.xml to D:\.m2\repos\com\imooc\demo\1.0-SNAPSHOT\demo-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 13.229 s
[INFO] Finished at: 2020-08-09T16:53:41+08:00
[INFO] ------------------------------------------------------------------------
SSH: Connecting from host [LAPTOP-BE0LNP1J]
SSH: Connecting with configuration [centos] ...
SSH: EXEC: STDOUT/STDERR from command [cd /home/ldp/app/
./stop.sh
./start.sh] ...
Stop Procedure : demo-1.0-SNAPSHOT.jar
old Procedure pid:
Start the program : demo-1.0-SNAPSHOT.jar
-------Starting-------
start success
2020-08-09 16:53:53 INFO Version:21 - HV000001: Hibernate Validator 6.0.14.Final
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.3.RELEASE)
2020-08-09 16:53:55 INFO Application:50 - Starting Application v1.0-SNAPSHOT on centos with PID 13674 (/home/ldp/app/demo-1.0-SNAPSHOT.jar started by root in /home/ldp/app)
2020-08-09 16:53:55 INFO Application:675 - No active profile set, falling back to default profiles: default
2020-08-09 16:54:02 INFO TomcatWebServer:90 - Tomcat initialized with port(s): 8000 (http)
2020-08-09 16:54:03 INFO ContextLoader:296 - Root WebApplicationContext: initialization completed in 7971 ms
2020-08-09 16:54:05 INFO ThreadPoolTaskExecutor:171 - Initializing ExecutorService 'applicationTaskExecutor'
2020-08-09 16:54:06 INFO TomcatWebServer:204 - Tomcat started on port(s): 8000 (http) with context path ''
2020-08-09 16:54:06 INFO Application:59 - Started Application in 13.74 seconds (JVM running for 16.411)
鍚姩鎴愬姛锛侊紒
鏍瑰湴鍧�: http://127.0.0.1:8000
鐧诲綍鎺ュ彛: http://127.0.0.1:8000/user/login?loginName=test&password=test
娴嬭瘯
2020-08-09 16:55:00 INFO DispatcherServlet:524 - Initializing Servlet 'dispatcherServlet'
2020-08-09 16:55:00 INFO DispatcherServlet:546 - Completed initialization in 25 ms
123
SSH: Disconnecting configuration [centos] ...
ERROR: Exception when publishing, exception message [Exec timed out or was interrupted after 120,001 ms]
Build step 'Send build artifacts over SSH' changed build result to UNSTABLE
Finished: UNSTABLE
https://blog.csdn.net/qq_38946537/article/details/107871976
https://www.cnblogs.com/wfd360/p/11314697.html