On a completed "when git push client code into the gitlab, Jenkins will go immediately gitlab pulling code and build."
Objective: After completion of this section jenkins automated build, deploy automatically in the jar package to the application server and start the service.
machine:
- jenkins server: 10.211.55.4
- Application server: 10.211.55.3
A, jenkins mounting publish-over-ssh
Plug process with the former.
Second, the establishment ssh trust (which is the principle of ssh)
1, jenkins generate the key pair
Performed jenkins server
- ssh-keygen -t rsa -C "[email protected]"
At this time, ~ / .ssh / directory will generate id_rsa (private key) and id_rsa.pub (public key),
2, paste the public key to the application server
The contents of the copy and paste id_rsa.pub application server ~ / .ssh / authorized_keys,
If this file does not create yourself.
3, the private key attached to the server ssh server at jenkins
"System Management" -> "System Settings"
Description:
- Passphrase: performing key pair generation command when we set.
- Key: jenkins generated private key id_rsa content.
- SSH Server:
- Name: casual
- Hostname: The application server ip
- Username: random
- Remote Directory: jenkins upload directory to upload files to the application server.
After configuration is complete, click on the "Test Configuration", displaying "success", the connection is successful! ! !
Third, the Configuration Service
Click "add a build operation" in service "mytest-service1" of "Configuration",
Description:
- source files:上传文件(这里是jar包)的所在位置,其相对的路径是~/.jenkins/workspace/mytest-service1
- remove prefix:删除掉source files的前缀target,否则会在应用服务器创建target目录,即:jar包存放在/data/jar/target中
- Exec command:上传文件后,在应用服务器上执行的远程命令
- 进入脚本service_start.sh所在的位置
- 执行service_start.sh脚本,并传入参数mytest-service1-1.0-SNAPSHOT.jar和8088
四、应用服务器
创建脚本/opt/script/service_start.sh
1 #!/bin/bash
2 export jar_name=$1
3 export port=$2
4 export java_home=/opt/jdk1.8.0_102
5 shutdown_second=5
6 echo "jar_name is ${jar_name}, port is ${port}"
7
8 pid="`${java_home}/bin/jps -l | grep ${jar_name} | awk '{print $1}'`"
9 echo "pid is ${pid}"
10
11 if [ -n "${pid}" ]
12 then
13 kill -9 ${pid}
14 sleep ${shutdown_second}
15 fi
16
17 cd /data/jar/
18 echo "start ${jar_name} process"
19 nohup ${java_home}/bin/java -jar -Dserver.port=${port} ${jar_name}>/data/log/${jar_name}.log &
20 echo "end ${jar_name} process"
说明:
- jar_name为命令行传入的第一个参数,即mytest-service1-1.0-SNAPSHOT.jar
- 注意:需要export
- port为命令行传入的第二个参数,即8088
- 指定java_home
- 注意:直接使用${JAVA_HOME},在应用服务器本机是可以的,但是通过jenkins远程执行就获取不到了
- 定义shutdown_second为5s
- 使用jps获取所有java进程,使用grep获取mytest-service1-1.0-SNAPSHOT.jar的那条进程,使用awk获取进程信息的第一个参数(即进程号pid)
- 若是在应用服务器本机,只要配置了JAVA_HOME,可以直接使用jps,但是通过jenkins远程执行就要写全了,包括后边的java命令。
- 如果pid不为空,kill该进程,睡眠5s
- 进入jar宝所在位置,执行启动命令,并且将日志输出到/data/log/mytest-service1-1.0-SNAPSHOT.jar.log中
- 若是在在应用服务器本机,可以直接nohup执行,若是通过jenkins远程执行,则需要将日志输出到一个应用服务器的一个文件中,否则jenkins的构建过程将一直等到超时失败为止。(还有/data/log/目录要提前建好,或者在脚本中建好)
五、测试
本地开发机,修改代码,
- git add --all
- git commit -m"test ci"
- git push origin HEAD:dev
查看jenkins输出console log,查看应用服务器jps java进程,在浏览器访问http://10.211.55.3:8088/test/hello