我在字符串中有一个Jenkins全局变量-如何评估它? [英] I have a Jenkins global variable in a string - how do I evaluate it?

查看:298
本文介绍了我在字符串中有一个Jenkins全局变量-如何评估它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要接受所有类型的全局Jenkins变量作为字符串(基本上是像ansible这样的系统的参数-存储在\vars中的模板).

I need to accept all kinds of global Jenkins variables as strings (basically as parameters to ansible like system - a template stored in \vars).

def proof = "\"${params.REPOSITORY_NAME}\""
echo proof 
def before = "\"\${params.REPOSITORY_NAME}\""
echo before
def after = Eval.me(before)
echo after

结果是:

[Pipeline] echo
"asfd"
[Pipeline] echo
"${params.REPOSITORY_NAME}"

groovy.lang.MissingPropertyException: No such property: params for class: Script1

  • 第一个回波证明param值确实存在.
  • 第二个回显是输入的实际外观.
  • 第三个回声应该已经发出asdf,但是我得到了异常.
    • the first echo proves that the param value actually exists.
    • the second echo is the what the input actually looks like.
    • the third echo should have emitted asdf instead I get the exception.
    • 有什么想法吗?我花了几个小时:-(

      Any ideas? I'm hours into this :-(

      推荐答案

      您可能要检查: 常规:有一个字段名称,需要设置值并且不想使用switch

      如果您具有:xyz="REPOSITORY_NAME"并想要参数REPOSITORY_NAME的值,则可以简单地使用:

      In case you have: xyz="REPOSITORY_NAME" and want the value of the parameter REPOSITORY_NAME you can simply use:

      def xyz = "REPOSITORY_NAME"
      echo params."$xyz" // will print the value of params.REPOSITORY_NAME
      

      如果变量xyz必须包含完整字符串,包括params.,则可以使用以下解决方案

      In case if your variable xyz must hold the full string including params. you could use the following solution

      @NonCPS
      def split(string) {
          string.split(/\./)
      }
      
      def xyz = "params.REPOSITORY_NAME"
      
      def splitString = split(xyz)
      echo this."${splitString[0]}"."${splitString[1]}"  // will print the value of params.REPOSITORY_NAME
      

      第二变体

      如果要指定环境变量名称作为参数,可以使用:

      2nd Variant

      In case you want to specify an environment variable name as parameter you can use:

       env."${params.REPOSITORY_NAME}"
      

      在普通的groovy中,env[params.REPOSITORY_NAME]可以工作,但是在管道中,它不能在沙箱中工作.

      In plain groovy env[params.REPOSITORY_NAME] would work but in pipeline this one would not work inside the sandbox.

      这样,您首先要检索REPOSITORY_NAME的值,然后将其用作环境变量的键.

      That way you first retrieve the value of REPOSITORY_NAME and than use it as key to a environment variable.

      直接使用env.REPOSITORY_NAME不同于尝试使用REPOSITORY_NAME本身作为键.

      Using directly env.REPOSITORY_NAME will not be the same as it would try to use REPOSITORY_NAME itself as the key.

      例如说您有一个名为MyJob的工作,其脚本如下:

      E.g. say you have a job named MyJob with the following script:

      assert(params.MyParameter == "JOB_NAME")
      echo env."${params.MyParameter}"
      assert(env."${params.MyParameter}" == 'MyJob')
      

      这将在您确实将MyParameter参数设置为JOB_NAME的情况下将作业的名称(MyJob)打印到控制台.这两个断言都会通过.

      This will print the name of the job (MyJob) to the console assuming you did set the MyParameter parameter to JOB_NAME. Both asserts will pass.

      如果您要检索该节点的环境,请不要忘记先打开node{}块.

      Please don’t forget to open a node{} block first in case you want to retrieve the environment of that very node.

      这篇关于我在字符串中有一个Jenkins全局变量-如何评估它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆