在编写jenkins管道时,提交每个新更改以查看它是否有效似乎非常不方便。

是否有一种方法可以在本地执行这些而不提交代码?


当前回答

在我的开发设置中(缺少合适的Groovy编辑器),大量的Jenkinsfile问题源于简单的语法错误。要解决这个问题,你可以根据Jenkins实例验证Jenkinsfile(运行在$JENKINS_HTTP_URL):

curl -X POST -H $(curl '$JENKINS_HTTP_URL/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)') -F "jenkinsfile=< jenkinsfile " $JENKINS_HTTP_URL/管道模型转换器/验证

上面的命令是对 -a-Declarative-Jenkinsfile-from-the-command-line https://github.com/jenkinsci/pipeline-model-definition-plugin/wiki/Validating- (or-linting)

其他回答

据我所知,这个管道插件是新的Jenkinsfile机制的“引擎”,所以我非常肯定你可以用它来本地测试你的脚本。

我不确定是否有任何额外的步骤,当你复制到一个Jenkinsfile,但语法等应该完全相同。

编辑:在“引擎”上找到了参考,检查这个功能描述,最后一段,第一个条目。

在我的开发设置中(缺少合适的Groovy编辑器),大量的Jenkinsfile问题源于简单的语法错误。要解决这个问题,你可以根据Jenkins实例验证Jenkinsfile(运行在$JENKINS_HTTP_URL):

curl -X POST -H $(curl '$JENKINS_HTTP_URL/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)') -F "jenkinsfile=< jenkinsfile " $JENKINS_HTTP_URL/管道模型转换器/验证

上面的命令是对 -a-Declarative-Jenkinsfile-from-the-command-line https://github.com/jenkinsci/pipeline-model-definition-plugin/wiki/Validating- (or-linting)

有点晚了,但这就是为什么我写了jenny,一个小的重新实现一些核心的Jenkinsfile步骤。(https://github.com/bmustiata/jenny)

博士TL;

Jenkins Pipeline单元测试框架 Jenkinsfile跑步

长版本 管道测试越来越让人头疼。不同于经典的声明式作业配置方法(其中用户仅限于UI所公开的内容),新的Jenkins Pipeline是一种用于构建过程的成熟编程语言,您可以将声明式部分与自己的代码混合在一起。作为优秀的开发人员,我们也希望对这类代码进行单元测试。

在开发Jenkins pipeline时,您应该遵循三个步骤。第一步。应该覆盖80%的用例。

Do as much as possible in build scripts (eg. Maven, Gradle, Gulp etc.). Then in your pipeline scripts just calls the build tasks in the right order. The build pipeline just orchestrates and executes the build tasks but does not have any major logic that needs a special testing. If the previous rule can't be fully applied then move over to Pipeline Shared libraries where you can develop and test custom logic on its own and integrate them into the pipeline. If all of the above fails you, you can try one of those libraries that came up recently (March-2017). Jenkins Pipeline Unit testing framework or pipelineUnit (examples). Since 2018 there is also Jenkinsfile Runner, a package to execution Jenkins pipelines from a command line tool.

例子

pipelineUnit GitHub回购包含了一些关于如何使用Jenkins Pipeline Unit测试框架的Spock示例

除了其他人已经提到的重放功能(关于它的有用性也一样!),我发现以下功能也很有用:

创建一个测试管道作业,您可以在其中键入管道代码或指向Jenkinsfile的repo/分支来快速测试一些东西。为了更准确的测试,使用Multibranch Pipeline,它指向你自己的fork,在那里你可以快速做出改变并提交,而不影响proch。 由于Jenkinsfile是Groovy代码,只需使用“Groovy Jenkinsfile”调用它来验证基本语法。