我有一个像下面的Jenkinsfile-A,在这个文件中,我需要在执行所有阶段后调用另一个Jenkinsfile-B .
Jenkinsfile-A
#!groovy
pipeline {
agent { label "" }
triggers { pollSCM('*/5 * * * *') }
stages {
stage('Build Artifact') {
steps {
}
}
stage('Publish Artifact') {
steps {
}
}
}//stages
post {
always {
deleteDir() /* clean up our workspace */
}
}
}//pipeline
def jenkinsFile
stage('Loading Jenkinsfile'){
jenkinsFile = fileLoader.fromGit('Jenkinsfile', 'git@bitbucket.org:org-us-dev/cicd-pipelines.git', 'master', null, '')
}
jenkinsFile.start()
Jenkinsfile-B
#!groovy
void Download_Repositories(){
}
void SonarQube_Analysis(){
}
void Deploy_And_Test(){
}
void Prepare_Sonar_Scanner(){
}
void Publish_Artifact(){
}
void Host_Verification(){
}
pipeline {
agent { label '' }
// triggers { pollSCM('*/5 * * * *') }
stages {
stage('Download Repositories') {
steps {
Download_Repositories()
}
}
stage('Host Verification') {
steps {
Host_Verification()
}
}
stage('Env: DEV - Deploy and Test') {
steps {
Deploy_And_Test()
}
}
stage('Prepare Sonar Scanner') {
steps {
Prepare_Sonar_Scanner()
}
}
stage('SonarQube Analysis') {
steps {
SonarQube_Analysis()
}
}
stage('Publish Artifact') {
steps {
Publish_Artifact()
}
}
}//stages
post {
always {
deleteDir() /* clean up our workspace */
}
}//post
}//pipeline
我通过读取此块Invoke a remote jenkins file from another Jenkinsfile在Jenkinsfile-A中完成管道后添加了def,但这导致了错误 .
我的错误日志 -
groovy.lang.MissingPropertyException: No such property: fileLoader for class: groovy.lang.Binding
at groovy.lang.Binding.getVariable(Binding.java:63)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:264)
at org.kohsuke.groovy.sandbox.impl.Checker$6.call(Checker.java:288)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:292)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:268)
at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:29)
at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
at WorkflowScript.run(WorkflowScript:41)
at ___cps.transform___(Native Method)
at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.get(PropertyishBlock.java:74)
at com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30)
at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.fixName(PropertyishBlock.java:66)
at sun.reflect.GeneratedMethodAccessor132.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: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 jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:59)
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:748)
Finished: FAILURE
1 回答
没有办法直接从另一个jenkinsfile调用jenkinsfile . 但你有一些选择:
使用 build jenkins步骤触发构建运行jenkinsfile-B . 有关详细信息,请参阅https://jenkins.io/doc/pipeline/steps/pipeline-build-step/
使用 load 与 scm jenkins组合步骤使用您的jenkinsfile-B签出存储库并将其加载为groovy代码 . 有关详细信息,请参阅https://jenkins.io/doc/pipeline/steps/workflow-scm-step/和https://jenkins.io/doc/pipeline/steps/workflow-cps/#code-load-code-evaluate-a-groovy-source-file-into-the-pipeline-script . 但在这种情况下,你应该重写你的jenkinsfile-B . 你可以在这里看到例子:https://github.com/jenkinsci/pipeline-examples/tree/master/pipeline-examples/load-from-file
创建jenkins共享库 . 见https://jenkins.io/doc/book/pipeline/shared-libraries/
使用管道远程加载程序插件 - https://wiki.jenkins.io/display/JENKINS/Pipeline+Remote+Loader+Plugin您可以在此处找到如何使用该插件 - https://github.com/jenkinsci/workflow-remote-loader-plugin
根据您的更新,您尝试使用4选项 . 您应该在使用之前安装该插件 .