如何在管道步骤中使用 Jenkins Sidebar Link 插件?

How can I use Jenkins Sidebar Link Plugin in pipeline step?

我正在开发此插件 https://plugins.jenkins.io/sidebar-link/ 以在 jenkins 侧边栏中添加一个 link。此插件适用于 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

你把它包裹在 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

我自己试过了,成功生成了sidebarlinks

see google link in my sidebar

要了解如何在声明式管道中执行某些操作,您可以使用位于 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'
            }
        }
    }
}

但是,您提到您希望通过使用共享管道库在不同的作业中重复使用这些链接。不幸的是,共享管道库 can not modify the options section of a Declarative Pipeline,除了在单个 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!"'
    }
}

或声明性管道的 script 块:

library('my-sidebar')

script {
    mySidebarScripted()
}

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

另一个可用的选项是使用 classLoader 加载插件并将新的 link 作为 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(...)
                }
            }
        }
    }
}