从与当前管道源相同的分支加载 Jenkins 共享库

发布于 2025-01-20 19:49:51 字数 555 浏览 4 评论 0原文

背景

我有复杂的构建系统,该系统正在运行并使用共享库。管道代码与共享库存储在同一 git 存储库中。两个源都位于 master 分支上。

问题

现在我进行了更大的重构来改进构建和测试过程。因此,我正在开发 feature 分支,并配置了相应的 Jenkins 作业来测试它。

由于我还对共享库引入了更改,所以有一件事情很烦人:要导入库,我必须以这种方式导入该库:

@Library('my_library@feature') _

因此,要将这些更改合并到 master ,我必须更新代码。

有没有办法访问当前管道代码已签出的分支(或其他类型的引用)? 因此,当我合并分支时,共享库将遵循而不更改代码。

我在想这样的事情:

@Library("my_library@${PIPELINE_SOURCE_REF}") _

我搜索文档和互联网,但没有找到这样的东西。

或者有其他解决方案吗?

Background

I have complex build system which is working and uses shared library. Pipelines code is stored in same git repository as shared library. Both sources are on master branch.

Problem

Now I do larger refactoring to improve build and test process. So I'm working on feature branch and I configured respective Jenkins job to test it.

Since I introduce changes also to shared library one thing is annoying: to import library I have to import this library this way:

@Library('my_library@feature') _

So to merge this changes to master I have to update code.

Is there a way to access branch (or other kind of reference) which current pipeline code was checkout?
So when I merge branches shared library follows to without altering code.

I was thinking something like this:

@Library("my_library@${PIPELINE_SOURCE_REF}") _

I search documentation and internet and didn't found anything like this.

Or is there an alternative solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

征棹 2025-01-27 19:49:51

如果您足以将参数用于库分支,则可以这样做,请查看共享库文档

您需要更改:

@Library('my_library@feature') _

为此

library("my_library@${params['BRANCH']}")

,应该加载全局var。

如果您需要实例化某些课程,则可以做类似的事情:

def someClass = library("my_library@${params['BRANCH']}").com.mypackage.SomeClass.new(this)

文档中提到的一些局限性,取决于您的图书馆的外观

If it is enough for you to use a parameter for the library branch, it is possible to do so, check out the shared libraries documentation

You would need to change:

@Library('my_library@feature') _

to

library("my_library@${params['BRANCH']}")

This should load the global vars.

If you need to instantiate some class, it is possible to do something like:

def someClass = library("my_library@${params['BRANCH']}").com.mypackage.SomeClass.new(this)

It has some limitations mentioned in the docs, depends on how your library looks like

天荒地未老 2025-01-27 19:49:51

在一些非公共存储库中,我在管道前面发现了类似的东西。

def pipelineBranch = scm.branches[0].name
library("someLibrary@${pipelineBranch}")

尚未测试,但看起来很合理。 scm.branches[0].name 应包含用于签出管道代码的分支名称。

On some none public repository I've found something like this in front of the pipeline.

def pipelineBranch = scm.branches[0].name
library("someLibrary@${pipelineBranch}")

Didn't test it yet, but it seams reasonable. scm.branches[0].name should contain name of branch used to checkout pipeline code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文