java - Jenkins Pipeline env.JAVA_HOME differs from sh printenv
问题描述
Jenkins 2.2024.2
Kubernetes Plugin 1.22.0
The env.JAVA_HOME
variable is getting set to a different value than what sh 'printenv'
shows. I had thought env
was a reflection of the environment. I also can't find where env.JAVA_HOME
is set. It doesn't reflect the value in the Global JDK configurations. I also grepped the Jenkins Master root directory for openjdk and only found the correct value.
bash-4.4# grep openjdk *
config.xml: <home>/usr/local/openjdk-8/jre</home>
Here's a pipeline demonstrating the behavior.
pipeline {
agent {
kubernetes { label 'java8-mvn3' }
}
stages {
stage('test') {
steps {
container ('java8-mvn3') {
echo env.JAVA_HOME
echo "++++++++++++++++++++++++++"
sh 'printenv | grep JAVA_HOME'
}
}
}
}
}
** OUTPUT **
[Pipeline] echo
/usr/lib/jvm/java-1.8-openjdk
[Pipeline] echo
++++++++++++++++++++++++++
[Pipeline] sh
+ printenv
+ grep JAVA_HOME
JAVA_HOME=/usr/local/openjdk-8
解决方案
根据手册页: http: //man7.org/linux/man-pages/man1/printenv.1.html
NOTE: your shell may have its own version of printenv, which usually supersedes the version described here. Please refer to your shell's documentation for details about the options it supports.
所以你很多人需要使用特定的外壳,比如sh '''#!/bin/bash -c ...
或者作为另一种选择导出全局/阶段定义环境级别所需的选项
推荐阅读
- html - 响应式切换不起作用,可能是拼写问题,但我找不到我的错误
- python - 数据框中的时区转换
- javascript - jQuery - 在一定数量的点击后用另一个替换图像
- c++ - 然后在 C++ 条件
- ruby-on-rails - 如何在 Ruby on Rails 中接收 JSON:API 嵌套属性的帖子?
- delphi - 为什么这个过程不是 TProc?
- java - 具有继承和引用的 Json 模式生成器
- javascript - Javascript 中的功能与非功能循环
- android - Snapchat 中的 Firebase 动态链接无法作为附加链接工作
- css - 如何使用带有 selenium web-driver 的 python 将数据发送到此复选框