我正在使用Jenkins 2来编译Java项目,我想从pom.xml中读取该版本,我遵循这个例子:
https://github.com/jenkinsci/pipeline-plugin/blob/master/TUTORIAL.md
这个例子表明:
似乎访问文件系统存在一些安全问题,但我无法弄清楚它给出了什么(或为什么)这个问题:
我只是做了一个与示例有点不同的事情:
def version() {
String path = pwd();
def matcher = readFile("${path}/pom.xml") =~ '<version>(.+)</version>'
return matcher ? matcher[0][1] : null
}
Run Code Online (Sandbox Code Playgroud)
我在运行'version'方法时遇到的错误:
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use method groovy.lang.GroovyObject invokeMethod java.lang.String java.lang.Object (org.codehaus.groovy.runtime.GStringImpl call org.codehaus.groovy.runtime.GStringImpl)
at org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.StaticWhitelist.rejectMethod(StaticWhitelist.java:165)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:117)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:103)
at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:149)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:146)
at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:15)
at WorkflowScript.run(WorkflowScript:71)
at ___cps.transform___(Native Method)
at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:55)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:106)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:79)
at sun.reflect.GeneratedMethodAccessor408.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:100)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:79)
at sun.reflect.GeneratedMethodAccessor408.invoke(Unknown Source) …Run Code Online (Sandbox Code Playgroud) def data = readJSON text: '{"rel" : {"configVersion": "1.0","manifest" :"'+"${manifestURL}"+'"}}'
writeJSON(file: 'C:\\Users\\Public\\json\\config.json', json: data)
Run Code Online (Sandbox Code Playgroud)
我在我的jenkins管道中使用JSON函数并获取NoSuchMethodFoundError.我正在使用Jenkins 2.85.
知道如何解决这个问题吗?
java.lang.NoSuchMethodError: No such DSL method 'readJSON' found among steps
[archive, bat, build, catchError, checkout, deleteDir, dir,
dockerFingerprintFrom, dockerFingerprintRun, echo, emailext,
emailextrecipients, envVarsForTool, error, fileExists, getContext, git,
input, isUnix, library, libraryResource, load, mail, milestone, node,
parallel, powershell, properties, pwd, readFile, readTrusted, resolveScm,
retry, script, sh, sleep, stage, stash, step, svn, timeout, timestamps, tm,
tool, unarchive, unstash, validateDeclarativePipeline, waitUntil,
withContext, withCredentials, withDockerContainer, withDockerRegistry, …Run Code Online (Sandbox Code Playgroud) 我在 Jenkins 管道中有以下代码:
stage ("distribution"){
steps{
script{
def rules = [
service_name: "core",
site_name: "*",
city_name: "*",
country_codes: ["*"]
]
amd_distribution_distribute_bundle distribution_rules: rules
}
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,它是一个地图参数。如何使用 Groovy 代码将其转换为 JSON 文件?最后它应该是这样的:
{
"distribution_rules": [
{
"service_name": "core*",
"site_name": "*",
"city_name": "*",
"country_codes": ["*"]
}
]
}
Run Code Online (Sandbox Code Playgroud)
我尝试了以下命令,但没有帮助:
import groovy.json.JsonBuilder
import groovy.json.JsonOutput
def call(Map parameters)
{
def DISTRIBUTION_RULES = parameters.distribution_rules
def json = new groovy.json.JsonBuilder()
json rootKey: "${DISTRIBUTION_RULES}"
writeFile file: 'rootKey', text: JsonOutput.toJson(json)
}
Run Code Online (Sandbox Code Playgroud) 我想构造一个JSON对象并将内容写入文件。
原来我的灵感来自于这和尝试:
def data = [
a:"test: ${myVar}"
]
writeJSON(file: 'message1.json', json: data)
Run Code Online (Sandbox Code Playgroud)
但这失败了:
无法为WriteJSONStep实例化{file = message1.json,json = {a = test}}(文件:字符串,json:JSON {},漂亮?:int):java.lang.UnsupportedOperationException:必须在实现中指定$ class net.sf.json.JSON的接口
所以接下来我尝试了:
def data = readJSON text: '{}'
data.a = "test: ${myVar}"
writeJSON(file: 'message1.json', json: data, pretty: 4)
Run Code Online (Sandbox Code Playgroud)
现在构建通过了,但是文件的内容如下所示:
{
"a": {
"bytes": [
114,
101,
108,
101,
97,
115,
101
50
],
"strings": [
"test: ",
""
],
"valueCount": 1,
"values": ["v1.0.2"]
}
}
Run Code Online (Sandbox Code Playgroud)
而我的意图是 {"a": "test: v1.0.2"}
我的最终目标是我想动态构造一个JSON对象,使用一些动态数据设置一些属性,然后编写JSON文件。
是否有一些语法可用于将值分配为字符串,而不是某些字节。
Consider code:
Map prJsonData = readJSON text: '{}'
prJsonData.head = "release/${NEW_TAG}" as String
prJsonData.title = "Release ${NEW_TAG}"
writeJSON(file: 'create-pr.json', json: prJsonData, pretty: 4)
Run Code Online (Sandbox Code Playgroud)
and output
{
"head": "release/v1.0.2",
"title": {
"bytes": [
82,
101,
97
],
"strings": [
"Release ",
""
],
"valueCount": 1,
"values": ["v1.0.2"]
}
}
Run Code Online (Sandbox Code Playgroud)
Why is it that specifying as String changes the output such that interpolation works but without this the output appears to be some sort of complex type.