Jenkins共享库获取版本

时间:2019-11-15 14:00:41

标签: jenkins jenkins-pipeline shared-libraries

我在jenkinsfiles中加载了带有 Application ppApp = new Application(); ppApp.Visible = MsoTriState.msoTrue; Presentations ppPresens = ppApp.Presentations; Presentation objPres = ppPresens.Open("C:\\Users\\Users\\Documents\\Projects\\LS\\WindowsFormsApp1\\PPT.pptx", MsoTriState.msoFalse, MsoTriState.msoTrue, MsoTriState.msoTrue); Slides objSlides = objPres.Slides; SlideShowWindows objSSWs; SlideShowSettings objSSS; //Run the Slide show objSSS = objPres.SlideShowSettings; objSSS.Run(); objSSWs = ppApp.SlideShowWindows; while (objSSWs.Count >= 1) System.Threading.Thread.Sleep(100); //Close the presentation without saving changes and quit PowerPoint objPres.Close(); ppApp.Quit(); 批注的共享库。如何获取知识(在管道代码中)已加载哪个版本?如何区分是否已使用以下方法加载了库: @Library('libName')@Library('libName')@Library('libName@master')? 戴维德,敬上。

2 个答案:

答案 0 :(得分:0)

您可以在下面做类似的事情。

@Library('MyLibrary@test') _
node('master') {

    dir( "${WORKSPACE}@libs/MyLibrary") {
        //This is the path library.
        //Run any command to get branch name
    }
}

要点:如果同时运行此作业,则库目录名称将类似于MyLibrary@2,具体取决于内部版本号。

希望这会有所帮助。

答案 1 :(得分:0)

所以这并不容易,但这就是我的项目所做的。我们使用git标签,但其本质上是相同的概念。但是,由于我们使用约定,因此可以区分。 (Jenkins共享结帐首先将@'whatever'作为分支进行检查,然后对标签进行修订。)

这是内幕内容,因此无法保证在詹金斯开发过程中它将保持不变。

如果包装器功能已锁定到某个版本,则该包装器本质上将返回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
    }