在编写jenkins管道时,提交每个新更改以查看它是否有效似乎非常不方便。
是否有一种方法可以在本地执行这些而不提交代码?
在编写jenkins管道时,提交每个新更改以查看它是否有效似乎非常不方便。
是否有一种方法可以在本地执行这些而不提交代码?
当前回答
将SSH密钥放入Jenkins配置文件,然后使用声明式linter,如下所示:
ssh jenkins.hostname.here declarative-linter < Jenkinsfile
这将对你的詹金斯档案进行静态分析。在您选择的编辑器中,定义一个自动运行该命令的键盘快捷方式。在Visual Studio Code中,这是我使用的,转到Tasks > Configure Tasks,然后使用以下JSON创建一个Validate Jenkinsfile命令:
{
"version": "2.0.0",
"tasks": [
{
"label": "Validate Jenkinsfile",
"type": "shell",
"command": "ssh jenkins.hostname declarative-linter < ${file}"
}
]
}
其他回答
据我所知,这个管道插件是新的Jenkinsfile机制的“引擎”,所以我非常肯定你可以用它来本地测试你的脚本。
我不确定是否有任何额外的步骤,当你复制到一个Jenkinsfile,但语法等应该完全相同。
编辑:在“引擎”上找到了参考,检查这个功能描述,最后一段,第一个条目。
有一些限制,对于脚本管道,我使用这个解决方案:
使用内联groovy脚本的管道作业:
node('master') {
stage('Run!') {
def script = load('...you job file...')
}
}
用于测试的Jenkinsfile具有与lesfuret相同的结构:
def execute() {
... main job code here ...
}
execute()
将SSH密钥放入Jenkins配置文件,然后使用声明式linter,如下所示:
ssh jenkins.hostname.here declarative-linter < Jenkinsfile
这将对你的詹金斯档案进行静态分析。在您选择的编辑器中,定义一个自动运行该命令的键盘快捷方式。在Visual Studio Code中,这是我使用的,转到Tasks > Configure Tasks,然后使用以下JSON创建一个Validate Jenkinsfile命令:
{
"version": "2.0.0",
"tasks": [
{
"label": "Validate Jenkinsfile",
"type": "shell",
"command": "ssh jenkins.hostname declarative-linter < ${file}"
}
]
}
您不能在本地执行Pipeline脚本,因为它的全部目的是编写Jenkins脚本。(这就是为什么Jenkins文件最好保持简短,并限制在实际处理Jenkins特性的代码中;你实际的构建逻辑应该通过外部进程或构建工具来处理,你可以通过一行sh或bat步骤调用它们。)
如果你想测试对Jenkinsfile的修改,但不提交,可以使用1.14中添加的Replay功能。
JENKINS-33925跟踪自动测试框架的特性请求。
博士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示例