我该如何在流水线步骤中使用Jenkins Sidebar Link插件?

7
我正在开发这个插件 https://plugins.jenkins.io/sidebar-link/ ,用于在 Jenkins 侧边栏中添加一个链接。该插件与 Jenkins 项目配置一起工作。现在我正在尝试添加一个流水线步骤来调用此插件。
我已经尝试了以下代码行,但它没有生效。
sidebarLinks {
            link("my_url", "the title", 'image path')
        }

我已经阅读了相关的线程,但没有得到任何被接受的回应。

我认为Jenkins插件的文档不太好。

是否有人知道如何在流水线中使用它?

更新

我正在使用用Groovy编写的共享库。这个库包含所有流水线方法。

@Library('xxxx@v1.0.0') _
pipeline {
   stages {
      ...
      stage('Add side link') {
            steps {
                addLink()
            }
        }
   }
}

在共享库方面,我有一个addLink.groovy文件。

def call(){

    properties {
        sidebarLinks {
            link("url", 'Title', 'icon_path')
        }
    }
}

我遇到了下面的错误:

ERROR: <- : java.lang.IllegalArgumentException: Could not instantiate {properties=org.jenkinsci.plugins.workflow.cps.CpsClosure2@6b5322b} for JobPropertyStep


你有错误日志吗? - JRichardsz
Jenkins控制台没有错误日志,尽管在Jenkins侧边栏中没有链接。 - 13KZ
@13KZ 你是说,这个链接出现在左侧菜单中,当你点击它时,它会导航到预期的页面?如果你只想编程地遍历到特定的URL,为什么要点击它,难道你不能只使用groovy脚本来导航到URL吗?你是想在运行时显示一个链接在左侧菜单中吗? - mdabdullah
@mdabdullah 我正在尝试在 Jenkins 构建(步骤)期间使用侧边栏链接插件以编程方式添加侧边链接。 - 13KZ
3个回答

6
要在声明性流水线中了解如何做某事,可以使用指令生成器,网址为 http://JENKINS_URL/directive-generator/。这提供了类似于作业配置的用户界面。但是,在选择“选项” -> “添加” -> “sidebarLinks” -> 填写字段 -> “生成”时,由于内部服务器错误,将不会生成任何内容。
以下声明性流水线语法适用于单个作业:
pipeline {
    agent any
    
    options {
        sidebarLinks([
            [displayName: 'Side Bar Example', iconFileName: '', urlName: 'http://example.com']
        ])
    }

    stages {
        stage('Hello') {
            steps {
                echo 'Hello World'
            }
        }
    }
}

然而,您提到希望通过共享流水线库在不同的任务中重用这些链接。不幸的是,共享流水线库不能修改声明性流水线的options部分,除非您通过单个def call()生成整个pipeline { }

幸运的是,脚本式流水线可以覆盖配置的这一部分。使用http://JENKINS_URL/pipeline-syntax/上的代码片段生成器,您可以生成下面的代码片段,并将其放入共享流水线库中,例如在var/mySidebar.groovy中:

def call() {
    properties([
        sidebarLinks([[
            displayName: 'My fancy sidebar url', iconFileName: '', urlName: 'https://example.com'
        ]])
    ])
}

然后你可以在脚本化的流水线中使用它:

library('my-sidebar')

mySidebar()

node() {
    stage('Hello') {
        sh 'echo "Hello World!"'
    }
}

或者一个声明式 Pipeline 的 script 代码块:

library('my-sidebar')

script {
    mySidebarScripted()
}

pipeline {
    agent any
    
    stages {
        stage('Hello') {
            steps {
                echo 'Hello World'
            }
        }
    }
}

1

另一个可用的选项是使用classLoader加载插件,并将新链接作为Action添加到构建或项目级别。
在您的共享库文件中,您可以编写如下内容:

def addSideBarLink(String url, String displayName, String relativeIconPath, Boolean linkToBuild = true) {
   assert url : "The URL parameter cannot be empty"
   assert displayName : "The Display Name parameter cannot be empty"

   def linkActionClass = this.class.classLoader.loadClass("hudson.plugins.sidebar_link.LinkAction")
   def run = linkToBuild ? currentBuild.rawBuild : currentBuild.rawBuild.getParent()
   def action = linkActionClass.newInstance(url, displayName, relativeIconPath)
   println "Adding sidebar link to '${url}' at the ${linkToBuild ? 'build' : 'job'} level"
   run.getActions().add(action)
}

然后从脚本化的流水线或声明性流水线中的脚本块中调用它,方法如下:

pipeline {
    agent any

    stages {
        stage('Hello') {
            steps {
                script {
                    // assuming functions resides inside utils.groovy
                    utils.addSideBarLink(...)
                }
            }
        }
    }
}

0
你是否已经用 properties 包装它了?
job("Sidebar Link Job 2") {
    description()
    keepDependencies(false)
    disabled(false)
    concurrentBuild(false)
    properties {
        sidebarLinks {
            link('https://google.com/', 'Google', 'userContent/favicon.ico')
        }
    }
}

您可以参考此链接https://jenkinsci.github.io/job-dsl-plugin/#path/freeStyleJob-properties-sidebarLinks进行参考。

我已经尝试过,成功生成了侧边栏链接enter image description here

在我的侧边栏中查看Google链接


感谢您的回复。我已经更新了问题并提供了更多细节。 - 13KZ
1
@13KZ,以上解决方案有什么问题吗?根据@Dimas的代码,如果有人点击“立即构建”,则“在我的侧边栏中查看Google链接”将会使用当前执行的新链接进行更新。这不是您的要求吗? - mdabdullah

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接