在 Windows Jenkins 代理上的 Groovy 脚本中查找文件中的行数
Finding number of lines in file in Groovy Script on Windows Jenkins agent
我正在使用 Synopsys Coverity 为 运行 静态代码分析编写 groovy 脚本,并希望将所有发现的问题存储在 .txt 文件中。然后需要找到该 .txt 文件中的行数,如果它大于 1,则需要向相关人员发送电子邮件。我的这个 groovy 脚本代码如下:-
def call(Map coverityParams){
echo "${coverityParams.defectsFileName}"
bat """
cov-analyze --dir ${coverityParams.tempDir} --all --security ${coverityParams.securitySwitch}
cov-commit-defects --dir ${coverityParams.tempDir} --stream ${coverityParams.stream} --host 172.16.15.34 --auth-key-file ${coverityParams.authKeyFile}
cov-manage-im --mode defects --show --host 172.16.15.34 --stream ${coverityParams.stream} --user=${coverityParams.userName} --password=${coverityParams.password} --status New > ${coverityParams.defectsFileName}
"""
int DEFECTS_COUNT = bat(
script: find /v \"\" /c < \"${coverityParams.defectsFileName}\",
returnStdout: true
).trim().toInteger()
DEFECTS_COUNT = DEFECTS_COUNT - 1
echo "Defects Count is ${DEFECTS_COUNT}"
if(DEFECTS_COUNT > 0){
echo "Defects Count is ${DEFECTS_COUNT}"
mail (body: 'You have ' + DEFECTS_COUNT + ' new coverity defects introduced. Please go to ' + "${coverityParams.viewName}" + ' view on http://172.16.15.34:8080/ ', subject: DEFECTS_COUNT + ' new coverity issues detected.', to: "${coverityParams.mailTo}")
}
}
运行时 Jenkins 抛出以下错误
java.lang.NumberFormatException: For input string: "C:\JenkinsWS\workspace\gwp-master>find /v "" /c 0<"GWDefects.txt"
1"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.valueOf(Integer.java:766)
at org.codehaus.groovy.runtime.StringGroovyMethods.toInteger(StringGroovyMethods.java:3312)
at org.codehaus.groovy.runtime.dgm60.doMethodInvoke(Unknown Source)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1213)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022)
at org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:47)
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 coverityCommitDefectsAndMailWin.call(coverityCommitDefectsAndMailWin.groovy:8)
at WorkflowScript.run(WorkflowScript:197)
at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.delegateAndExecute(ModelInterpreter.groovy:137)
at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.executeSingleStage(ModelInterpreter.groovy:666)
at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.catchRequiredContextForNode(ModelInterpreter.groovy:395)
at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.catchRequiredContextForNode(ModelInterpreter.groovy:393)
at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.executeSingleStage(ModelInterpreter.groovy:665)
at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.evaluateStage(ModelInterpreter.groovy:288)
at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.toolsBlock(ModelInterpreter.groovy:544)
at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.toolsBlock(ModelInterpreter.groovy:543)
at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.evaluateStage(ModelInterpreter.groovy:276)
at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.withEnvBlock(ModelInterpreter.groovy:438)
at ___cps.transform___(Native Method)
at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:86)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:113)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixName(FunctionCallBlock.java:78)
at sun.reflect.GeneratedMethodAccessor1021.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.ConstantBlock.eval(ConstantBlock.java:21)
at com.cloudbees.groovy.cps.Next.step(Next.java:83)
at com.cloudbees.groovy.cps.Continuable.call(Continuable.java:174)
at com.cloudbees.groovy.cps.Continuable.call(Continuable.java:163)
at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:129)
at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:268)
at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access[=13=]1(SandboxContinuable.java:18)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:51)
at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:185)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:402)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access0(CpsThreadGroup.java:96)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.call(CpsThreadGroup.java:314)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.call(CpsThreadGroup.java:278)
at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService.call(CpsVmExecutorService.java:67)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at hudson.remoting.SingleLaneExecutorService.run(SingleLaneExecutorService.java:139)
at jenkins.util.ContextResettingExecutorService.run(ContextResettingExecutorService.java:28)
at jenkins.security.ImpersonatingExecutorService.run(ImpersonatingExecutorService.java:68)
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:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:750)
Finished: FAILURE
我知道这与将字符串转换为数字有关。我认为问题是我的脚本返回 1"
而不是 1
,这就是 toInteger()
无法将其转换为数字的原因。但是我找不到解决方法。
因此您可以将以下代码段用于您的案例
script {
// some block
def DEFECTS_COUNT_STR = bat(
script: '''find /v "" /c < "somefile.txt"''',
returnStdout: true
).trim().split("\n")
int DEFECTS_COUNT = DEFECTS_COUNT_STR[1].toInteger()
echo "$DEFECTS_COUNT"
}
问题出在哪里了,\m.你的问题是你得到的字符串如下:
"C:\JenkinsWS\workspace\gwp-master>find /v "" /c 0<"GWDefects.txt"
1"
这是 2 行。 1 在哪里执行什么蝙蝠,第二个是你的结果。
你需要结果。所以最好的方法是拆分它然后丢弃第一个输出并使用第二个输出然后转换为整数。
编辑:
我应该更新你的代码。这是您的代码的样子
def call(Map coverityParams){
echo "${coverityParams.defectsFileName}"
bat """
cov-analyze --dir ${coverityParams.tempDir} --all --security ${coverityParams.securitySwitch}
cov-commit-defects --dir ${coverityParams.tempDir} --stream ${coverityParams.stream} --host 172.16.15.34 --auth-key-file ${coverityParams.authKeyFile}
cov-manage-im --mode defects --show --host 172.16.15.34 --stream ${coverityParams.stream} --user=${coverityParams.userName} --password=${coverityParams.password} --status New > ${coverityParams.defectsFileName}
"""
def DEFECTS_COUNT_STR = bat(
script: find /v \"\" /c < \"${coverityParams.defectsFileName}\",
returnStdout: true
).trim().split("\n")
int DEFECTS_COUNT = DEFECTS_COUNT_STR[1].toInteger()
DEFECTS_COUNT = DEFECTS_COUNT - 1
echo "Defects Count is ${DEFECTS_COUNT}"
if(DEFECTS_COUNT > 0){
echo "Defects Count is ${DEFECTS_COUNT}"
mail (body: 'You have ' + DEFECTS_COUNT + ' new coverity defects introduced. Please go to ' + "${coverityParams.viewName}" + ' view on http://172.16.15.34:8080/ ', subject: DEFECTS_COUNT + ' new coverity issues detected.', to: "${coverityParams.mailTo}")
}
}
我正在使用 Synopsys Coverity 为 运行 静态代码分析编写 groovy 脚本,并希望将所有发现的问题存储在 .txt 文件中。然后需要找到该 .txt 文件中的行数,如果它大于 1,则需要向相关人员发送电子邮件。我的这个 groovy 脚本代码如下:-
def call(Map coverityParams){
echo "${coverityParams.defectsFileName}"
bat """
cov-analyze --dir ${coverityParams.tempDir} --all --security ${coverityParams.securitySwitch}
cov-commit-defects --dir ${coverityParams.tempDir} --stream ${coverityParams.stream} --host 172.16.15.34 --auth-key-file ${coverityParams.authKeyFile}
cov-manage-im --mode defects --show --host 172.16.15.34 --stream ${coverityParams.stream} --user=${coverityParams.userName} --password=${coverityParams.password} --status New > ${coverityParams.defectsFileName}
"""
int DEFECTS_COUNT = bat(
script: find /v \"\" /c < \"${coverityParams.defectsFileName}\",
returnStdout: true
).trim().toInteger()
DEFECTS_COUNT = DEFECTS_COUNT - 1
echo "Defects Count is ${DEFECTS_COUNT}"
if(DEFECTS_COUNT > 0){
echo "Defects Count is ${DEFECTS_COUNT}"
mail (body: 'You have ' + DEFECTS_COUNT + ' new coverity defects introduced. Please go to ' + "${coverityParams.viewName}" + ' view on http://172.16.15.34:8080/ ', subject: DEFECTS_COUNT + ' new coverity issues detected.', to: "${coverityParams.mailTo}")
}
}
运行时 Jenkins 抛出以下错误
java.lang.NumberFormatException: For input string: "C:\JenkinsWS\workspace\gwp-master>find /v "" /c 0<"GWDefects.txt"
1"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.valueOf(Integer.java:766)
at org.codehaus.groovy.runtime.StringGroovyMethods.toInteger(StringGroovyMethods.java:3312)
at org.codehaus.groovy.runtime.dgm60.doMethodInvoke(Unknown Source)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1213)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022)
at org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:47)
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 coverityCommitDefectsAndMailWin.call(coverityCommitDefectsAndMailWin.groovy:8)
at WorkflowScript.run(WorkflowScript:197)
at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.delegateAndExecute(ModelInterpreter.groovy:137)
at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.executeSingleStage(ModelInterpreter.groovy:666)
at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.catchRequiredContextForNode(ModelInterpreter.groovy:395)
at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.catchRequiredContextForNode(ModelInterpreter.groovy:393)
at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.executeSingleStage(ModelInterpreter.groovy:665)
at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.evaluateStage(ModelInterpreter.groovy:288)
at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.toolsBlock(ModelInterpreter.groovy:544)
at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.toolsBlock(ModelInterpreter.groovy:543)
at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.evaluateStage(ModelInterpreter.groovy:276)
at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.withEnvBlock(ModelInterpreter.groovy:438)
at ___cps.transform___(Native Method)
at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:86)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:113)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixName(FunctionCallBlock.java:78)
at sun.reflect.GeneratedMethodAccessor1021.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.ConstantBlock.eval(ConstantBlock.java:21)
at com.cloudbees.groovy.cps.Next.step(Next.java:83)
at com.cloudbees.groovy.cps.Continuable.call(Continuable.java:174)
at com.cloudbees.groovy.cps.Continuable.call(Continuable.java:163)
at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:129)
at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:268)
at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access[=13=]1(SandboxContinuable.java:18)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:51)
at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:185)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:402)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access0(CpsThreadGroup.java:96)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.call(CpsThreadGroup.java:314)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.call(CpsThreadGroup.java:278)
at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService.call(CpsVmExecutorService.java:67)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at hudson.remoting.SingleLaneExecutorService.run(SingleLaneExecutorService.java:139)
at jenkins.util.ContextResettingExecutorService.run(ContextResettingExecutorService.java:28)
at jenkins.security.ImpersonatingExecutorService.run(ImpersonatingExecutorService.java:68)
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:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:750)
Finished: FAILURE
我知道这与将字符串转换为数字有关。我认为问题是我的脚本返回 1"
而不是 1
,这就是 toInteger()
无法将其转换为数字的原因。但是我找不到解决方法。
因此您可以将以下代码段用于您的案例
script {
// some block
def DEFECTS_COUNT_STR = bat(
script: '''find /v "" /c < "somefile.txt"''',
returnStdout: true
).trim().split("\n")
int DEFECTS_COUNT = DEFECTS_COUNT_STR[1].toInteger()
echo "$DEFECTS_COUNT"
}
问题出在哪里了,\m.你的问题是你得到的字符串如下:
"C:\JenkinsWS\workspace\gwp-master>find /v "" /c 0<"GWDefects.txt"
1"
这是 2 行。 1 在哪里执行什么蝙蝠,第二个是你的结果。 你需要结果。所以最好的方法是拆分它然后丢弃第一个输出并使用第二个输出然后转换为整数。
编辑: 我应该更新你的代码。这是您的代码的样子
def call(Map coverityParams){
echo "${coverityParams.defectsFileName}"
bat """
cov-analyze --dir ${coverityParams.tempDir} --all --security ${coverityParams.securitySwitch}
cov-commit-defects --dir ${coverityParams.tempDir} --stream ${coverityParams.stream} --host 172.16.15.34 --auth-key-file ${coverityParams.authKeyFile}
cov-manage-im --mode defects --show --host 172.16.15.34 --stream ${coverityParams.stream} --user=${coverityParams.userName} --password=${coverityParams.password} --status New > ${coverityParams.defectsFileName}
"""
def DEFECTS_COUNT_STR = bat(
script: find /v \"\" /c < \"${coverityParams.defectsFileName}\",
returnStdout: true
).trim().split("\n")
int DEFECTS_COUNT = DEFECTS_COUNT_STR[1].toInteger()
DEFECTS_COUNT = DEFECTS_COUNT - 1
echo "Defects Count is ${DEFECTS_COUNT}"
if(DEFECTS_COUNT > 0){
echo "Defects Count is ${DEFECTS_COUNT}"
mail (body: 'You have ' + DEFECTS_COUNT + ' new coverity defects introduced. Please go to ' + "${coverityParams.viewName}" + ' view on http://172.16.15.34:8080/ ', subject: DEFECTS_COUNT + ' new coverity issues detected.', to: "${coverityParams.mailTo}")
}
}