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

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

问题描述

我最近正在考虑使用 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 repo.

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

有什么好主意吗?

推荐答案

共享库(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.

例如这是 我的图书馆这是一个调用这个通用函数的 Jenkinsfile.

编辑(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/...') 注释指向共享库 repo 的 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天全站免登陆