我撰寫了一個 groovy 腳本,用于使用 Synopsys Coverity 運行靜態代碼分析,并希望將所有發現的問題存盤在 .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}")
}
}
當這個運行詹金斯拋出以下錯誤
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.dgm$1160.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$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: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$001(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.access$400(CpsThreadGroup.java:96)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:314)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:278)
at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:67)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:139)
at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
at jenkins.security.ImpersonatingExecutorService$1.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()無法將其轉換為數字的原因。但我不知道如何解決這個問題。
uj5u.com熱心網友回復:
因此,您可以將以下代碼段用于您的案例
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}")
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/443536.html
上一篇:如何從詹金斯上的jfrogwebhook觸發器獲取有效負載資訊
下一篇:構建ASPNetCore6.0專案:Microsoft包與.NETFramework,Version=v6.0不兼容,但支持.NETCoreApp,Version=v6.0
