简体   繁体   English

快速周转Jenkins管道脚本开发

[英]Fast-turnaround Jenkins Pipeline script development

Is there a way to write Jenkins Pipeline script interactively, like you can run other Groovy in the Jenkins Script Console? 有没有办法以交互方式编写Jenkins管道脚本,就像您可以在Jenkins脚本控制台中运行其他Groovy一样? Ideally I effectively want "pipeline groovysh in Jenkins". 理想情况下,我有效地想要“詹金斯的管道groovysh ”。

AFAICS the script console doesn't set up any of the environment needed to make Pipeline work so you can't just run ad-hoc chunks of pipeline jobs there. AFAICS脚本控制台没有设置使Pipeline工作所需的任何环境,因此您不能只在那里运行ad-hoc块管道作业。

I'm aware of the "Replay" option, but it's painfully slow for a remote Jenkins and doesn't integrate well into VCS. 我知道“重播”选项,但是对于远程Jenkins而言它很慢并且不能很好地集成到VCS中。

Background: As I'm doing a lot of work on Jenkins pipelines lately I'm finding that the test cycle is becoming painful. 背景:由于我最近在Jenkins管道上做了很多工作,我发现测试周期变得很痛苦。 Ironic really. 讽刺真的。 Commit, push to working branch, "build with parameters" (or rebuild) a job pointing to the the working branch for the pipleine script, wait for results, examine console log, repeat. 提交,推送到工作分支,“使用参数构建”(或重建)指向pipleine脚本的工作分支的作业,等待结果,检查控制台日志,重复。

Tedious. 乏味。

I've improved it a little by having my test job auto-trigger on pushes to my pipeline repo but that's still pretty clumsy. 通过将我的测试作业自动触发推送到我的管道回购,我已经改进了一点,但这仍然非常笨拙。

Firing up a local Jenkins for testing is not very practical - the pipelines need to interact with a private Docker registry, need a specific plugin config, need some specific worker types, etc. It also doesn't help a great deal with the turnaround. 启动本地Jenkins进行测试并不是很实用 - 管道需要与私有Docker注册表进行交互,需要特定的插件配置,需要一些特定的工作者类型等。它也无助于解决周转问题。

So how do others do it better? 那么别人怎么做得更好呢? jenkins-cli remote control? jenkins-cli遥控器? Other? 其他?

There are two ways of working on pipelines that I have used that alleviate the painful develop-test-fail-fix cycle that you are experiencing: 我使用了两种处理管道的方法,可以减轻您遇到的痛苦的开发 - 测试 - 失败 - 修复周期:

1) Unit Test Based Test Method 1)基于单元测试的测试方法

Write the pipelines in a unit test framework with everything completely mocked using an IDE like IntelliJ IDEA. 在单元测试框架中编写管道,使用像IntelliJ IDEA这样的IDE完全模拟所有内容。 Create unit tests developed in Groovy using Spock or JUnit and making use of the JenkinsPipelineUnit test framework. 使用Spock或JUnit创建在Groovy中开发的单元测试,并使用JenkinsPipelineUnit测试框架。 This can be OK if you are comfortable writing unit tests and mocking, but many people are not. 如果您愿意编写单元测试和模拟,这可能没问题,但很多人不是。 It worked for me for most of my pipelines. 它适用于我的大部分管道。

2) Using Docker and File System SCM Plugin 2)使用Docker和文件系统SCM插件

Spin up Jenkins in Docker on your development machine configuring it as near as possible to your production system using Jenkins Configuration as Code (JCasC) . 在开发机器上的Docker中调用Jenkins,使用Jenkins Configuration as Code(JCasC)将其尽可能地配置到您的生产系统。 In the Docker container running Jenkins map / bind mount a directory on the host system containing all the pipeline code into the Docker container. 在运行Jenkins map / bind的Docker容器中,将包含所有管道代码的主机系统上的目录挂载到Docker容器中。 You can then use a Jenkins plugin called " File system SCM Plugin ". 然后,您可以使用名为“ File system SCM Plugin ”的Jenkins插件。 This FS SCM plugin will allow you to configure the pipeline jobs in the Dockerized Jenkins to get their pipeline code directly off the file system on your host mapped into the Docker container. 此FS SCM插件允许您在Dockerized Jenkins中配置管道作业,以直接从映射到Docker容器的主机上的文件系统获取其管道代码。 So what you do is open up the pipeline code in an IDE on your host system, edit as much as you like and execute the job in the Dockerized Jenkins without having to commit. 因此,您所做的是在主机系统上的IDE中打开管道代码,根据需要进行编辑,并在Dockerized Jenkins中执行作业,而无需提交。 Then when done with debugging / developing, commit your code for the production server. 然后在完成调试/开发时,为生产服务器提交代码。 This method is based on work and ideas from Oleg Nenashev at CloudBees and it is worth taking a look at his presentation "How to Develop Your Jenkins Pipeline Locally" . 这种方法基于来自CloudBees的Oleg Nenashev的工作和想法,值得一看他的演讲“如何在本地开发Jenkins管道” To set up Jenkins in Docker with most of it configured from YAML have a look through the JCasC README file and also at the Jenkins Docker project to see how to configure the installed plugins. 要在Docker中设置Jenkins,其中大部分都是从YAML配置的,请查看JCasC README文件以及Jenkins Docker项目,了解如何配置已安装的插件。

You may also get some useful information on this subject from the Jenkins Pipeline Authoring special interest group . 您还可以从Jenkins Pipeline Authoring特殊兴趣小组获得有关此主题的一些有用信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM