[英]Jenkins shared library get version
我在 jenkinsfiles 中加載了帶有@Library('libName')
注釋的共享庫。 如何獲取知識(在管道的代碼中)已加載哪個版本? 如何區分庫是否已使用: @Library('libName')
、 @Library('libName@master')
或@Library('libName@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 開發過程中它會保持不變。
如果包裝器 function 被鎖定到某個版本,則它基本上返回真/假。 每當它的 vxxx 時,我們都會返回它您可能會在它不是默認分支時返回(無論您在 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
}
以下適用於 Jenkins 2.318 並返回分支名稱,至少在庫內:
env."library.LIBNAME.version"
其中LIBNAME
是您的庫的名稱,因此在您的示例中:
echo "library version: ${env."library.libName.version"}"
將打印例如master
或superBranch
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.