How to use a global pipeline library when defining an Active Choices Reactive Parameter? - jenkins

Is there a way to use a global pipeline library when defining an Active Choices Reactive Parameter in Jenkins?
I have added a global pipeline library in Jenkins, let's say PipelineLibrary, and I am able to use it successfully in my pipelines by loading it with #Library('PipelineLibrary') _. In this library I have a global function foo.bar(), which I would like to use also in the Groovy Script box when adding an Active Choices Reactive Parameter to several of my jobs.
So I would like to have something like this in the Groovy Script box of that parameter:
// Somehow take into use PipelineLibrary
return foo.bar();
What is the correct syntax to load the library here?
Or is it even possible? If not, is there some other way to share the Groovy script to several places without just copy-pasting the code in the GUI?

I think you're banging on this door - JENKINS-46394

Actually, I have found a way to do it. You just need to need to define new global variables in your Jenkinsfile and their values will be from the Shared library respectfully. Afterwards you can use the new defined global variables in your Active Choice Parameter script.

Related

Global shared variables in Jenkins Groovy pipelines

It seems like it's really difficult to be able to store a bunch of variables for use in shared code in Jenkins/Groovy scripted pipelines. I've tried a bunch of methods and none of them seem to give the desired result.
This method looked the most promising, but the values all came back as null in the calling pipeline. Get Global Variables in jenkins pipeline.
My codes is something lie
import org.blabla.JobHelper
println("env.NO_PROXY: -->${env.NO_PROXY}<--")
And in the JobHelper.groovy file, I've defined
package org.blabla.project
env.NO_PROXY = 'localhost,127.0.0.1,169.254.169.254'
the names have been changed a bit to protect the innocent, but you get the idea.
the script just prints null for the value.
Is there a simple way (or indeed any way) that I can pull in a bunch of variables from a shared library file? This feels like it should be a really simple exercise, but after spending many hours searching I'm none the wiser.
In general, env is only available once the pipeline has started, but groovy scripts are resolved much earlier.
I'm using static class members as global variables. Applied to your code sample, it would look like this:
JobHelper.groovy
package org.blabla.project
# Class must be named like the file that contains it.
class JobHelper {
static String getNO_PROXY() { 'localhost,127.0.0.1,169.254.169.254' }
}
Elsewhere:
import org.blabla.project
println("NO_PROXY: -->${JobHelper.NO_PROXY}<--")
Note that Groovy automatically generates properties from get*() and set*() methods, so we can use the short form instead of having to write JobHelper.getNO_PROXY().

How to add to existing properties in groovy script?

I have a dsl file in which properties such as log rotator and parameters are being defined for a jenkins job. However, I want to add a property to the jenkins job in a groovy script. I do this by putting
properties([pipelineTriggers([githubPush()])])
in the corresponding groovy script. However, this overwrites all other parameters and properties as defined in the dsl script.
What I have right now is putting all properties in the dsl script in the groovy script as well but that causes two different places where developers need to change the properties. Is there a way in groovy to simply add a new property instead of overwriting the old ones.
Something like
properties.add([pipelineTriggers([githubPush()])])
would be very helpful.

Jenkins - How to specify an Active Choice Reactive parameter in a declarative pipeline?

I am trying to implement a Jenkins pipeline whereby I want to control the source code for the pipeline code in git.
To declare my parameters in a Declarative pipeline, I'll have to adhere to the following syntax:-
...
pipeline {
parameters {
...
}
...
}
For the parameters section, how can I declare an Active Choice Reactive parameter, so that I can programatically populate the choices using a groovy script?
I know this is possible using the Jenkins UI using the Configure pipeline option, however I am trying to understand how I can implement this behavior in Groovy code.
Thank you
Jenkins dynamic declarative pipeline parameters
Look at solution in post #5, not the accepted. It works well always.
This is the only way for now cause Active Choices (AC) built for scripted pipelines and not straightforward support declarative pipelines.
I work a lot with AC in declarative pipelines, so my own decision is always move all the properties (parameters) wrote in scripted way before "pipeline" and even use it in Shared Libraries (as init function) or load from disc if needed and all other pipeline writing in declarative way to get all the advantages of both.
Additional benefit of that trick that I can reuse pipeline with different parameters cause loading them on the fly from Git or Shared Libraries.
Found lifehack:
To built Groovy script of different codes and AC types go to pipeline syntax constructor, select "input" > "This build is parametrized" > add there needed AC > fill all the fields and choose options as needed (same as in Job UI) > Generate code. After just copy "properties block" or part of AC code and put it in your Jenkinsfile before "pipeline".

Using active choice reactive parameter in jenkinsfile

I am using the active choice reactive parameter and I would like to access the selected value in my jenkinsfile.
Is there any way I can access select_server in my jenkinsfile? because I tried ${select_server} and this did not work.
After spending some time on this, I found a way of accessing it in jenkinsfile. the reactive choice parameter could simply be accessed by doing
${params.Select_Server}
In my example the name of the paramter is "Select_Server". Maybe this could help someone in the future

How do I write a Jenkins pipeline function in order to be able to use it as an option?

I would like to add general functionality to my Jenkins pipeline script, similar to what you can do with built-in functions (timestamps, ansiColor):
options {
timestamps()
ansiColor 'xterm'
// failureNotification() <- I want to add this
}
How do I write a function in the script so that it can be used as an option?
Currently I don't believe that's possible with the declarative syntax that you're using. You could write your own Jenkins plugin to do this, but that could get hairy.
If you're willing to use a slightly more complicated syntax, I would look at this blog post: https://jenkins.io/blog/2017/02/15/declarative-notifications/
Essentially, you'll need to create a shared groovy library and use the step from that to manage your notification step. There's a few steps to this:
Create a repository for your shared library. This should have a folder called "vars", which is where your steps and step documentation goes.
Create a step in your shared library. Using camelCase and a groovy extension, create a file name to describe your step. This is what you will call in your Jenkinsfile. Ex: sendFailureNotification.groovy
Within that file, create a function with the name of call. You can use whatever parameters you want. Ex: def call() { }
That call function is your "step logic". In your case, it sounds like you would want to look at the build result and use whatever notification steps you feel are necessary.
Copying from the documentation... 'To setup a "Global Pipeline Library," I navigated to "Manage Jenkins" → "Configure System" in the Jenkins web UI. Once there, under "Global Pipeline Libraries", I added a new library.'
Import your library into your Jenkinsfile like so: #Library('<library name you picked here>')
Now you should be able to call sendFailureNotification() at the end of your Jenkinsfile. Maybe in a post stage like so?:
post {
failure {
sendFailureNotification()
}
}

Resources