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

13K*_*3KZ 7 jenkins jenkins-plugins jenkins-job-dsl jenkins-groovy jenkins-pipeline

我正在使用这个插件https://plugins.jenkins.io/sidebar-link/在 jenkins 侧栏中添加一个链接。这个插件适用于 jenkins 项目配置。现在我正在尝试添加一个管道步骤来调用这个插件。

我已经尝试了下面的代码行,但它不起作用

sidebarLinks {
            link("my_url", "the title", 'image path')
        }
Run Code Online (Sandbox Code Playgroud)

我已经阅读了关于此的主题,但没有找到可接受的回复。我认为 jenkins 插件没有很好的文档记录。

有人知道我如何将它与管道一起使用吗?

更新

我正在使用用 Groovy 编写的共享库。该库包含所有管道方法。

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

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

def call(){

    properties {
        sidebarLinks {
            link("url", 'Title', 'icon_path')
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我有以下错误:

错误:<-:java.lang.IllegalArgumentException:无法为 JobPropertyStep 实例化 {properties=org.jenkinsci.plugins.workflow.cps.CpsClosure2@6b5322b}

Joe*_*ers 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'
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,您提到您希望通过使用共享管道库在不同的作业中重用这些链接。不幸的是,共享管道库不能修改options声明性管道的部分,除了pipeline { }在单个def call().

幸运的是,Scripted Pipeline 可以覆盖配置的那部分。使用 http://JENKINS_URL/pipeline-syntax/ 上的代码段生成器,您可以生成以下代码,您可以将其放入共享管道库中,例如var/mySidebar.groovy

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

然后,您可以在脚本化管道中使用它:

library('my-sidebar')

mySidebar()

node() {
    stage('Hello') {
        sh 'echo "Hello World!"'
    }
}
Run Code Online (Sandbox Code Playgroud)

或者script声明性管道的一个块:

library('my-sidebar')

script {
    mySidebarScripted()
}

pipeline {
    agent any
    
    stages {
        stage('Hello') {
            steps {
                echo 'Hello World'
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)