Oracle 系统管理 - Linux 系统 - Backtrack 5 - 安全 - Juniper 技术 - Cisco 技术 - 思科模拟器 - Cisco 认证 - Cisco ios 下载

您现在的位置是:Docker > Jenkins > Jenkins Pipeline引入指定的环境变量

Jenkins Pipeline引入指定的环境变量

时间:2018-05-22 21:02  来源:未知  阅读次数: 复制分享 我要评论

环境

操作系统: win7
虚拟机:centos6.5
工具:CRT

场景

最近在公司想研究下JenkinsPipeline
在参考官方教程时,
每次构建我总是失败:

这里写图片描述

上面错误主要是这句:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile (default-testCompile) on project simple-maven-project-with-tests: Fatal error compiling: 无效的目标版本: 1.7 -> [Help 1]
  • 1

这句话的意思是,我要编译的项目要求jdk必须是1.7,而我Jenkins构建时,却不是jdk1.7的环境。 这我就更纳闷啦!

我的配置

maven全局配置: 这里写图片描述

jdk配置:

这里写图片描述

git配置:

这里写图片描述

maven 配置:

这里写图片描述

可以看出,我jdk明明配置的就是jdk1.7可是为什么就是不行呢?

猜测一,可能Jenkins启动配置文件读取了其他版本的jdk

于是我把启动文件只保留jdk1.7的路径:

# 打开配置文件
vim /etc/init.d/jenkins

# 修改后的结果
for candidate in /usr/java/jdk1.7.0_51/bin/java /usr/java/jdk1.7.0_51/jre/bin/java
do
  [ -x "$JENKINS_JAVA_CMD" ] && break
  JENKINS_JAVA_CMD="$candidate"
done

# 原本是
/etc/alternatives/java /usr/lib/jvm/java-1.6.0/bin/java /usr/lib/jvm/jre-1.6.0/bin/java /usr/lib/jvm/java-1.5.0/bin/java /usr/lib/jvm/jre-1.5.0/bin/java /usr/bin/java

# 其中/etc/alternatives/java,在服务器上指向的Java版本却是1.6,也就是说它可能读取到了这个配置
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

带着欢喜重启Jenkins

service jenkins stop
service jenkins start
  • 1
  • 2

但是还是失败啦!

猜测二,是不是服务器上maven读取的Java版本不对

在服务器上执行:

mvn -v

[root@master01 pipeline_test]# mvn -v
Apache Maven 3.2.3 (33f8c3e1027c3ddde99d3cdebad2656a31e8fdf4; 2014-08-12T04:58:10+08:00)
Maven home: /home/activemq/apache-maven-3.2.3
Java version: 1.7.0_51, vendor: Oracle Corporation
Java home: /usr/java/jdk1.7.0_51/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "2.6.32-431.29.2.el6.x86_64", arch: "amd64", family: "unix"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

可以看出是jdk1.7


正真的原因

这时我纠结了很久,甚至去github上去截图留言;

后来我在命令框里执行了这么一句:

node {
    sh "java -version"
    sh "which java"
}
  • 1
  • 2
  • 3
  • 4

得到的结果:

[pipeline_test] Running shell script
+ java -version
# java version "1.6.0_32"
OpenJDK Runtime Environment (IcedTea6 1.13.4) (rhel-7.1.13.4.el6_5-x86_64)
OpenJDK 64-Bit Server VM (build 23.25-b01, mixed mode)
[pipeline_test] Running shell script
+ which java
# /usr/bin/java
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

注意看我用井号注释的,可以看出虽然我在配置里配置了jdk的路径是1.7的,但是在pipeline里读取到的环境变量却是/usr/bin/java,而这个指向正好是jdk1.6的。

[root@master01 pipeline_test]# cd /usr/bin
[root@master01 bin]# ll | grep "java"
lrwxrwxrwx    1 root root          22 Sep 26  2014 java -> /etc/alternatives/java
lrwxrwxrwx    1 root root          23 Sep 26  2014 javac -> /etc/alternatives/javac
lrwxrwxrwx    1 root root          25 Sep 26  2014 javadoc -> /etc/alternatives/javadoc
lrwxrwxrwx    1 root root          23 Sep 26  2014 javah -> /etc/alternatives/javah
lrwxrwxrwx    1 root root          23 Sep 26  2014 javap -> /etc/alternatives/javap
lrwxrwxrwx.   1 root root          28 Oct 30  2013 javaws -> /usr/java/default/bin/javaws
lrwxrwxrwx.   1 root root          30 Oct 30  2013 jcontrol -> /usr/java/default/bin/jcontrol


[root@master01 bin]# cd /etc/alternatives
[root@master01 alternatives]# ll | grep "java"
lrwxrwxrwx 1 root root 55 Sep 26  2014 appletviewer -> /usr/lib/jvm/java-1.6.0-openjdk.x86_64/bin/appletviewer
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

解决办法

找到具体原因后,就知道怎么解决啦! 因为服务器是公司的,所以我不能随便删除文件。 既然在配置上,我们没法解决,那我们就在命令框里引入指定jdk就行了。

node {

    git url: 'https://github.com/jglick/simple-maven-project-with-tests.git'

    def mvnHome = tool 'M3.2.3'
    // 这里就是引入我们在全局配置中配置的jdk;写法:tool 名称
    def jdk77 = tool 'jdk1.7'
    // 再把变量加入到环境变量中
    env.PATH = "${jdk77}/bin:${mvnHome}/bin:${env.PATH}"
    sh "mvn -B verify"
    sh "echo '$PATH'"
    sh "which java"
    //sh "echo ${JOB_NAME}"
    //sh "echo ${env.JOB_NAME}"
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
// 部分内容
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running test.SomeTest
Tests run: 6, Failures: 0, Errors: 0, Skipped: 1, Time elapsed: 0.002 sec - in test.SomeTest
Running test.OtherTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec - in test.OtherTest

Results :

Tests run: 7, Failures: 0, Errors: 0, Skipped: 1

[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ simple-maven-project-with-tests ---
[WARNING] JAR will be empty - no content was marked for inclusion!
[INFO] Building jar: /home/jenkins/dataspace/workspace/pipeline_test/target/simple-maven-project-with-tests-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.660 s
[INFO] Finished at: 2017-11-17T18:10:51+08:00
[INFO] Final Memory: 22M/964M
[INFO] ------------------------------------------------------------------------
[Pipeline] sh
[pipeline_test] Running shell script
+ which java
/usr/java/jdk1.7.0_51/bin/java
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
相关资讯