[英]Is it possible to include calls in Declarative Pipelines in the shared library (Jenkins)?
As you know in shared libraries in Jenkins, it is possible to call in the Jenkinsfile a content of a file in vars folder. 如您所知,在Jenkins的共享库中,可以在Jenkinsfile中调用vars文件夹中文件的内容。
For example in vars folder in the shared library we can have a file named build.groovy
and in the Jenkinsfile we can call it by : 例如,在共享库的vars文件夹中,我们可以有一个名为
build.groovy
的文件,在Jenkinsfile中,我们可以通过以下方式调用它:
build { parameter1 = "some param1" parameter2 = "some param2" }
As it is described in this section . 如本节所述 。 I have no problems having groovy files and calling them with
call()
method in Jenkinsfile. 我在使用groovy文件并在Jenkinsfile中使用
call()
方法调用它们时没有问题。
But I want to customize a pipeline and make it as generic as possible. 但我想自定义管道,并使其尽可能通用。 So I want to call a groovy file contained in
vars
folder but in the same pipeline : Calling a genericStage.groovy
in an other file contained in the same vars folder in the shared library. 所以我想在
vars
文件夹中但在同一管道中调用一个groovy文件:在共享库的同一vars文件夹中包含的另一个文件中调用genericStage.groovy
。
So what I have is a groovy file in vars
folder : genericStage.groovy
What I have is : 所以我所拥有的是
vars
文件夹中的一个groovy文件: genericStage.groovy
我所拥有的是:
Pipeline{
agent{label myNode}
stages{
stage("init"){
//steps
}
genericStage{
parameter1 = "some param"
}
}
}
And in the genericStage : 在genericStage中:
def call(Closure body) {
def config = [:]
body.resolveStrategy = Closure.DELEGATE_FIRST
body.delegate = config
body()
stage(config.parameter1){
steps{
//steps
}
}
} }
But I get the error : 但是我得到了错误:
Expected a stage @ line 125, column 6.
genericStage{
^
1 error
at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1085)
So how to make calls in the same shared library as we do for the Jenkinsfile ? 那么,如何像我们对Jenkinsfile一样在同一共享库中进行调用?
In your shared library method defining a DSL style step like this you can either define a block of steps and script that does some common processing you need to share between jobs or stages in a job or a whole complete pipeline. 在定义这种DSL风格步骤的共享库方法中,您可以定义一个步骤和脚本块,以执行一些需要在作业之间或作业中的阶段或整个完整管道中共享的一些通用处理。 So your genericStage.groovy can only contain what comes after the "// steps" comment and cannot contain the stage and steps definition like this.
因此,您的genericStage.groovy只能包含“ //步骤”注释之后的内容,而不能包含这样的阶段和步骤定义。 I do this type of library custom step quite a lot in the style you have here but without trying to define the stage / steps inside it and it works fine.
我以您在此处使用的样式执行了很多此类库自定义步骤,但没有尝试定义其中的阶段/步骤,因此效果很好。 What is happening for you here is the pipeline validation / parser is failing the main pipeline syntax before it even gets to handling your custom step because you don't have it wrapped in a stage and steps definition.
这里发生的事情是管道验证/解析器在处理自定义步骤之前使主要管道语法失败,因为您没有将其包装在阶段和步骤定义中。
If you read the documentation link you have at the end there is a section on defining a complete declarative pipeline in a shared library. 如果您阅读了文档链接,最后将有一节介绍如何在共享库中定义完整的声明性管道。 It says at the end "Only entire pipeline
s can be defined in shared libraries as of this time. This can only be done in
vars/*.groovy, and only in a call method. Only one Declarative Pipeline can be executed in a single build, and if you attempt to execute a second one, your build will fail as a result." 最后,它说:“到目前为止,只能
s can be defined in shared libraries as of this time. This can only be done in
整个管道s can be defined in shared libraries as of this time. This can only be done in
vars / *。groovy中,并且只能在调用方法中进行。单个声明式管道只能在一个中执行构建,如果您尝试执行第二个构建,则构建将因此失败。” This suggests that a partial pipeline like you have won't work, you should have just steps / script or a complete pipeline. 这表明像您这样的部分管道将无法工作,您应该只有步骤/脚本或完整的管道。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.