由于公司的一些变化,我们不得不切换到Jenkins作为CI工具。虽然这毕竟不是一个坏主意,但由于缺乏对非Java应用程序的支持,我们遇到了很多麻烦,特别是对于Pipeline(旧的Workflow)插件,当然还有我们缺乏Jenkins的知识(此时此刻没有。)
node('master')
{
try
{
stage('Checkout, restore, build')
{
//Checkout the code from the repository
git branch: '<branch_name>', credentialsId: '<credentials_ID>', url: '<repo_URL>'
//git clean
bat returnStatus: true, script: 'git clean -fdx'
//Perform dotnet restore and nuget restore
bat returnStatus: true, script: '''for /f "tokens=*" %%a in (\'dir project.json /b /s\') do dotnet restore "%%a"
"C:\\Users\\Administrator\\.jenkins\\workspace\\nuget.exe" restore "C:\\Users\\Administrator\\.jenkins\\workspace\\CI\\<solution_name>.sln"'''
//Build the solution
bat returnStatus: true, script: '"C:\\Program Files (x86)\\MSBuild\\14.0\\Bin\\msbuild.exe" /p:DebugType=full /p:platform=x64 /p:configuration=release /p:VisualStudioVersion=14.0 '
}
} catch(err)
{
currentBuild.result = 'FAILURE'
}
jobDsl("${env.JOB_NAME}") {
steps {
bat returnStatus: true, script: '"C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\Common7\\IDE\\CommonExtensions\\Microsoft\\TestWindow\\vstest.console.exe" "C:\\Users\\Administrator\\.jenkins\\workspace\\CI\\<path_to_tests_dll>" /TestCaseFilter:"TestCategory=UnitTest|TestCategory=ContinuousTest" /EnableCodeCoverage /Platform:x64 /logger:trx'
}
publishers {
archiveXUnit {
msTest {
pattern('"C:\\Users\\Administrator\\.jenkins\\workspace\\CI\\TestResults"')
}
}
}
}
}
但是我收到jobDsl
的错误:
java.lang.IllegalArgumentException:期望的命名参数但得到了 [CI,org.jenkinsci.plugins.workflow.cps.CpsClosure2@1a706730] at org.jenkinsci.plugins.workflow.cps.DSL.parseArgs(DSL.java:442)at at org.jenkinsci.plugins.workflow.cps.DSL.invokeDescribable(DSL.java:251) 在org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:129) 在 org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:108) at groovy.lang.GroovyObject $ invokeMethod.call(Unknown Source)at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48) 在 org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113) 在org.kohsuke.groovy.sandbox.impl.Checker $ 1.call(Checker.java:151) 在 org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:21) 在 org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:115) 在 org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:103) 在org.kohsuke.groovy.sandbox.impl.Checker $ 1.call(Checker.java:149) 在 org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:146) 在 com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:16) 在WorkflowScript.run(WorkflowScript:1)at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall上的 cps.transform (原生方法)(ContinuationGroup.java:57) 在 com.cloudbees.groovy.cps.impl.FunctionCallBlock $ ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:109) 在 com.cloudbees.groovy.cps.impl.FunctionCallBlock $ ContinuationImpl.fixArg(FunctionCallBlock.java:82) 在sun.reflect.GeneratedMethodAccessor295.invoke(未知来源)at sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)at java.lang.reflect.Method.invoke(未知来源)at com.cloudbees.groovy.cps.impl.ContinuationPtr $ ContinuationImpl.receive(ContinuationPtr.java:72) 在 com.cloudbees.groovy.cps.impl.ClosureBlock.eval(ClosureBlock.java:46) 在com.cloudbees.groovy.cps.Next.step(Next.java:74)at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:154)at at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access $ 001(SandboxContinuable.java:18) 在 org.jenkinsci.plugins.workflow.cps.SandboxContinuable $ 1.call(SandboxContinuable.java:33) 在 org.jenkinsci.plugins.workflow.cps.SandboxContinuable $ 1.call(SandboxContinuable.java:30) 在 org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:108) 在 org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:30) 在 org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:165) 在 org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:328) 在 org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access $ 100(CpsThreadGroup.java:80) 在 org.jenkinsci.plugins.workflow.cps.CpsThreadGroup $ 2.call(CpsThreadGroup.java:240) 在 org.jenkinsci.plugins.workflow.cps.CpsThreadGroup $ 2.call(CpsThreadGroup.java:228) 在 org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService $ 2.call(CpsVmExecutorService.java:64) 在java.util.concurrent.FutureTask.run(未知来源)at hudson.remoting.SingleLaneExecutorService $ 1.run(SingleLaneExecutorService.java:112) 在 jenkins.util.ContextResettingExecutorService $ 1.run(ContextResettingExecutorService.java:28) 在java.util.concurrent.Executors $ RunnableAdapter.call(未知 来自)java.util.concurrent.FutureTask.run(未知来源)at java.util.concurrent.ThreadPoolExecutor.runWorker(未知来源)at java.util.concurrent.ThreadPoolExecutor $ Worker.run(未知来源)at java.lang.Thread.run(未知来源)
这真的让我觉得这不是一个好方法,所以有人可以在正确的道路上引导我并引导我吗?
答案 0 :(得分:1)
您不能混用Pipeline DSL和Job DSL。那些是完全不同的东西。
自{1.1}版以来,XUnit Plugin内置了对Pipeline DSL的支持,详见JENKINS-27240。因此,您无需尝试在管道脚本中嵌入Job DSL脚本。
答案 1 :(得分:0)
我使用“ MSTestPublisher ”类做到了这一点,
最后一条管道是:
node {
stage 'Checkout'
checkout scm
stage 'Build'
bat "\"C:/Program Files/dotnet/dotnet.exe\" restore \"${workspace}/MyProg.sln\""
bat "\"C:/Program Files/dotnet/dotnet.exe\" build \"${workspace}/MyProg.sln\""
stage 'UnitTests'
bat returnStatus: true, script: "\"C:/Program Files/dotnet/dotnet.exe\" test \"${workspace}/MyProg.sln\" --logger \"trx;LogFileName=unit_tests.xml\" --no-build"
step([$class: 'MSTestPublisher', testResultsFile:"**/unit_tests.xml", failOnError: true, keepLongStdio: true])
}
我已将我制作的一些示例上传到GitHub,供所有人使用和贡献,请随时查看:
https://github.com/avrum/JenkinsFileFor.NETCore
这些pipline jenkinsfile会将此pipline模板添加到您的构建中: