如何与 Jenkins 并行执行阶段?
How to execute stages in parallel with Jenkins?
我创建了一个测试管道来了解并行功能如何与 Jenkins 一起工作。
这里可以看到代码:
node ('docker-slave') {
stage('Source') {
sh 'echo Downloading source code from GIT'
sh 'sleep 3'
}
stage('Compile'){
sh 'echo Compiling the code'
sh 'sleep 3'
}
stage('Test') {
parallel(
stage ('Unit test') {
sh 'echo Performing unit test'
sh 'sleep 10'
},
stage ('Security test') {
sh 'echo Performing security test'
sh 'sleep 8'
}
)
stage ('Integration test') {
sh 'echo Performing integration test'
sh 'sleep 8'
}
}
stage('Release') {
def release = input message: 'Release into production?',
parameters: [booleanParam(defaultValue: false,
description: 'Click on the box to release into prod', name: 'releasing')]
if(release == true){
sh 'echo release into Production'
}
}
}
不幸的是,当我执行它时,出现以下错误:
Started by user Jenkins
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on docker-slave-00002e11zecvz on docker in /home/jenkins/workspace/my-pipe
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Source)
[Pipeline] sh
+ echo Downloading source code from GIT
Downloading source code from GIT
[Pipeline] sh
+ sleep 3
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Compile)
[Pipeline] sh
+ echo Compiling the code
Compiling the code
[Pipeline] sh
+ sleep 3
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] stage
[Pipeline] { (Unit test)
[Pipeline] sh
+ echo Performing unit test
Performing unit test
[Pipeline] sh
+ sleep 10
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Security test)
[Pipeline] sh
+ echo Performing security test
Performing security test
[Pipeline] sh
+ sleep 8
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
java.lang.IllegalArgumentException: Expected named arguments but got [null, null]
at org.jenkinsci.plugins.workflow.cps.DSL.parseArgs(DSL.java:708)
at org.jenkinsci.plugins.workflow.cps.DSL.parseArgs(DSL.java:640)
at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:234)
at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:193)
at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:122)
at jdk.internal.reflect.GeneratedMethodAccessor280.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1213)
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 org.kohsuke.groovy.sandbox.impl.Checker.call(Checker.java:163)
at org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:23)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:157)
at org.kohsuke.groovy.sandbox.impl.Checker.call(Checker.java:161)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:165)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:135)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:135)
at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:17)
at WorkflowScript.run(WorkflowScript:14)
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.fixArg(FunctionCallBlock.java:83)
at jdk.internal.reflect.GeneratedMethodAccessor252.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
at com.cloudbees.groovy.cps.Outcome.resumeFrom(Outcome.java:73)
at com.cloudbees.groovy.cps.Continuable.call(Continuable.java:166)
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[=11=]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:400)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access0(CpsThreadGroup.java:96)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.call(CpsThreadGroup.java:312)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.call(CpsThreadGroup.java:276)
at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService.call(CpsVmExecutorService.java:67)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at hudson.remoting.SingleLaneExecutorService.run(SingleLaneExecutorService.java:136)
at jenkins.util.ContextResettingExecutorService.run(ContextResettingExecutorService.java:28)
at jenkins.security.ImpersonatingExecutorService.run(ImpersonatingExecutorService.java:59)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
Finished: FAILURE
看来我没有正确使用并行功能,因为单元测试和安全测试是一起执行的。任何帮助将不胜感激。
我已经根据你的编写了一个工作版本代码。
代码:
pipeline {
agent { label 'MISC' }
stages{
stage('Source'){
steps {
sh 'echo Downloading source code from GIT'
sh 'sleep 3'
}
}
stage('Compile'){
steps{
sh 'echo Compiling the code'
sh 'sleep 3'
}
}
stage('Test'){
parallel{
stage ('Unit test') {
steps{
sh 'echo Performing unit test'
sh 'sleep 10'
}
}
stage ('Security test') {
steps{
sh 'echo Performing security test'
sh 'sleep 8'
}
}
}
}
stage('Release') {
steps{
sh 'echo Performing release'
sh 'sleep 8'
}
}
}
}
日志相关部分:
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] parallel
[Pipeline] { (Branch: Unit test)
[Pipeline] { (Branch: Security test)
[Pipeline] stage
[Pipeline] { (Unit test)
[Pipeline] stage
[Pipeline] { (Security test)
[Pipeline] sh
[Pipeline] sh
+ echo Performing unit test
Performing unit test
[Pipeline] sh
+ echo Performing security test
Performing security test
[Pipeline] sh
+ sleep 10
+ sleep 8
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // parallel
[Pipeline] }
[Pipeline] // stage
BlueOcean
视图中的流程:
我创建了一个测试管道来了解并行功能如何与 Jenkins 一起工作。 这里可以看到代码:
node ('docker-slave') {
stage('Source') {
sh 'echo Downloading source code from GIT'
sh 'sleep 3'
}
stage('Compile'){
sh 'echo Compiling the code'
sh 'sleep 3'
}
stage('Test') {
parallel(
stage ('Unit test') {
sh 'echo Performing unit test'
sh 'sleep 10'
},
stage ('Security test') {
sh 'echo Performing security test'
sh 'sleep 8'
}
)
stage ('Integration test') {
sh 'echo Performing integration test'
sh 'sleep 8'
}
}
stage('Release') {
def release = input message: 'Release into production?',
parameters: [booleanParam(defaultValue: false,
description: 'Click on the box to release into prod', name: 'releasing')]
if(release == true){
sh 'echo release into Production'
}
}
}
不幸的是,当我执行它时,出现以下错误:
Started by user Jenkins Running in Durability level: MAX_SURVIVABILITY [Pipeline] Start of Pipeline [Pipeline] node Running on docker-slave-00002e11zecvz on docker in /home/jenkins/workspace/my-pipe [Pipeline] { [Pipeline] stage [Pipeline] { (Source) [Pipeline] sh + echo Downloading source code from GIT Downloading source code from GIT [Pipeline] sh + sleep 3 [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (Compile) [Pipeline] sh + echo Compiling the code Compiling the code [Pipeline] sh + sleep 3 [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (Test) [Pipeline] stage [Pipeline] { (Unit test) [Pipeline] sh + echo Performing unit test Performing unit test [Pipeline] sh + sleep 10 [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (Security test) [Pipeline] sh + echo Performing security test Performing security test [Pipeline] sh + sleep 8 [Pipeline] } [Pipeline] // stage [Pipeline] } [Pipeline] // stage [Pipeline] } [Pipeline] // node [Pipeline] End of Pipeline java.lang.IllegalArgumentException: Expected named arguments but got [null, null] at org.jenkinsci.plugins.workflow.cps.DSL.parseArgs(DSL.java:708) at org.jenkinsci.plugins.workflow.cps.DSL.parseArgs(DSL.java:640) at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:234) at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:193) at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:122) at jdk.internal.reflect.GeneratedMethodAccessor280.invoke(Unknown Source) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:566) at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93) at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325) at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1213) 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 org.kohsuke.groovy.sandbox.impl.Checker.call(Checker.java:163) at org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:23) at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:157) at org.kohsuke.groovy.sandbox.impl.Checker.call(Checker.java:161) at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:165) at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:135) at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:135) at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:17) at WorkflowScript.run(WorkflowScript:14) 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.fixArg(FunctionCallBlock.java:83) at jdk.internal.reflect.GeneratedMethodAccessor252.invoke(Unknown Source) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:566) at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72) at com.cloudbees.groovy.cps.Outcome.resumeFrom(Outcome.java:73) at com.cloudbees.groovy.cps.Continuable.call(Continuable.java:166) 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[=11=]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:400) at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access0(CpsThreadGroup.java:96) at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.call(CpsThreadGroup.java:312) at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.call(CpsThreadGroup.java:276) at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService.call(CpsVmExecutorService.java:67) at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) at hudson.remoting.SingleLaneExecutorService.run(SingleLaneExecutorService.java:136) at jenkins.util.ContextResettingExecutorService.run(ContextResettingExecutorService.java:28) at jenkins.security.ImpersonatingExecutorService.run(ImpersonatingExecutorService.java:59) at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) at java.base/java.lang.Thread.run(Thread.java:834) Finished: FAILURE
看来我没有正确使用并行功能,因为单元测试和安全测试是一起执行的。任何帮助将不胜感激。
我已经根据你的编写了一个工作版本代码。
代码:
pipeline {
agent { label 'MISC' }
stages{
stage('Source'){
steps {
sh 'echo Downloading source code from GIT'
sh 'sleep 3'
}
}
stage('Compile'){
steps{
sh 'echo Compiling the code'
sh 'sleep 3'
}
}
stage('Test'){
parallel{
stage ('Unit test') {
steps{
sh 'echo Performing unit test'
sh 'sleep 10'
}
}
stage ('Security test') {
steps{
sh 'echo Performing security test'
sh 'sleep 8'
}
}
}
}
stage('Release') {
steps{
sh 'echo Performing release'
sh 'sleep 8'
}
}
}
}
日志相关部分:
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] parallel
[Pipeline] { (Branch: Unit test)
[Pipeline] { (Branch: Security test)
[Pipeline] stage
[Pipeline] { (Unit test)
[Pipeline] stage
[Pipeline] { (Security test)
[Pipeline] sh
[Pipeline] sh
+ echo Performing unit test
Performing unit test
[Pipeline] sh
+ echo Performing security test
Performing security test
[Pipeline] sh
+ sleep 10
+ sleep 8
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // parallel
[Pipeline] }
[Pipeline] // stage
BlueOcean
视图中的流程: