Marek R
Marek R

Reputation: 38209

Load Jenkins shared library from same branch as current pipeline source

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?

Upvotes: 2

Views: 1819

Answers (2)

Marek R
Marek R

Reputation: 38209

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.

Upvotes: 1

VANAN
VANAN

Reputation: 578

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

Upvotes: 1

Related Questions