本文介绍了调用与另一个jenkinsfile不同的回购的一个Jenkinsfile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a Jenkinsfile-A like below, In this file,I need to invoke another Jenkinsfile-B after execution of all the stages.

Jenkinsfile-A

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', '[email protected]:org-us-dev/org-cicd-pipelines.git', 'master', 'org-service-user', 'ORG')
}
jenkinsFile.start()

Jenkinsfile-B

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

在Jenkinsfile-A中完成流水线之后,我通过阅读以下代码块添加了def ,但这会导致错误.

I added def after completion of pipeline in Jenkinsfile-A by reading this block Invoke a remote jenkins file from another Jenkinsfile but that resulted in error.

我的错误日志-

    Loading from Jenkinsfile.groovy
[Pipeline] load
[Pipeline] // load
[Pipeline] }
[Pipeline] // dir
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // stage
[Pipeline] End of Pipeline
java.nio.file.NoSuchFileException: /data/jenkins/workspace/org-master-source-build/libLoader/Jenkinsfile.groovy
    at sun.nio.fs.UnixException.translateToIOException(UnixException.java:86)
    at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
    at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
    at sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:214)
    at java.nio.file.Files.newByteChannel(Files.java:361)
    at java.nio.file.Files.newByteChannel(Files.java:407)
    at java.nio.file.spi.FileSystemProvider.newInputStream(FileSystemProvider.java:384)
    at java.nio.file.Files.newInputStream(Files.java:152)
    at hudson.FilePath$Read.invoke(FilePath.java:1901)
    at hudson.FilePath$Read.invoke(FilePath.java:1893)
    at hudson.FilePath$FileCallableWrapper.call(FilePath.java:2917)
    at hudson.remoting.UserRequest.perform(UserRequest.java:212)
    at hudson.remoting.UserRequest.perform(UserRequest.java:54)
    at hudson.remoting.Request$2.run(Request.java:369)
    at hudson.remoting.InterceptingExecutorService$1.call(InterceptingExecutorService.java:72)
Caused: java.io.IOException
    at hudson.remoting.FastPipedInputStream.read(FastPipedInputStream.java:169)
    at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
    at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
    at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
    at java.io.InputStreamReader.read(InputStreamReader.java:184)
    at java.io.Reader.read(Reader.java:140)
    at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:2001)
    at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:1980)
    at org.apache.commons.io.IOUtils.copy(IOUtils.java:1957)
    at org.apache.commons.io.IOUtils.copy(IOUtils.java:1907)
    at org.apache.commons.io.IOUtils.toString(IOUtils.java:778)
    at org.apache.commons.io.IOUtils.toString(IOUtils.java:759)
    at hudson.FilePath.readToString(FilePath.java:1980)
    at org.jenkinsci.plugins.workflow.cps.steps.LoadStepExecution.start(LoadStepExecution.java:37)
    at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:270)
    at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:180)
    at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:122)
    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:48)
    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 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)
    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)
    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)
    at ___cps.transform___(Native Method)
    at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:57)
    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:109)
    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:82)
    at sun.reflect.GeneratedMethodAccessor127.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.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:103)
    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:82)
    at sun.reflect.GeneratedMethodAccessor127.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.ContinuationGroup.methodCall(ContinuationGroup.java:60)
    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:109)
    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:82)
    at sun.reflect.GeneratedMethodAccessor127.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

推荐答案

无法直接从另一个jenkinsfile调用jenkinsfile.但是您有一些选择:

There is no way to invoke jenkinsfile from another jenkinsfile directly. But you have some options:

  1. 使用 build jenkins步骤来触发运行jenkinsfile-B的构建.有关更多信息,请参见 https://jenkins.io/doc/pipeline/steps /pipeline-build-step/
  2. 结合使用 load scm jenkins步骤,使用jenkinsfile-B签出存储库,并将其作为常规代码加载.有关更多信息,请参见 https://jenkins.io/doc/pipeline/steps /workflow-scm-step/ https://jenkins.io/doc/pipeline/steps/workflow-cps/#code-load-code-evaluate-a-groovy-source-文件到管道脚本中.但是在这种情况下,您应该重写jenkinsfile-B.您可以在此处看到示例: https://github.com/jenkinsci/pipeline-examples/tree/master/pipeline-examples/load-from-file
  3. 创建jenkins共享库.请参见 https://jenkins.io/doc/book/book/pipeline/shared-libraries/
  4. 使用管道远程加载程序插件- https://wiki.jenkins .io/display/JENKINS/Pipeline + Remote + Loader + Plugin 您可以在此处找到如何使用该插件- https://github.com/jenkinsci/workflow-remote-loader-plugin
  1. Use build jenkins step to trigger build running your jenkinsfile-B. For more info see https://jenkins.io/doc/pipeline/steps/pipeline-build-step/
  2. Use load with combination scm jenkins step to checkout repository with your jenkinsfile-B and load that as groovy code. For more info see https://jenkins.io/doc/pipeline/steps/workflow-scm-step/ and https://jenkins.io/doc/pipeline/steps/workflow-cps/#code-load-code-evaluate-a-groovy-source-file-into-the-pipeline-script. But in that case you should rewrite your jenkinsfile-B. You can see example here: https://github.com/jenkinsci/pipeline-examples/tree/master/pipeline-examples/load-from-file
  3. Create jenkins shared library. See https://jenkins.io/doc/book/pipeline/shared-libraries/
  4. Use pipeline remote loader plugin - https://wiki.jenkins.io/display/JENKINS/Pipeline+Remote+Loader+Plugin You can find how to use that plugin here - https://github.com/jenkinsci/workflow-remote-loader-plugin

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

According to your update you tried to use 4 option. You should install that plugin before using it.

这篇关于调用与另一个jenkinsfile不同的回购的一个Jenkinsfile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 08:22