将不同存储库的一个 Jenkinsfile 调用到另一个 jenkinsfile

Kal*_*lly 3 groovy jenkins-groovy jenkins-pipeline jenkins-declarative-pipeline

我有一个如下所示的 Jenkinsfile-A,在这个文件中,我需要在执行所有阶段后调用另一个 Jenkinsfile-B。

\n\n

Jenkinsfile-A

\n\n
#!groovy\xe2\x80\x8b\npipeline {\n    agent { label "" }\n    triggers { pollSCM(\'*/5 * * * *\') }\n     stages {\n        stage(\'Build Artifact\') {\n            steps {\n            }\n        }\n        stage(\'Publish Artifact\') {\n            steps {\n            }\n        }\n     }//stages\n     post {\n        always {\n            deleteDir() /* clean up our workspace */\n        }\n    }\n}//pipeline\ndef jenkinsFile\nstage(\'Loading Jenkinsfile\'){\n\n  jenkinsFile = fileLoader.fromGit(\'Jenkinsfile\', \'git@bitbucket.org:org-us-dev/org-cicd-pipelines.git\', \'master\', \'org-service-user\', \'ORG\')\n}\njenkinsFile.start()\n
Run Code Online (Sandbox Code Playgroud)\n\n

Jenkinsfile-B

\n\n
#!groovy\nvoid Download_Repositories(){\n}\nvoid SonarQube_Analysis(){\n}\nvoid Deploy_And_Test(){\n}\nvoid Prepare_Sonar_Scanner(){\n}\nvoid Publish_Artifact(){\n}\nvoid Host_Verification(){\n}\npipeline {\n    agent { label \'\' }\n//    triggers { pollSCM(\'*/5 * * * *\') }\n\n    stages {\n      stage(\'Download Repositories\') {\n        steps {\n            Download_Repositories()\n          }\n        }\n        stage(\'Host Verification\') {\n            steps {\n                Host_Verification()\n            }\n        }\n        stage(\'Env: DEV - Deploy and Test\') {\n            steps {\n              Deploy_And_Test()\n            }\n        }\n        stage(\'Prepare Sonar Scanner\') {\n         steps {\n            Prepare_Sonar_Scanner()\n         }\n        }\n        stage(\'SonarQube Analysis\') {\n            steps {\n              SonarQube_Analysis()\n            }\n        }\n        stage(\'Publish Artifact\') {\n          steps {\n              Publish_Artifact()\n          }\n        }\n   }//stages\n    post {\n        always {\n            deleteDir() /* clean up our workspace */\n        }\n    }//post\n}//pipeline\n
Run Code Online (Sandbox Code Playgroud)\n\n

我在 Jenkinsfile-A 中完成管道后通过读取此块添加了 def从另一个 Jenkinsfile 调用远程 jenkins 文件,但这导致了错误。

\n\n

我的错误日志-

\n\n
    Loading from Jenkinsfile.groovy\n[Pipeline] load\n[Pipeline] // load\n[Pipeline] }\n[Pipeline] // dir\n[Pipeline] }\n[Pipeline] // node\n[Pipeline] }\n[Pipeline] // stage\n[Pipeline] End of Pipeline\njava.nio.file.NoSuchFileException: /data/jenkins/workspace/org-master-source-build/libLoader/Jenkinsfile.groovy\n    at sun.nio.fs.UnixException.translateToIOException(UnixException.java:86)\n    at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)\n    at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)\n    at sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:214)\n    at java.nio.file.Files.newByteChannel(Files.java:361)\n    at java.nio.file.Files.newByteChannel(Files.java:407)\n    at java.nio.file.spi.FileSystemProvider.newInputStream(FileSystemProvider.java:384)\n    at java.nio.file.Files.newInputStream(Files.java:152)\n    at hudson.FilePath$Read.invoke(FilePath.java:1901)\n    at hudson.FilePath$Read.invoke(FilePath.java:1893)\n    at hudson.FilePath$FileCallableWrapper.call(FilePath.java:2917)\n    at hudson.remoting.UserRequest.perform(UserRequest.java:212)\n    at hudson.remoting.UserRequest.perform(UserRequest.java:54)\n    at hudson.remoting.Request$2.run(Request.java:369)\n    at hudson.remoting.InterceptingExecutorService$1.call(InterceptingExecutorService.java:72)\nCaused: java.io.IOException\n    at hudson.remoting.FastPipedInputStream.read(FastPipedInputStream.java:169)\n    at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)\n    at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)\n    at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)\n    at java.io.InputStreamReader.read(InputStreamReader.java:184)\n    at java.io.Reader.read(Reader.java:140)\n    at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:2001)\n    at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:1980)\n    at org.apache.commons.io.IOUtils.copy(IOUtils.java:1957)\n    at org.apache.commons.io.IOUtils.copy(IOUtils.java:1907)\n    at org.apache.commons.io.IOUtils.toString(IOUtils.java:778)\n    at org.apache.commons.io.IOUtils.toString(IOUtils.java:759)\n    at hudson.FilePath.readToString(FilePath.java:1980)\n    at org.jenkinsci.plugins.workflow.cps.steps.LoadStepExecution.start(LoadStepExecution.java:37)\n    at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:270)\n    at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:180)\n    at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:122)\n    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:48)\n    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)\n    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)\n    at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.methodCall(DefaultInvoker.java:20)\n    at org.jenkinsci.plugins.workflow.remoteloader.FileLoaderDSL.FileLoaderDSLImpl.load(jar:file:/opt/efs/jenkins/plugins/workflow-remote-loader/WEB-INF/lib/workflow-remote-loader.jar!/org/jenkinsci/plugins/workflow/remoteloader/FileLoaderDSL/FileLoaderDSLImpl.groovy:109)\n    at org.jenkinsci.plugins.workflow.remoteloader.FileLoaderDSL.FileLoaderDSLImpl.fromGit(jar:file:/opt/efs/jenkins/plugins/workflow-remote-loader/WEB-INF/lib/workflow-remote-loader.jar!/org/jenkinsci/plugins/workflow/remoteloader/FileLoaderDSL/FileLoaderDSLImpl.groovy:46)\n    at org.jenkinsci.plugins.workflow.remoteloader.FileLoaderDSL.FileLoaderDSLImpl.withGit(jar:file:/opt/efs/jenkins/plugins/workflow-remote-loader/WEB-INF/lib/workflow-remote-loader.jar!/org/jenkinsci/plugins/workflow/remoteloader/FileLoaderDSL/FileLoaderDSLImpl.groovy:66)\n    at ___cps.transform___(Native Method)\n    at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:57)\n    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:109)\n    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:82)\n    at sun.reflect.GeneratedMethodAccessor127.invoke(Unknown Source)\n    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\n    at java.lang.reflect.Method.invoke(Method.java:498)\n    at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)\n    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:103)\n    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:82)\n    at sun.reflect.GeneratedMethodAccessor127.invoke(Unknown Source)\n    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\n    at java.lang.reflect.Method.invoke(Method.java:498)\n    at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)\n    at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:60)\n    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:109)\n    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:82)\n    at sun.reflect.GeneratedMethodAccessor127.invoke(Unknown Source)\n    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\n    at java.lang.reflect.Method.invoke(Method.java:498)\n    at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)\n    at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21)\n    at com.cloudbees.groovy.cps.Next.step(Next.java:83)\n    at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:174)\n    at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:163)\n    at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:122)\n    at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:261)\n    at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)\n    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$101(SandboxContinuable.java:34)\n    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.lambda$run0$0(SandboxContinuable.java:59)\n    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:108)\n    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:58)\n    at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:174)\n    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:332)\n    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$200(CpsThreadGroup.java:83)\n    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:244)\n    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:232)\n    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:64)\n    at java.util.concurrent.FutureTask.run(FutureTask.java:266)\n    at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:131)\n    at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)\n    at jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:59)\n    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)\n    at java.util.concurrent.FutureTask.run(FutureTask.java:266)\n    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)\n    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)\n    at java.lang.Thread.run(Thread.java:748)\nFinished: FAILURE\n
Run Code Online (Sandbox Code Playgroud)\n

gro*_*gor 5

无法直接从另一个 jenkinsfile 调用 jenkinsfile。但你有一些选择:

  1. 使用build jenkins 步骤触发构建运行 jenkinsfile-B。有关更多信息,请参阅https://jenkins.io/doc/pipeline/steps/pipeline-build-step/
  2. 使用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-将groovy源文件评估到管道脚本中。但在这种情况下,你应该重写你的 jenkinsfile-B。您可以在此处查看示例: https: //github.com/jenkinsci/pipeline-examples/tree/master/pipeline-examples/load-from-file
  3. 创建詹金斯共享库。请参阅https://jenkins.io/doc/book/pipeline/shared-libraries/
  4. 使用管道远程加载器插件 - https://wiki.jenkins.io/display/JENKINS/Pipeline+Remote+Loader+Plugin您可以在此处找到如何使用该插件 - https://github.com/jenkinsci/workflow-remote -加载器插件

根据您的更新,您尝试使用 4 个选项。您应该在使用该插件之前安装它。