创建可重用的詹金斯管道脚本 [英] Create resusable jenkins pipeline script

查看:80
本文介绍了创建可重用的詹金斯管道脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑最近使用Jenkins管道脚本,一个问题是我不知道创建内部可重用utils代码的明智方法,想象一下,我有一个通用功能helloworld,该功能将被大量使用管道作业,所以我希望创建一个utils.jar可以将其注入作业类路径.

I am considering to use Jenkins pipeline script recently, one question is that I don't figure out a smart to way to create internal reusable utils code, imagine, I have a common function helloworld which will be used by lots of pipeline jobs, so I hope to create a utils.jar can injected it into the job classpath.

我注意到Jenkins在全局库中也有类似的概念,但是我对此插件的关注:

I notice Jenkins have a similar concept with the global library, but my concern regarding this plugin:

由于它是一个插件,因此我们需要通过jenkins插件管理器进行安装/升级,然后可能需要重新启动才能应用更改,这不是我想要的内容,因为utils可能会更改,请始终添加,我们希望它可能会立即可用.

Since it is a plugin, so we need to install/upgrade it through jenkins plugin manager, then it may require reboot to apply the change, this is not what I want to see since utils may change, add always, we hope it could be available immediately.

第二,这是jenkins的官方共享库,我不想(否则他们将不会应用我们)将私有代码放入jenkins回购中.

Secondly, it is official jenkins shared lib, I dont want to (Or they will not apply us) put private code into jenkins repo.

有什么好主意吗?

推荐答案

Shared Libraries ( docs )允许您使所有管道脚本都可以访问您的代码.您不必为此构建插件,也不必重新启动Jenkins.

The Shared Libraries (docs) allows you to make your code accessible to all your pipeline scripts. You don't have to build a plugin for that and you don't have to restart Jenkins.

例如这是我的图书馆

E.g. this is my library and this a Jenkinsfile that calls this common function.

编辑(2017年2月): 可以通过Jenkins的内部Git服务器访问该库,也可以通过其他方式(例如通过Chef)将其部署到 jenkins 用户主目录中的workflow-lib/目录.(仍然有可能,但非常不方便).

EDIT (Feb 2017): The library can be accessed through Jenkins' internal Git server, or deployed through other means (e.g. via Chef) to the workflow-lib/ directory within the jenkins user's home directory. (still possible, but very unhandy).

可以通过以下方式配置全局库:

The global library can be configured through the following means:

  • Jenkinsfile中的@Library('github.com/...')批注,指向共享库存储库的URL.
  • 在Jenkins作业的文件夹级别上配置.
  • 在Jenkins配置中配置为全局库,其优点是代码受信任,即不受脚本安全性的限制.
  • an @Library('github.com/...') annotation in the Jenkinsfile pointing to the URL of the shared library repo.
  • configured on the folder level of Jenkins jobs.
  • configured in Jenkins configuration as global library, with the advantage that the code is trusted, i.e., not subject to script security.

第一个和最后一个方法的混合将是未显式加载的共享库,然后仅使用Jenkinsfile中的名称进行请求:@Library('mysharedlib').

A mix of the first and last method would be a not explicitly loaded shared library that is then requested only using its name in the Jenkinsfile: @Library('mysharedlib').

这篇关于创建可重用的詹金斯管道脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆