【问题标题】:Jenkins shared library get versionJenkins共享库获取版本
【发布时间】:2024-01-22 16:50:01
【问题描述】:
我在 jenkinsfiles 中加载了带有 @Library('libName') 注释的共享库。如何获取知识(在管道的代码中)已加载哪个版本?如何区分库是否已加载使用:
@Library('libName')、@Library('libName@master') 或 @Library('libName@superBranch')?
问候,大卫。
【问题讨论】:
标签:
jenkins
jenkins-pipeline
shared-libraries
【解决方案1】:
以下在 Jenkins 2.318 上适用于我并返回分支名称,至少在库中:
env."library.LIBNAME.version"
LIBNAME 是您的库的名称,因此在您的示例中:
echo "library version: ${env."library.libName.version"}"
将打印例如master 或 superBranch。
【解决方案2】:
你可以在下面做类似的事情。
@Library('MyLibrary@test') _
node('master') {
dir( "${WORKSPACE}@libs/MyLibrary") {
//This is the path library.
//Run any command to get branch name
}
}
要点:如果您同时运行此作业,库目录名称将类似于 MyLibrary@2,具体取决于内部版本号。
希望这会有所帮助。
【解决方案3】:
所以这并不容易,但这就是我的项目所做的。我们使用 git 标签,但它本质上是相同的概念。但是,因为我们使用约定,所以我们可以区分。 (Jenkins 共享结帐首先检查 @'whatever' 作为一个分支,然后是一个标签修订)。
这是引擎盖下的东西,所以不能保证在 jenkins 开发过程中它会保持不变。
如果它被锁定到一个版本,包装函数本质上返回真/假。每当它的 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
}