Jenkins --windows搭建练手

首先呢,我也是简单玩玩,下了windows 的安装然后安装插件,大佬请手下留情别喷太狠啊。。。

1. 将代码上传到Github

SVN也可以,这里我就用Github了。

先去下载Git官网下载Git-2.23.0-64-bit.exe

再去下载TortoiseGit,有了这个就不用老是打命令commit了。

1.1. 图形界面的更新代码

Git的项目初始化就不多说了,这里介绍一下用图形界面的代码更新步骤。

① Git Commit -> "master"

② Push

 

1.2. 我在将Git从1.X升级到2.X的过程中,遇到了

SourceTree error:1407742E:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert protocol version

的问题,将Git版本升级后也解决不了。

后来想起来,是不是在环境变量中配置了Git1.X的路径

果然,在系统里面的Path中找到了Git的配置。

原来Git2.X以后,默认路径放到了C:\Program Files\Git下,和原来1.X的路径不一样了。

所以需要把1.X的路径配置删除,否则还是会优先使用1.X的Git。

 

2. 搭建Jenkins环境

2.1. 去Jenkins官网下载war包

2.2. 启动Jenkins的war包

java -jar jenkins.war --httpPort=8080

启动后,访问http://localhost:8080即可。

使用的时候需要注册,这些就省略不说了。

2.3. Jenkins插件的安装

需要安装Maven,Git等插件,才能和项目关联使用

Manage Jenkins -> Manage Plugins -> Available里面找

2.4. 创建新项目

在Source Code Management里面选择Git,填入自己的仓库地址:

在Build Triggers里面,选择Poll SCM,在Schedule中填入 * * * * *    :

在Build中,设置pom.xml的路径

一开始可能会报找不到pom.xml的Error,因为Jenkins需要先从Git库中同步项目,等同步完以后,Error就会自动消失。

在Post Steps中,Add post-build stemp -> Execute Windows bath command

添加的windows命令如下:

D:\JenkinsTest\process.bat 6001
  1.  
    D:
  2.  
    del D:\JenkinsTest\springbootService-1.0-SNAPSHOT.jar
  3.  
    copy C:\Users\Administrator\.jenkins\workspace\SpringbootService\target\springbootService-1.0-SNAPSHOT.jar D:\JenkinsTest\springbootService-1.0-SNAPSHOT.jar
  4.  
    SET BUILD_ID=donKillMe
  5.  
    start javaw -Dhudson.util.ProcessTree.disable=true -jar D:\JenkinsTest\springbootService-1.0-SNAPSHOT.jar

效果如图:

其中,D:\JenkinsTest\process.bat的内容如下:

::demo
@echo off
::延迟环境变量扩展
setlocal enabledelayedexpansion
for /f "delims= tokens=1" %%i in ('netstat -aon ^| findstr %1') do (
set a=%%i)
::判断服务是否已经启动,如果启动则杀掉进程
if defined a (taskkill /F /pid "!a:~71,7!") else (echo Service does not exist)
::等待你按任意键结束
pause>nul
::执行时后面带上端口即可

命令说明:

① 创建D:\JenkinsTest目录

② process.bat 6001 

是为了查找是否有占用6001端口的进程,如果有,则关闭6001端口的进程,因为我这里的Service启用的是6001端口。

③ 删除D:\JenkinsTest目录下的Service的Jar包,并将Jenkins自动打包生成的Jar包拷过来

然后通过Start javaw -jar *.jar命令在后台启动jar包。

④ 由于Jenkins默认在自动Build完成后,会关闭所有子进程,所以用下面这个命令可以避免Service被关闭

SET BUILD_ID=donKillMe

⑤ 据说start javaw -Dhudson.util.ProcessTree.disable=true -jar *.jar也有用,但是我用下来好像子进程还是被关闭了。

 

3. 测试

现在,当我更新代码到Git上以后,Jenkins就会自动将代码打包成Jar,然后执行我预先写好的命令自动部署启动。

在Console Output中可以看到Jenkins的执行内容:

Started by user sun wenli
Running as SYSTEM
Building in workspace C:\Users\Administrator\.jenkins\workspace\SpringbootService
No credentials specified
> git.exe rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> git.exe config remote.origin.url https://github.com/sunroyi/springbootService.git # timeout=10
Fetching upstream changes from https://github.com/sunroyi/springbootService.git
> git.exe --version # timeout=10
> git.exe fetch --tags --force --progress https://github.com/sunroyi/springbootService.git +refs/heads/*:refs/remotes/origin/*
> git.exe rev-parse "refs/remotes/origin/master^{commit}" # timeout=10
> git.exe rev-parse "refs/remotes/origin/origin/master^{commit}" # timeout=10
Checking out Revision 7ee3c59e4573b4256e6949ebbaeb99c9d398a841 (refs/remotes/origin/master)
> git.exe config core.sparsecheckout # timeout=10
> git.exe checkout -f 7ee3c59e4573b4256e6949ebbaeb99c9d398a841
Commit message: "3"
> git.exe rev-list --no-walk 7ee3c59e4573b4256e6949ebbaeb99c9d398a841 # timeout=10
Parsing POMs
Established TCP socket on 58566
[SpringbootService] $ java -cp C:\Users\Administrator\.jenkins\plugins\maven-plugin\WEB-INF\lib\maven31-agent-1.13.jar;D:\maven\apache-maven-3.1.1\boot\plexus-classworlds-2.5.1.jar;D:\maven\apache-maven-3.1.1/conf/logging jenkins.maven3.agent.Maven31Main D:\maven\apache-maven-3.1.1 C:\Users\Administrator\.jenkins\war\WEB-INF\lib\remoting-3.29.jar C:\Users\Administrator\.jenkins\plugins\maven-plugin\WEB-INF\lib\maven31-interceptor-1.13.jar C:\Users\Administrator\.jenkins\plugins\maven-plugin\WEB-INF\lib\maven3-interceptor-commons-1.13.jar 58566
<===[JENKINS REMOTING CAPACITY]===>channel started
Executing Maven: -B -f C:\Users\Administrator\.jenkins\workspace\SpringbootService\pom.xml clean package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building springbootService 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.6.1:clean (default-clean) @ springbootService ---
[INFO] Deleting C:\Users\Administrator\.jenkins\workspace\SpringbootService\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ springbootService ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ springbootService ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 7 source files to C:\Users\Administrator\.jenkins\workspace\SpringbootService\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ springbootService ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\Administrator\.jenkins\workspace\SpringbootService\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ springbootService ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ springbootService ---
[INFO] No tests to run.
[JENKINS] Recording test results
[INFO]
[INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ springbootService ---
[INFO] Building jar: C:\Users\Administrator\.jenkins\workspace\SpringbootService\target\springbootService-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:1.5.8.RELEASE:repackage (default) @ springbootService ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 34.060s
[INFO] Finished at: Thu Sep 12 11:27:02 CST 2019
[INFO] Final Memory: 37M/403M
[INFO] ------------------------------------------------------------------------
Waiting for Jenkins to finish collecting data
[JENKINS] Archiving C:\Users\Administrator\.jenkins\workspace\SpringbootService\pom.xml to com.sun/springbootService/1.0-SNAPSHOT/springbootService-1.0-SNAPSHOT.pom
[JENKINS] Archiving C:\Users\Administrator\.jenkins\workspace\SpringbootService\target\springbootService-1.0-SNAPSHOT.jar to com.sun/springbootService/1.0-SNAPSHOT/springbootService-1.0-SNAPSHOT.jar
channel stopped
[SpringbootService] $ cmd /c call C:\Users\ADMINI~1\AppData\Local\Temp\jenkins2482229857112663558.bat
C:\Users\Administrator\.jenkins\workspace\SpringbootService>D:\JenkinsTest\process.bat 6001
成功: 已终止 PID 为 1299256 的进程。
[SpringbootService] $ cmd /c call C:\Users\ADMINI~1\AppData\Local\Temp\jenkins7623094155436157631.bat
C:\Users\Administrator\.jenkins\workspace\SpringbootService>D:
D:\workspace20160509\Jenkins>del D:\JenkinsTest\springbootService-1.0-SNAPSHOT.jar
D:\workspace20160509\Jenkins>copy C:\Users\Administrator\.jenkins\workspace\SpringbootService\target\springbootService-1.0-SNAPSHOT.jar D:\JenkinsTest\springbootService-1.0-SNAPSHOT.jar
已复制 1 个文件。
 
D:\workspace20160509\Jenkins>SET BUILD_ID=donKillMe
D:\workspace20160509\Jenkins>start javaw -Dhudson.util.ProcessTree.disable=true -jar D:\JenkinsTest\springbootService-1.0-SNAPSHOT.jar
D:\workspace20160509\Jenkins>exit 0
Process leaked file descriptors. See https://jenkins.io/redirect/troubleshooting/process-leaked-file-descriptors for more information
Finished: SUCCESS

 

结果验证:

posted @ 2020-08-06 16:16  aBiu--  阅读(381)  评论(0编辑  收藏  举报