如何使用DSL访问Jenkins环境变量?

时间:2015-07-29 17:51:17

标签: groovy jenkins

正如问题所说。我无法访问它。

尝试:

#1:

def env = System.getenv()
def BUILD_NUMBER= env["BUILD_NUMBER"]

结果:

访问BUILD_NUMBER

时为空

#2:

def BUILD_NUMBER= params["BUILD_NUMBER"]

结果:

访问BUILD_NUMBER时为空 但是,此解决方案适用于我创建的环境变量

5 个答案:

答案 0 :(得分:11)

找到答案:<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- Non focused states --> <item android:drawable="@android:color/transparent" android:state_focused="false" android:state_pressed="false" android:state_selected="false" /> <item android:drawable="@drawable/tab_selected_example" android:state_focused="false" android:state_pressed="false" android:state_selected="true" /> <!-- Focused states --> <item android:drawable="@drawable/tab_unselected_focused_example" android:state_focused="true" android:state_pressed="false" android:state_selected="false" /> <item android:drawable="@drawable/tab_selected_focused_example" android:state_focused="true" android:state_pressed="false" android:state_selected="true" /> <!-- Pressed --> <!-- Non focused states --> <item android:drawable="@drawable/tab_unselected_pressed_example" android:state_focused="false" android:state_pressed="true" android:state_selected="false" /> <item android:drawable="@drawable/tab_selected_pressed_example" android:state_focused="false" android:state_pressed="true" android:state_selected="true" /> <!-- Focused states --> <item android:drawable="@drawable/tab_unselected_pressed_example" android:state_focused="true" android:state_pressed="true" android:state_selected="false" /> <item android:drawable="@drawable/tab_selected_pressed_example" android:state_focused="true" android:state_pressed="true" android:state_selected="true" /> </selector>

答案 1 :(得分:4)

只是让大家知道我试过

def foo = build.buildVariableResolver.resolve("FOO")
println "FOO=$foo" 

有效。 请参考此处的示例 https://wiki.jenkins-ci.org/display/JENKINS/Groovy+plugin

答案 2 :(得分:1)

这将与最新版本的Jenkins和DSL插件一起使用:

def BUILD_NUMBER = getBinding().getVariables()['BUILD_NUMBER']

答案 3 :(得分:0)

echo 'Build No: ' + env.BUILD_NUMBER + '. Build URL: ' + env.BUILD_URL

可通过以下方式访问的环境变量列表: http://HOST/PATH_TO_JENKINS/env-vars.html/

如, http://localhost:8888/jenkins/env-vars.html/

答案 4 :(得分:0)

詹金斯版2.138.2

def gitUrl = binding.variables.get(“ GITLAB_URL”)