简体   繁体   English

Jenkins-将数组/列表传递给参数化的远程构建

[英]Jenkins - Passing array/list to Parameterized Remote Build

I am using Jenkins to remotely run an Ansible playbook via the Publish Over SSH command. 我正在使用Jenkins通过Publish Over SSH上的Publish Over SSH命令远程运行Ansible剧本。

This command: 该命令:

curl -k -v -X POST https://jenkins.myhost.com/job/Ansible_Deploy/build?token=<appToken> --user <myUser>:<userToken> --data-urlencode json='{"parameter":[{"name":"thisIsAList","value":["one","two","three"]}]}'

should trigger a post-build action to remotely execute the following command over SSH: 应该触发构建后操作,以通过SSH远程执行以下命令:

ansible-playbook /home/<myUser>/test/practice.yml --extra-vars "thisIsAList=$thisIsAList"

thisIsAList is a string parameter under Job Notifications, and the job is parameterized. thisIsAList是“作业通知”下的字符串参数,并且已对作业进行了参数化。 I have successfully executed similar commands, but this one fails, assumingly because the value is a list. 我已经成功执行了类似的命令,但是由于值是一个列表,因此该命令失败了。 I have tried both "String Parameter" as well as "Multi-line String Parameter" to no avail. 我尝试了“字符串参数”和“多行字符串参数”都无济于事。

Here's the stack trace: 这是堆栈跟踪:

org.kohsuke.stapler.WrongTypeException: Got type array but no lister class found for type class java.lang.String
        at org.kohsuke.stapler.RequestImpl$TypePair.convertJSON(RequestImpl.java:723)
        at org.kohsuke.stapler.RequestImpl.bindJSON(RequestImpl.java:478)
        at org.kohsuke.stapler.RequestImpl.instantiate(RequestImpl.java:777)
Caused: java.lang.IllegalArgumentException: Failed to convert the value parameter of the constructor public hudson.model.StringParameterValue(java.lang.String,java.lang.String)
        at org.kohsuke.stapler.RequestImpl.instantiate(RequestImpl.java:779)
        at org.kohsuke.stapler.RequestImpl.access$200(RequestImpl.java:83)
        at org.kohsuke.stapler.RequestImpl$TypePair.convertJSON(RequestImpl.java:678)
Caused: java.lang.IllegalArgumentException: Failed to instantiate class hudson.model.StringParameterValue from {"name":"thisIsAList","value":["one","two","three"]}
        at org.kohsuke.stapler.RequestImpl$TypePair.convertJSON(RequestImpl.java:680)
        at org.kohsuke.stapler.RequestImpl.bindJSON(RequestImpl.java:478)
        at org.kohsuke.stapler.RequestImpl.bindJSON(RequestImpl.java:474)
        at hudson.model.StringParameterDefinition.createValue(StringParameterDefinition.java:88)
        at hudson.model.ParametersDefinitionProperty._doBuild(ParametersDefinitionProperty.java:165)

Note: This may be a duplicate of How to pass an array to a jenkins parameterized job via remote access api? 注意:这可能与如何通过远程访问api将数组传递给jenkins参数化作业的重复 but it hasn't gotten a valid response. 但没有得到有效的回应。

Since this level of nesting isn't detailed anywhere in the Jenkins or Ansible documentation I'll shed some light on the topic now that I've solved my issue. 由于在Jenkins或Ansible文档中的任何地方都没有详细介绍这种嵌套级别,因此,在解决问题之后,我将对该主题进行一些说明。

The command: 命令:

ansible-playbook /home/<myUsr>/test/practice.yml --extra-vars "thisIsAList=$thisIsAList"

Should have declared thisIsAList to be a dictionary object. 应该已经声明thisIsAList为一个字典对象。 Ie: 即:

ansible-playbook /home/<myUsr>/test/practice.yml --extra-vars "{thisIsAList=$thisIsAList}"

Furthermore, the data in the cURL command should've been formatted differently like so: 此外, cURL命令中的数据应采用不同的格式,如下所示:

json='{"parameter":[{"name":"thisIsAList","value":"[one,two,three]"}]}'

Note: the double-quotes are around the whole list, rather than the individual elements. 注意:双引号用于整个列表,而不是单个元素。

Finally, with further nested items (such as dict inside a list) you have to escape the double-quotes like so: 最后,对于其他嵌套项(例如列表中的dict),您必须像这样对双引号进行转义:

{"parameter":[{"name":"thisIsADictNestedInAList","value":"[{\"name\":\"numbers\",\"value\":[1s, 2s, 3s]}]"}]}

It seems, that at this level of nesting, it is no longer required to double-quote the lists; 看起来,在此嵌套级别上,不再需要对列表加双引号; probably because the quotes one level up already lead it to be interpreted correctly. 可能是因为向上一级的引号已经导致其被正确解释。

This is a bit of a guess, based on a similar problem I have seen with a choice parameter. 基于我在选择参数中看到的类似问题,这有点猜测。 Any documentation I have found seems to be wrong about how to handle these. 我发现的任何文档似乎都对如何处理这些错误。 It shouldn't be a list. 它不应该是列表。 Try passing as a string with newlines separating the items. 尝试以带换行符的字符串形式传递以分隔项目。

curl -k -v -X POST https://jenkins.myhost.com/job/Ansible_Deploy/build?token=<appToken> --user <myUser>:<userToken> --data-urlencode json='{"parameter":[{"name":"thisIsAList","value":"one\ntwo\nthree"}]}'

Let me know if this works. 让我知道这个是否奏效。 I'm interested to find out. 我有兴趣找出答案。


Edit: (based on comments) 编辑:(基于评论)

Would this work: 这项工作会:

curl -k -v -X POST https://jenkins.myhost.com/job/Ansible_Deploy/build?token=<appToken> --user <myUser>:<userToken> --data-urlencode json='{"parameter":[{"name":"thisIsAList","value":"'{\"thisIsAList\": [\"one\",\"two\",\"three\"]}'"}]}'

The nested quotes get a bit ugly. 嵌套的引号有点难看。 If you are using pipeline or can massage the data in a shell script first, it would probably be cleaner. 如果您正在使用管道,或者可以先在Shell脚本中处理数据,那么它可能会更干净。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM