Jenkins fetch plugin configuration with groovy - jenkins

For a custom Jenkins plugin for which there are parameters configured under 'Configure System' on a global level.
I would like to fetch those values and dynamically use that information in a job.
I get the list of all plugins by using:
def plugins = new ArrayList(jenkins.model.Jenkins.instance.getPluginManager().getPlugins())
But, how do I fetch the configuration values for the required plugin, say Plugin:xyz
Is there a way to get all the properties?

Related

Jenkins : Change the name of JenkinsFile

I'm using Pipeline Plugin under Jenkins
My job is basically using a file called "jenkinsFile" to get the divers steps to run.
-> My purpose is how to let the job use a different file name :
examples:
myJenkinsFile
build_JenkinsFile
deploy_JenkinsFile
buildSteps
...
Since it seems that "JenkinsFile" is a conventional format ,
is there any ways to change it if it's not verry clean ??
Suggestions ??
On the project section of the configuration page you just have to click Add > Pipeline Jenkins and then you can choose the custom name that jenkins will look for the pipeline.
If you want also a better level of customization you can also use Remote File Plugin, which allows you to put your pipeline in a repository and make it work with multiple repositories/branch (and of course you can still customize the name of the file)

Key Value store option for Jenkins

Is there any plugin available for Jenkins which provides a key-value store option for Jenkins?
The plugin which's functionality is close to that is the credentials plugin.
The goal is to have a plugin which stores global configuration parameters and this parameters are available to Jenkins jobs.
Go to Manage jenkins -> Configure System -> Global Properties -> Environment Variables:
Check the box and Click on ADD
Enter Key-value and Save
To access the variable simply ${<Your-key>}
Could the enthronement variables fit your need?
They are like regular shell variable.
If you are using the pipeline you can define it this way:
environment {
VAR = 'your_value'
}
and use it later in your build.
This is explained there: https://jenkins.io/doc/pipeline/tour/environment/
If you are writing your pipeline from the UI, you can add a 'source' step in your build step.
source your_environnement_setting
test='Hello'
And then the variables can simply be used like any shell var:
echo $test
If you have variables that you do not know in advance, but you know when you are triggering your job, you can also use the parametrized plugin:
https://wiki.jenkins.io/display/JENKINS/Parameterized+Build

Create Jenkins WorkflowMultibranchProject job with groovy init

I am automating the configuration of Jenkins masters to get to a one-click instantiation. We have 6 standard jobs we create for each instance and I'd like to be able to create them via groovy.init.d scripts but haven't found examples for this type of job.
We use the cloudbees Bitbucket Team/Project plugin that ends up creating jobs of type WorkflowMultibranchProject with additional configuration to connect to our on-prem Bitbucket instance.
Does anyone have samples of creating such a job via groovy? Am I better off trying to use JobDSL to create the job (am doing that already for a Mother Seed job)
[UPDATE] : with the help of the answer below came up with a full sample creating an entire Bitbucket Team/Project Job: https://github.com/redfive/jenkins-init/blob/master/init.groovy.d/core-jobs.groovy
Having used Job DSL, I'm 50/50 undecided if it is easier compared to using Groovy (as Job DSL lacks support for some of the config options).
An example for the similar OrganizationFolder can be found in #coderanger's article on https://coderanger.net/jenkins/:
// Create the top-level item if it doesn't exist already.
def folder = jenkins.items.isEmpty() ? jenkins.createProject(OrganizationFolder, 'MyName') : jenkins.items[0]
// Set up GitHub source.
def navigator = new GitHubSCMNavigator(githubOrg)
navigator.credentialsId = cred.id // Loaded above in the GitHub section.
navigator.traits = [
// Too many repos to scan everything. This trims to a svelte 265 repos at the time of writing.
new jenkins.scm.impl.trait.WildcardSCMSourceFilterTrait('*-cookbook', ''),
// We have a ton of old branches so try to limit to just master and PRs for now.
new jenkins.scm.impl.trait.RegexSCMHeadFilterTrait('^(master|PR-.*)'),
new BranchDiscoveryTrait(1), // Exclude branches that are also filed as PRs.
new OriginPullRequestDiscoveryTrait(1), // Merging the pull request with the current target branch revision.
]
folder.navigators.replace(navigator)
The next time when I set up an instance, I'd likely give that a try.

Jenkins Job DSL Plugin: How to Modify Parameters on other jobs

I want to create a job in Jenkins which modifies an existing parameter on another job.
I'm using the Job DSL Plugin. The code I'm using is:
job('jobname'){
using('jobname')
parameters {
choiceParam('PARAMETER1',['newValue1', 'newValue2'],'')
}
}
However, this only adds another parameter with the same name in the other job.
I'm trying the alternative to delete all parameters and start from scratch, but I haven't found the way to do that using Job DSL (not even with the Configure block).
Another alternative would be to define the other job completely and start from scratch, but that would make the job too complicated, specially if I want to apply this change to many jobs at a time.
¿Is there a way to edit or delete lines on the config.xml file using the Job DSL plugin?

Jenkins - How do I pass Email-ext plugin's "Culprits" email list variable to a build step?

Culprits is the list of users who committed a change since the last non-broken build till now. The Jenkins Email-ext plugin is able to send an email to the culprits during a Post-Build action.
I want to use the list of emails defined by Culprits in a python script build step inside of my Jenkins job.
Can anyone suggest how I can do this?
The 'culprits' list comes from the SCM plugin in jenkins and includes all users who have committed since the last successful build. Ultimately the email-ext plugin is sourcing its list from scm and generating the email addresses based on the following heuristic
The plugin will generate an email address based on the committer's id and an appended "default email suffix" from Jenkins's global configuration page. For instance, if a change was committed by someone with an id "first.last", and the default email suffix is "#somewhere.com", then an email will be sent to "first.last#somewhere.com"
If your email addresses have some sort of pattern (and they must do, otherwise the email-ext plugin would not be generating the correct addresses) then you can generate them yourself inside a groovy script eg:
import hudson.model.*
def culprits = build.getCulprits()
def list = culprits.collect{it.getFullName().toLowerCase().replace(" ", ".") + "#mydomain.com"}
This example would convert a culprit like "Adam Smith" to adam.smith#mydomain.com
But you could replace the call to getFullName() with a call to getId() and manipulate that however appropriate. eg:
def list = culprits.collect{it.getId().toLowerCase() + "#mydomain.com"}
Which is the basic format that email-ext uses - You can get a full list of user properties from the documentation.
Now you have the list in a groovy script, but how to make that list available to your python script? That will come down to what you are used to doing. You could write the list to your workspace and read it from python, or save the result to an environmental variable, or even save it to a build parameter.

Resources