Jenkins 部署 远程调试

作者: Java_or_PHP | 来源:发表于2018-03-26 16:52 被阅读864次

1. 安装git、java JDK

如果系统中已经安装了jdk,可以直接使用系统中的Java

例如:

aa@ubuntu:~$ uname -a
Linux ubuntu 4.10.0-42-generic #46-Ubuntu SMP Mon Dec 4 14:38:01 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
aa@ubuntu:~$

aa@ubuntu:~$ which java
/usr/bin/java
aa@ubuntu:~$ java -version
openjdk version "1.8.0_151"
OpenJDK Runtime Environment (build 1.8.0_151-8u151-b12-0ubuntu0.16.04.2-b12)
OpenJDK 64-Bit Server VM (build 25.151-b12, mixed mode)
aa@ubuntu:~$ file /usr/bin/java
/usr/bin/java: symbolic link to /etc/alternatives/java
aa@ubuntu:~$ file /etc/alternatives/java
/etc/alternatives/java: symbolic link to /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java

可见,在这台ubuntu里面已经安装了JDK,安装路径在 /usr/lib/jvm/java-8-openjdk-amd64

可以把这个JDK的路径加入到 .bashrc

aa@ubuntu:~$ echo export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64 >> ~/.bashrc
aa@ubuntu:~$ . .bashrc
aa@ubuntu:~$ echo $JAVA_HOME
/usr/lib/jvm/java-8-openjdk-amd64

如果没有安装Jdk

aa@ubuntu:~$ sudo apt-cache search jdk | grep jdk
[sudo] password for aa:
default-jdk - Standard Java or Java compatible Development Kit
default-jdk-doc - Standard Java or Java compatible Development Kit (documentation)
default-jdk-headless - Standard Java or Java compatible Development Kit (headless)
gcj-5-jdk - GCJ and Classpath development tools for Java(TM)
openjdk-8-dbg - Java runtime based on OpenJDK (debugging symbols)
openjdk-8-demo - Java runtime based on OpenJDK (demos and examples)
openjdk-8-doc - OpenJDK Development Kit (JDK) documentation
openjdk-8-jdk - OpenJDK Development Kit (JDK)
openjdk-8-jdk-headless - OpenJDK Development Kit (JDK) (headless)
openjdk-8-jre - OpenJDK Java runtime, using Hotspot JIT
openjdk-8-jre-headless - OpenJDK Java runtime, using Hotspot JIT (headless)
openjdk-8-source - OpenJDK Development Kit (JDK) source files
gcj-4.8-jdk - GCJ and Classpath development tools for Java(TM)
gcj-4.9-jdk - GCJ and Classpath development tools for Java(TM)
gcj-jdk - gcj and Classpath development tools for Java(TM)
libhtsjdk-java - Java API for high-throughput sequencing data (HTS) formats
libhtsjdk-java-doc - Documentation for the java HTSJDK library
openjdk-8-jre-dcevm - Alternative VM for OpenJDK 8 with enhanced class redefinition
openjdk-8-jre-zero - Alternative JVM for OpenJDK, using Zero/Shark
openjdk-9-dbg - Java runtime based on OpenJDK (debugging symbols)
openjdk-9-demo - Java runtime based on OpenJDK (demos and examples)
openjdk-9-doc - OpenJDK Development Kit (JDK) documentation
openjdk-9-jdk - OpenJDK Development Kit (JDK)
openjdk-9-jdk-headless - OpenJDK Development Kit (JDK) (headless)
openjdk-9-jre - OpenJDK Java runtime, using Hotspot JIT
openjdk-9-jre-headless - OpenJDK Java runtime, using Hotspot JIT (headless)
openjdk-9-source - OpenJDK Development Kit (JDK) source files
uwsgi-plugin-jvm-openjdk-8 - Java plugin for uWSGI (OpenJDK 7)
uwsgi-plugin-jwsgi-openjdk-8 - JWSGI plugin for uWSGI (OpenJDK 7)
uwsgi-plugin-ring-openjdk-8 - Closure/Ring plugin for uWSGI (OpenJDK 7)
uwsgi-plugin-servlet-openjdk-8 - JWSGI plugin for uWSGI (OpenJDK 7)
openjdk-8-jre-jamvm - Transitional package for obsolete JamVM for OpenJDK

直接 apt-get install openjdk-9-jdk 或者 openjdk-8-jdk 即可。

2. 安装mvn,如果需要

步骤基本跟安装JDK一样。

3. Gradle,使用gradle wrapper。

Ubuntu 自带的gradle 版本太老,对于 CompileOnly 不支持。

aa@ubuntu:~$ which gradle
/usr/bin/gradle
aa@ubuntu:~$ gradle -version

------------------------------------------------------------
Gradle 2.10
------------------------------------------------------------

Build time:   2016-01-26 15:17:49 UTC
Build number: none
Revision:     UNKNOWN

Groovy:       2.4.5
Ant:          Apache Ant(TM) version 1.9.6 compiled on July 8 2015
JVM:          1.8.0_151 (Oracle Corporation 25.151-b12)
OS:           Linux 4.10.0-42-generic amd64

4. 设置环境变量

export M2_HOME=/usr/share/maven
export M2=$M2_HOME/bin
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64

5. 下载tomcat、Jenkins

wget http://mirrors.jenkins.io/war-stable/latest/jenkins.war
wget http://mirrors.hust.edu.cn/apache/tomcat/tomcat-8/v8.5.29/bin/apache-tomcat-8.5.29.tar.gz

解压tomcat。
为了实施远程调试,需要分别把tomat放在两个目录下,分别叫做:

$ ls
apache-tomcat-8.5.29_jenkins  apache-tomcat-8.5.29_projects

之所以需要把tomcat分别放在两个目录下,一个用来启动Jenkins,一个用来启动项目,是因为在后面配置远程调试,打开JDWP 的端口的时候,总是报错:

gradle clean build run
ERROR: transport error 202: bind failed: Address already in use

把 jenkins.war 放到 apache-tomcat-8.5.29_jenkins/webapps/下

mv jenkins.war apache-tomcat-8.5.29_jenkins/webapps/

修改启动Jenkins的tomcat的监听端口,监听7070端口。

用来启动项目的tomcat的端口,监听8080

启动监听7070端口的tomcat

在浏览器地址栏输入:http://192.168.2.180:7070/jenkins/

7. 按照导航,解锁密码、安装插件、添加用户

解锁密码 日志配置路径 安装插件 image.png

这里,因为项目是用的gitlab,所以特意安装了gitlab的插件

安装插件 安装gitlab插件

8. 全局工具配置:JDK 和 git

Jdk

JDK Git

9. 项目配置:

新建任务 新建任务

选择“构建一个自由风格的软件项目”。

任务名称不要使用中文

任务配置 gitlab 地址配置

注意:这里的项目地址,一定要填

https://gitlab.com/company/project.git 而不是 https://gitlab.com/company/project

否则,会报:

error: RPC failed; result=22, HTTP code = 404
fatal: The remote end hung up unexpectedly
构建触发

构建这一步是最重要的配置:


构建

选择“Use Gradle Wrapper”,并且选择下面的 “Force GRADLE_USER_HOME to use workspace”,同时跳过 test, 使用 -x 开关。

10. 构建:

点击“立即构建”之后,可以点击当前构建右侧向下的箭头,点击“控制台输出”,查看构建日志。

构建结果 构建日志

11. 部署:

构建完成之后,可以部署在本地或者远程服务器上。

本地部署:

构建完成之后,添加执行shell 脚本:

脚本

在文本框里输入以下脚本:

#!/bin/bash 
#copy file and restart tomcat 

export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
export CATALINA_HOME=/home/aa/apache-tomcat-8.5.29_projects
export CATALINA_BASE=/home/aa/apache-tomcat-8.5.29_projects
export BUILD_ID=dontKillMe

tomcat_path=/home/aa/apache-tomcat-8.5.29_projects
project=checkup
war_name=demo.war 
#宿主服务器的ip何端口
war_path=http://192.168.2.180:8080/jenkins/job/${project}/ws/targetserver_port=8080
#编译好的war包存放地址
file_path=~/.jenkins/workspace/${project}/build/libs

$tomcat_path/bin/shutdown.sh 

sleep 3s 
echo "rm -rf ${tomcat_path}/webapps/ROOT/*"
rm -rf ${tomcat_path}/webapps/ROOT/*
cd $file_path
cp ${war_name} ${tomcat_path}/webapps/ROOT/
cd $tomcat_path/webapps/ROOT/
unzip ${war_name}
rm -rf ${war_name}
sleep 5s 
#$tomcat_path/bin/startup.sh
cd $tomcat_path/bin/
./startup.sh
echo "server restarted"


脚本

12. 远程调试:

假设192.168.2.180是远程构建、测试服务器。要完成jenkins构建部署、远程调试,需要起两个tomcat分别监听不同的端口。

假设部署了Jenkins的tomcat监听了7070端口。需要远程调试的tomcat监听了8080端口。
监听7070端口的tomcat所在的路径是: ~/apache-tomcat-8.5.29_jenkins
监听8080端口的tomcat所在的路径是: ~/apache-tomcat-8.5.29_projects

打开IntelliJ IDEA,build-> Edit Build Configuration


remote

添加remote 选项

remote remote

复制JVM的相关参数

-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005

配置服务器Tomcat

进入~/apache-tomcat-8.5.29_projects/bin目录下

vi catalina.sh
#在# OS specific support.上面添加
export JAVA_OPTS='-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005'

jdwp

使用 $tomcat_path/bin/shutdown.sh 来关闭监听8080端口的tomcat,会带来一个问题,就是这个脚本会把监听7070端口的部署了Jenkins的tomcat也杀掉。

shutdown.sh 这个脚本会接受一个参数:CATALINA_PID。这个参数指向一个文件,文件里保存了指定要杀死的tomcat的pid。

问题在于使用 $tomcat_path/bin/startup.sh 并没有指定这个参数,告诉它启动的时候把pid写入一个文件。所以这一步需要先做。

start.sh

export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
export CATALINA_HOME=/home/aa/projects
export CATALINA_BASE=/home/aa/projects
export CATALINA_PID=${CATALINA_BASE}/tomcat.pid

${CATALINA_HOME}/bin/catalina.sh start

stop.sh

export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
export CATALINA_HOME=/home/aa/apache-tomcat-8.5.29_projects
export CATALINA_BASE=/home/aa/apache-tomcat-8.5.29_projects
export CATALINA_PID=${CATALINA_BASE}/tomcat.pid

${CATALINA_HOME}/bin/catalina.sh stop

把这两个脚本放到 ~/apache-tomcat-8.5.29_projects/bin 路径下。修改上面的“本地部署”步骤里的脚本

export M2_HOME=/usr/share/maven
export M2=$M2_HOME/bin
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64

export CATALINA_HOME=/home/aa/apache-tomcat-8.5.29_projects
export CATALINA_BASE=/home/aa/apache-tomcat-8.5.29_projects
export BUILD_ID=dontKillMe

#我的Tomcat位置
tomcat_path=/home/aa/apache-tomcat-8.5.29_projects

#我的项目编译的名称
project=checkup

#最后打包的war名称
war_name=demo.war

#宿主服务器的ip何端口
war_path=http://192.168.2.180:8080/jenkins/job/${project}/ws/targetserver_port=8080
#编译好的war包存放地址
file_path=~/.jenkins/workspace/${project}/build/libs

$tomcat_path/bin/stop.sh
sleep 3s 
echo "rm -rf ${tomcat_path}/webapps/ROOT/*"
rm -rf ${tomcat_path}/webapps/ROOT/*
cd $file_path
cp ${war_name} ${tomcat_path}/webapps/ROOT/
cd $tomcat_path/webapps/ROOT/
unzip ${war_name}
rm -rf ${war_name}
sleep 5s 
cd $tomcat_path/bin/
./start.sh
echo "server restarted"

这样就解决了调用 ~/apache-tomcat-8.5.29_projects/bin/shutdown.sh 连监听7070端口的tomcat也杀掉的问题了。

13. 问题:

13.1. 权限问题:

最好是在自己的目录下单独放一个tomcat来启动jenkins,而不是使用系统安装的jenkins,这会涉及到权限问题,会很非常麻烦。

Error
Unable to create the home directory ‘/usr/share/tomcat7/.jenkins’. This is most likely a permission problem.

或者,根据网上的说法,可以设置一个JENKINS_HOME的变量:

To change the home directory, use JENKINS_HOME environment variable or set the JENKINS_HOME system property. See Container-specific documentation for more details of how to do this.

13.2. 202的问题:

gradle clean build run
ERROR: transport error 202: bind failed: Address already in use

这个情况,一般是在在tomcat/bin/catalina.sh中有如下的配置:

CATALINA_OPTS="-server -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=12343"

即:打开了远程调试的端口。

相关文章

网友评论

    本文标题:Jenkins 部署 远程调试

    本文链接:https://www.haomeiwen.com/subject/jqrrcftx.html