7

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')
        }

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?

Updated

I'm using a shared library written in Groovy. This library holds all pipeline methods.

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

Shared library side, I've an addLink.groovy file.

def call(){

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

I've got error below :

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

13KZ
  • 1,295
  • 5
  • 24
  • 43
  • Do you have any error log? – JRichardsz Jul 05 '20 at 16:24
  • No error log in jenkins console. Despite no link in jenkins sidebar. – 13KZ Jul 05 '20 at 22:41
  • @13KZ Are you saying, the link is showing up. in left menu, and when you click on it, it navigates to the intended page? If all you want to do is to programatically traverse to a particular URL why do you want to click on it, cant you just use `groovy` script to navigate to the URL? Are you trying to display a link on the left menu during runtime? – mdabdullah Jul 20 '20 at 19:32
  • @mdabdullah I'm trying to add sidelink programmatically during a jenkins build (step) using sidebar link plugin. – 13KZ Jul 21 '20 at 18:04

3 Answers3

6

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'
            }
        }
    }
}

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().

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!"'
    }
}

Or a script block of a Declarative Pipeline:

library('my-sidebar')

script {
    mySidebarScripted()
}

pipeline {
    agent any
    
    stages {
        stage('Hello') {
            steps {
                echo 'Hello World'
            }
        }
    }
}
Joep Weijers
  • 1,844
  • 12
  • 19
1

Another available option is to use the classLoader to load the plugin and add a new link as an Action to the build or project level.
In your shared library file you can have something like this:

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)
}

Than call it from a scripted pipeline or a script block in a declarative pipeline as follows:

pipeline {
    agent any

    stages {
        stage('Hello') {
            steps {
                script {
                    // assuming functions resides inside utils.groovy
                    utils.addSideBarLink(...)
                }
            }
        }
    }
}
Noam Helmer
  • 5,310
  • 1
  • 9
  • 29
0

Have you wrap it in properties ?

job("Sidebar Link Job 2") {
    description()
    keepDependencies(false)
    disabled(false)
    concurrentBuild(false)
    properties {
        sidebarLinks {
            link('https://google.com/', 'Google', 'userContent/favicon.ico')
        }
    }
}

For references you can look at this https://jenkinsci.github.io/job-dsl-plugin/#path/freeStyleJob-properties-sidebarLinks

I've tried it myself and it is successfully generated the sidebarlinks enter image description here

see google link in my sidebar

Dimas Rizky
  • 354
  • 1
  • 3
  • 13
  • Thanks for your response. I updated the question with more details. – 13KZ Jul 10 '20 at 08:30
  • 1
    @13KZ what is the problem with the above solution? As per the @Dimas code it looks like if someone hits `Build Now` , the `see google link in my sidebar` will get updated with a fresh link from the current execution. Is this not your requirement? – mdabdullah Jul 21 '20 at 18:35