我有一个在jenkinsfiles中使用@Library('libName')
注释加载的共享库。如何在管道代码中获取已加载的版本信息?如何区分使用以下方式加载库:
@Library('libName')
,@Library('libName@master')
或@Library('libName@superBranch')
?
谢谢,Dawid。
我有一个在jenkinsfiles中使用@Library('libName')
注释加载的共享库。如何在管道代码中获取已加载的版本信息?如何区分使用以下方式加载库:
@Library('libName')
,@Library('libName@master')
或@Library('libName@superBranch')
?
谢谢,Dawid。
LIBNAME
是你的库的名称,因此在你的例子中:
echo "library version: ${env."library.libName.version"}"
将打印例如master
或superBranch
。您可以在下面实现类似的功能。
@Library('MyLibrary@test') _
node('master') {
dir( "${WORKSPACE}@libs/MyLibrary") {
//This is the path library.
//Run any command to get branch name
}
}
MyLibrary@2
。这不是一件容易的事情,但这就是我的项目所做的。我们使用git标签,但本质上是相同的概念。然而,由于我们采用了一种惯例,因此我们可以区分开来。(Jenkins共享检出将@'whatever'作为分支首先检查,然后检查标签修订版)。
这是底层的东西,所以不能保证在jenkins开发过程中会保持不变。
包装函数实际上返回true/false,如果它已锁定到一个版本。每当它是v.x.x.x时,我们就会返回这个值。你可能会在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
}