一. 概述

       传统的发布版本,是由运维通过手动发布的方式来发布。这种发布方式存在效率低下,手动发布成本高,存在操作失误风险等问题。部署大版本少则几小时,多则一两天的停机时间,已常态化;同时需要多部门长时间配合,最后大家身心俱疲,苦不堪言。如果采用自动化部署方式,上述的问题均能得到解决。

       我司于去年实现了所有服务,所有环境的部署自动化。在技术上,选用了Jenkins作为部署工具,在其基础上通过二次开发封装,并以UI界面形式展示。Jenkins是一个开源项目,提供了一种易于使用的持续集成系统,使开发者从繁杂的集成中解脱出来,专注于更为重要的业务逻辑实现上。同时 Jenkins 能实施监控集成中存在的错误,提供详细的日志文件和提醒功能。

       下面总结在Jenkins部署过程中遇到的常见问题,并列出对应的解决方案,供读者自查。

二. 问题汇总

1. Exec timed out or was interrupted after 120,001 ms

现象

ERROR: Exception when publishing, exception message [Exec timed out or was interrupted after 120,001 ms]
Build step 'Send files or execute commands over SSH' changed build result to UNSTABLE
Finished: UNSTABLE

解决方案

       Jenkins的Job在执行 "Send files or execute commands over SSH" 的过程中超时了,默认超时时间为120000ms,也就是2分钟。因此,解决方案有以下两种:

<1> 缩短这一步骤的部署时间

将job的部署时间控制在2分钟内即可。

<2> 配置更长的超时时间

Send build artifacts over SSH ---> Advanced ---> Exec timeout (ms) 中设置。

比如将超时时间调整为300000ms(也即5分钟),问题解决。

 

2. Couldn't find any revision to build

现象

ERROR: Couldn't find any revision to build. Verify the repository and branch configuration for this job.
Finished: FAILURE

解决方案

       在Job的配置中,我们使用Git作为拉取代码的托管仓库,但在部署过程中,不存在该分支或者版本号,导致Jenkins拉取不到对应Job的代码。解决方案为:在对应的源码管理中填写正确的分支名称或者正确的版本号。

Source Code Management  --> Branch Specifier (blank for 'any')

注意:如果配置了参数化构建过程,则需要保证默认值为正确的分支。

同时,如果使用了Jenkins的API进行调用,则需要保证输入的分支名称是正确存在的。

 

3. Could not create or change to directory

现象

SSH: Disconnecting configuration
ERROR: Exception when publishing, exception message [Could not create or change to directory. Directory xxx ]
Build step 'Send files or execute commands over SSH' changed build result to UNSTABLE

解决方案

       造成该错误的原因为Jenkins无法创建目录,这主要是因为:"连接到该SSH的用户没有权限" 或者 "服务器的磁盘空间满了"。

因此,针对上面两种可能的情况,解决方案分别如下:

<1> 连接到该SSH的用户没有权限

针对这种情况,需要将该SSH用户所操作的目录,赋予权限。操作命令为:

chown -R name:pwd /xxx

其中name,pwd为对应SSH用户的账号密码,/xxx为需要赋予权限的目录名称。注意必须要切换为root用户下操作。

例子: chown -R  zz:123456  /zz/ms

<2> 服务器的磁盘空间满了

可以通过df,du命令查看磁盘占用情况,清掉无用的资源,腾出空间。

df -hl
du -h --max-depth=1

其中, df 命令是linux系统以磁盘分区为单位查看文件系统,可以加上参数查看磁盘剩余空间信息。
而 du 命令可以查看当前目录下各文件夹的大小。

 

4. Build step 'Invoke top-level Maven targets' marked build as failure

现象

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war (default-war) on project xxx: Failed to copy file for artifact [xxx:jar:0.0.1:compile]: File /root/.m2/repository/xxx/xxx/xxx/xxx.jar does not exist -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war (default-war) on project xxx: Failed to copy file for artifact [xxx:jar:0.0.1:compile]
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956)
at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)

[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] For more information about the errors and possible solutions, please read the following articles:
Build step 'Invoke top-level Maven targets' marked build as failure
Finished: FAILURE

解决方案

       这里使用了Maven进行打包,且由 Failed to execute goal xxx 内容可以看出,这里是Maven编译打包过程中报错。根据相应的提示排查项目代码即可。例如这里的报错内容为:/root/.m2/repository/xxx/xxx/xxx/xxx.jar 不存在,因此在Maven仓库上传这个jar包即可。

 

5. Failed to connect and initialize SSH connection

现象

SSH: Connecting from host [JR-JDH-SITAPP3]
SSH: Connecting with configuration [10.0.17.234-jdh] ...
ERROR: Exception when publishing, exception message [Failed to connect and initialize SSH connection. Message: [Failed to connect SFTP channel. Message [failed to send channel request]]]
Build step 'Send files or execute commands over SSH' changed build result to UNSTABLE
Finished: UNSTABLE

解决方案

       由报错提示可知,在SSH阶段,Jenkins无法与需要连接到的服务器建立SSH连接。因此解决方案为:检查Jenkins与需要连接的服务器的网络策略是否开通。

 

6. Compilation failure

现象

Waiting for Jenkins to finish collecting data
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project jdh-invoice: Compilation failure
[ERROR] /xxx/xxxImpl.java:[111,62] cannot find symbol
[ERROR] symbol: variable p1001
[ERROR] location: class com.xxx.xxx.xxx.xxxImpl
[ERROR] -> [Help 1]

解决方案

项目编译报错,根据报错提示修改代码,重新部署即可。

 

7. Exec exit status not zero

现象

mv: cannot stat ‘/xxx/xxx/xxx.jar’: No such file or directory
SSH: EXEC: completed after 200 ms
SSH: Disconnecting configuration [10.0.112.59-fuhsi] ...
ERROR: Exception when publishing, exception message [Exec exit status not zero. Status [1]]
Build step 'Send build artifacts over SSH' changed build result to UNSTABLE
Finished: UNSTABLE

解决方案

       执行退出状态不为0,意味着执行出现异常。这里根据提示,No such file or directory,在shell脚本中执行了mv命令,且找不到xxx.jar,所以报错。解决方案为,根据错误提示,修改部署脚本即可。

 

8. Cannot find /Tomcat-7.0.39/bin/catalina.sh

现象

Cannot find /Tomcat-7.0.39/bin/catalina.sh

The file is absent or does not have execute permission

This file is needed to run this program

SSH: EXEC: completed after 8,008 ms

SSH: Disconnecting configuration [10.0.17.234-jdh] ...

ERROR: Exception when publishing, exception message [Exec exit status not zero. Status [1]]

Build step 'Send files or execute commands over SSH' changed build result to UNSTABLE

Finished: UNSTABLE

解决方案

报错原因为,sh脚本没有执行权限。因此解决方案为,在tomcat 的bin目录下,执行如下命令:

chmod +x *.sh  

然后重新部署即可。

 

9. Neither the JAVA_HOME nor the JRE_HOME environment variable is defined

现象

Neither the JAVA_HOME nor the JRE_HOME environment variable is defined

At least one of these environment variable is needed to run this program

SSH: EXEC: completed after 7,808 ms

SSH: Disconnecting configuration [10.0.17.xxx-xxx] ...

ERROR: Exception when publishing, exception message [Exec exit status not zero. Status [1]]

Build step 'Send files or execute commands over SSH' changed build result to UNSTABLE

Finished: UNSTABLE

解决方案

Tomcat 找不到Java的环境变量,需要手动添加。解决方案为,在当前 Tomcat 的目录下,新增 setenv.sh,并插入以下内容:

JAVA_HOME=/usr/java/jdk1.7.0_65

JAVA_JRE=/usr/java/jdk1.7.0_65/jre

保存并退出,重新部署即可。

 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐