简体   繁体   English

Jenkins 写回 883812976388 文件时抛出错误

[英]Jenkins throwing an error when writing back an xml File

I have a stage on my Jenkins pipeline where I search for a.xml File, open it, change a node and write the changes back to the.xml File.我的 Jenkins 管道上有一个阶段,我在其中搜索 .xml 文件,打开它,更改一个节点并将更改写回 .xml 文件。 It works fine when I do it on my local machine but it does not work when it is done on Jenkins.当我在我的本地机器上执行时它工作正常但当它在 Jenkins 上完成时它不起作用。

This is the code I'm using:这是我正在使用的代码:

def inFile = new File('myFile.xml')
def xml = new XmlSlurper(false,false).parse( inFile )


if(xml.repositories.repository.url.toString().contains("string to match") {

    xml.repositories.repository.replaceNode { 
        repository{
            id("id")
            name("name")
            url("url")
        }
    }

    inFile.withWriter { outWriter ->
        XmlUtil.serialize(xml, outWriter )
    }
}

And Jenkins is showing this error: Jenkins 显示此错误:

java.lang.NoSuchMethodError: No such DSL method 'repository' found among steps[]
    at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:176)
    at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:122)
    at groovy.lang.MetaClassImpl.invokeMethodOnGroovyObject(MetaClassImpl.java:1278)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1172)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022)
    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:42)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.methodCall(DefaultInvoker.java:20)
    at ___cps.transform___(Native Method)
    at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:57)
    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:109)
    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:82)
    at sun.reflect.GeneratedMethodAccessor204.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
    at com.cloudbees.groovy.cps.impl.ClosureBlock.eval(ClosureBlock.java:46)
    at com.cloudbees.groovy.cps.Next.step(Next.java:83)
    at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:174)
    at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:163)
    at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:122)
    at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:261)
    at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$101(SandboxContinuable.java:34)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.lambda$run0$0(SandboxContinuable.java:59)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:108)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:58)
    at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:174)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:332)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$200(CpsThreadGroup.java:83)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:244)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:232)
    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:64)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:131)
    at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

My xml file looks like this:我的 xml 文件如下所示:

...

<repositories>
    <repository>
        <id>id old</id>
        <url>url old</url>
    </repository>
</repositories>

...

I have check the imports and have them all correct.我已经检查了进口并且它们都是正确的。 I moved the write part to a function and added @NonCPS annotation and nothing changed.我将写入部分移至 function 并添加了 @NonCPS 注释,但没有任何变化。 I think I'm having a problem with the BufferedWriter but I don't know how to continue on this.我想我在使用 BufferedWriter 时遇到了问题,但我不知道如何继续。 Any help is welcome, thank you so much!欢迎任何帮助,非常感谢!

I think your problem may not be a BufferedWriter, but more the reading part.我认为您的问题可能不是 BufferedWriter,而是阅读部分。 The error message No such DSL method 'repository' found among steps points in that direction.错误消息No such DSL method 'repository' found among steps指向那个方向。

Line four of your code tries to access the XML object repository from your parsed myFile.xml :代码的第四行尝试从已解析的myFile.xml访问 XML object repository

if(xml.repositories.repository.url.toString().contains("string to match") {

Dereferencing xml.respositories.repository fails, maybe because the file can not be read or parsed.取消引用xml.respositories.repository失败,可能是因为无法读取或解析该文件。 Can you check in your Groovy code, whether the file exists and is readable?您可以检查您的 Groovy 代码,该文件是否存在并且可读吗?

I'm following up on my comment with an actual solution advice:我正在用实际的解决方案建议跟进我的评论:

No such DSL method 'repository' found among steps points to the fact that Jenkins is trying to interpret your repository as its own method. No such DSL method 'repository' found among steps表明 Jenkins 试图将您的repository解释为它自己的方法。 That could be happening actually on line 8 of your code as everywhere else it's explicitly defined what you're referencing.这可能实际上发生在您代码的第8行,因为它在其他任何地方都明确定义了您所引用的内容。

So - in order to fix this ambiguity in your code, try this:所以 - 为了解决代码中的这种歧义,试试这个:

  • Define your repository node variable and then use it within the replaceNode so it's crystal clear to Jenkins what it should do.定义您的repository节点变量,然后在replaceNode中使用它,这样 Jenkins 它应该做什么就非常清楚了。

Something likes this (you'll probably need it import groovy.util.Node probably too (but I don't know the rest of you pipeline code) + adjust your node attributes according to your needs):像这样的东西(你可能也需要它 import groovy.util.Node (但我不知道你管道代码的 rest )+根据你的需要调整你的节点属性):

import groovy.util.*
def repository = new Node(null, 'repository', [id:'3'])

xml.repositories.repository.replaceNode { 
    repository
}

To provide some references and further inspiration (as myself I'm not a groovy master at all):提供一些参考和进一步的启发(因为我自己根本不是groovy大师):

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

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