问题描述
我正在开发这个插件 https://plugins.jenkins.io/sidebar-link/ 在 jenkins 侧边栏中添加链接.该插件适用于 jenkins 项目配置.现在我正在尝试添加一个管道步骤来调用这个插件.
I'm working on this plugin https://plugins.jenkins.io/sidebar-link/ to add a link in jenkins side bar. This plugin works with jenkins project configuration. Now I'm trying to add a pipeline step to call this plugin.
我已经尝试了下面的代码行,但它不起作用
I already try lines of code below but it's not working
sidebarLinks {
link("my_url", "the title", 'image path')
}
我已经阅读了这方面的主题,但没有找到接受的回复.我认为 jenkins 插件没有很好的文档记录.
I already read thread on this but no accepted responses found.I think that jenkins plugin are not well documented.
有人知道我如何将它与管道一起使用吗?
Does anybody know how can I use it with pipeline?
更新
我正在使用一个用 Groovy 编写的共享库.该库包含所有管道方法.
I'm using a shared library written in Groovy. This library holds all pipeline methods.
@Library('[email protected]') _
pipeline {
stages {
...
stage('Add side link') {
steps {
addLink()
}
}
}
}
共享库方面,我有一个 addLink.groovy 文件.
Shared library side, I've an addLink.groovy file.
def call(){
properties {
sidebarLinks {
link("url", 'Title', 'icon_path')
}
}
}
下面有错误:
错误:<-:java.lang.IllegalArgumentException:无法实例化{properties=org.jenkinsci.plugins.workflow.cps.CpsClosure2@6b5322b}对于 JobPropertyStep
推荐答案
要了解如何在声明式管道中执行某些操作,您可以使用位于 http://JENKINS_URL/directive-generator/的指令生成器.这提供了一个类似于作业配置的用户界面.然而,在选择选项"时,->添加"->侧边栏链接"->填写字段->生成",由于内部服务器错误,不会生成任何内容.
To find out how to do something in Declarative Pipeline, you can use the Directive Generator at http://JENKINS_URL/directive-generator/. This provides a user interface similar to the Job Configuration. However upon selecting "options" -> "Add" -> "sidebarLinks" -> fill in fields -> "Generate", nothing will be generated due to an internal server error.
以下声明性管道语法适用于单个作业:
The following Declarative Pipeline syntax works for a single job:
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 { }
.
However, you mentioned that you want to reuse these links in different jobs through use of a Shared Pipeline Library. Unfortunately, Shared Pipeline Libraries can not modify the options
section of a Declarative Pipeline, with the exception of generating your entire pipeline { }
in a single def call()
.
幸运的是,脚本化管道可以覆盖配置的那部分.使用 http://JENKINS_URL/pipeline-syntax/上的代码段生成器,您可以生成以下代码,您可以将其放入共享管道库中,例如在 var/mySidebar.groovy
:
Fortunately, Scripted Pipeline can override that part of a configuration. Using the Snippet Generator at http://JENKINS_URL/pipeline-syntax/, you can generate the following bit of code that you can put in a Shared Pipeline Library, e.g. in var/mySidebar.groovy
:
def call() {
properties([
sidebarLinks([[
displayName: 'My fancy sidebar url', iconFileName: '', urlName: 'https://example.com'
]])
])
}
然后您可以在任一脚本化管道中使用它:
You can then use that in either a scripted pipeline:
library('my-sidebar')
mySidebar()
node() {
stage('Hello') {
sh 'echo "Hello World!"'
}
}
或声明性管道的 script
块:
Or a script
block of a Declarative Pipeline:
library('my-sidebar')
script {
mySidebarScripted()
}
pipeline {
agent any
stages {
stage('Hello') {
steps {
echo 'Hello World'
}
}
}
}
这篇关于如何在管道步骤中使用 Jenkins 侧边栏链接插件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!