2017-01-25 74 views
0

我想将当前正在执行的Jenkins作业的名称放入环境变量中,稍后在我的管道中使用,不需要文件夹名称。我假设我需要这样的东西:如何将JOB_NAME排除文件夹放入Jenkins的环境变量中?

withEnv(['JOB_BASE_NAME=JOB_NAME.split('/').last()']) { 
    echo "Job base name: ${JOB_BASE_NAME}" 
} 

,但我得到一个错误:

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: 
    unclassified method java.lang.String div java.lang.String 
+0

也许[这](http://stackoverflow.com/questions/33573568/sandbox-is-preventing-me-from-formatting-a-string)帮助 – fishi

回答

0

我的工作了。如果任何人发现它是有用的:

def jobBaseName = "${env.JOB_NAME}".split('/').last() 
echo "Job Name (excl. path): ${jobBaseName}" 
1

在詹金斯文档,你有"Working with the Environment"节,其中提到:

The full list of environment variables accessible from within Jenkins Pipeline is documented at localhost:8080/pipeline-syntax/globals#env , assuming a Jenkins master is running on localhost:8080

如果你跟随链接,你可以找到JOB_BASE_NAME已经詹金斯提供开箱即用的所以这正是你想要的。

JOB_BASE_NAME - Short Name of the project of this build stripping off folder paths, such as "foo" for "bar/foo".

+0

嘿嘿感谢指出了这一点给我!这是新的吗? –

相关问题