Reputation: 63
I have got shared library loaded with @Library('libName')
annotation in jenkinsfiles. How to get knowledge (in the code of the pipeline) which version has been loaded? How to distinguish if the library has been loaded using:
@Library('libName')
, @Library('libName@master')
or @Library('libName@superBranch')
?
Regards, Dawid.
Upvotes: 6
Views: 3605
Reputation: 996
The following works for me on Jenkins 2.318 and returns the branch name, at least inside the library:
env."library.LIBNAME.version"
Where LIBNAME
is the name of your library, so in your example:
echo "library version: ${env."library.libName.version"}"
Would print e.g. master
or superBranch
.
Upvotes: 8
Reputation: 2882
So this is not easy, but this is what my project does. We use git tags but its essentially the same concept. However because we use a convention, we can differentiate. (Jenkins shared checkouts check the @'whatever' as a branch first and then a tag revision).
This is under the hood stuff, so no guarantee it will stay the same during jenkins development.
The wrapper function essentially returns true/false if its been locked to a version. We return this whenever its v.x.x.x. You would probably return whenever its not the default branch (whatever you have set in jenkins)
/**
* Wrapper for checking if loaded jenkins shared libs are pointing to a git branch or tag
*
* @return Boolean
*/
Boolean isLockedSharedLibraryRevision() {
List<Action> actions = $build().getActions(BuildData.class)
return checkSharedLibraryBranches(actions)
}
/**
* Check if shared libraries are locked to specific git tag (commit hash)
* Return True if running on a particular revision (Git Tag)
* Return False if running on HEAD of a branch (develop by default)
*
* Assumption is that Git Tag follows format vx.x.x (e.g. v1.0.22)
*
* @param actions (List of jenkins actions thatmatch BuildData.class)
* @return Boolean
*/
Boolean checkSharedLibraryBranches(List<Action> actions) {
Boolean isLockedSharedLibraryRevision = false
Boolean jenkinsSharedFound = false
if (actions == null || actions.size() == 0) {
throw new IllegalArgumentException("Build actions must be provided")
}
// Check each BuildData Action returned for one containing the jenkins-shared revisions
actions.each { action ->
HashSet remoteURLs = action.getRemoteUrls()
remoteURLs.each { url ->
if ( url.contains('<insert-your-repo-name>') ) {
jenkinsSharedFound = true
Pattern versionRegex = ~/^v\d+\.\d+\.\d+$/
/**
* When jenkins-shared is found evaluate revision branch/tag name.
* getLastBuiltRevision() returns the current executions build. This was functionally tested.
* If a newer build runs and completes before the current job, the value is not changed.
* i.e. Build 303 starts and is in progress, build 304 starts and finishes.
* Build 303 calls getLastBuiltRevision() which returns job 303 (not 304)
*/
Revision revision = action.getLastBuiltRevision()
/**
* This is always a collection of 1, even when multiple tags exist against the same sha1 in git
* It is always the tag/branch your looking at and doesn't report any extras...
* Despite this we loop to be safe
*/
Collection<Branch> branches = revision.getBranches()
branches.each { branch ->
String name = branch.getName()
if (name ==~ versionRegex) {
println "INFO: Jenkins-shared locked to version ${name}"
isLockedSharedLibraryRevision = true
}
}
}
}
}
if (!jenkinsSharedFound) {
throw new IllegalArgumentException("None of the related build actions have a remoteURL pointing to Jenkins Shared, aborting")
}
println "INFO: isLockedSharedLibraryRevision == ${isLockedSharedLibraryRevision}"
return isLockedSharedLibraryRevision
}
Upvotes: 0
Reputation: 111
You can do something similar below.
@Library('MyLibrary@test') _
node('master') {
dir( "${WORKSPACE}@libs/MyLibrary") {
//This is the path library.
//Run any command to get branch name
}
}
Important point: If you run this job concurrently, library directory names will be something like this MyLibrary@2
depending on the build number.
Hope this helps.
Upvotes: 0