Get the job link in groovy script - jenkins

We need to update few job configurations accross all jobs and there are several instances of Jenkins running for multiple projects. We need to udpate SCM database path and the SCM password.
I am able to do the database path, but for password there is no set method. So to overcome this, I am just trying to print the link of the job for which we need to update the password. I could not find an API for job link.
Could you please help me out here, or suggest a better solution to update the password?

import hudson.model.*
jenkins = Hudson.instance
for (item in jenkins.items){
println jenkins.getRootUrl()+item.getUrl()
}
Tried this in script console. Lists all jobs absolute URLs. You just need to add the logic to get your jobs list

Related

How to get Slack notification from Jenkins on creating a new job?

I have a Jenkins instance with a bunch of jobs in it. This is shared across developers at the company. Our team would like to be notified whenever someone creates a new job on this instance.
Googling this is so difficult because I'm not looking for information on a specific job, I want an instance-wide hook that alerts Slack when a new job is created on it.
Is this possible? Maybe through a plugin? Or buried in the settings somewhere?
One possibly solution to my own problem is this: https://support.cloudbees.com/hc/en-us/articles/226941767-Groovy-to-list-all-jobs#resolution
Something like this specifically in a "execute system Groovy script" block
import jenkins.model.Jenkins
import hudson.model.*
// This script will print the name of all jobs including jobs inside of a folder, but not the folders themselves
Jenkins.instance.getAllItems(Job.class).each{
println it.name + " - " + it.class
}
Then do an alert based on those results.

How to get Script Path from each Jenkins Jobs using Groovy Console Script/Jenkins API

I have a requirement to fetch Script path from Jenkins Job .
Please find the below screenshot to understand the requirement more clearly.
I have checked in google to get some Groovy Console Script which lists details of each Job.
By using the jenkins.model.Jenkins.getJobNames() method ,i was able to get all Jobs in Jenkins , but actually the requirement is to get the Scriptpath value, mentioned inside each those jobs.
Which Jenkins Class/method can provide those details or is any other way to fetch those details using Jenkins API?
By reading the config.xml for each job and getting the Scriptpath tag from it will also give me the information needed, But I was looking for a REST method or Jenkins Groovy Script method which can give me this Script path Information for each Job.
Open your job configuration page.
Change the configure at the end of url to config.xml and press Enter
The new opening page loads xml content
Find Scriptpath value in tag <scriptPath>xxxxxx</scriptPath>
So you can send http requset to job's confim.xml url and parse the content to get the value programmatically

In Jenkins, can I find a job by its display name?

I have a pipeline project with a stage in which a unique identifier is retrieved from an external system and set as the job's display name. I know this identifier to be unique for my whole Jenkins installation, so any search by this key should return exactly zero or one job.
Is there any way I can get a job number/URL (or a list of jobs containing only this one job) given its display name and the project name?
EDIT: I want to find jobs from outside of Jenkins, via user interface or REST API, not from a pipeline.
Take look at Jenkins instance. You can retrieve all Jobs, Folders, ... using the getItem(string)/getItems() methods.
Hardly ideal, but I ended up using the Script Console to list all the jobs, and then used my browsers text search.
Navigate to http://your-jenkins-instance.org.com/script (or Jenkins → Manage Jenkins → Script Console) and run:
Jenkins.instance.getAllItems(AbstractItem.class).each {
println(it.fullName)
};

hudson: way to get the user value who initiated the build?

Is there a way to get the hudson job initiated user name.
Is it possible to get using script shell, py etc.
Lets assume I have the build # which was initiated. I know how to get the latest build info using api but would like to get a user details for a specific job.
Do you think, this will work for hudson? :)
https://wiki.jenkins-ci.org/display/JENKINS/Build+User+Vars+Plugin
Thanks in advance
That plugin will not work with Hudson, unless you download a very old version of the plugin. I'm not sure how many people are still using Hudson and haven't upgraded to Jenkins.
Anyway, when a user manually triggers a build, this is called a "user cause"; there are other types of cause, e.g. SCM trigger.
You can use the JSON or XML API to get the causes for a build, for example:
https://ci.jenkins-ci.org/job/remoting/lastSuccessfulBuild/api/xml?xpath=//action/cause/userId
In this case, this returns the username that caused the build to run.
Though note that there may be multiple causes for a build, and potentially other cause types that use the userId field.
This works in Jenkins, but it should also work in Hudson, but I haven't tested it.

Taking out common config of jenkins jobs

I have about 200 jenkins, each of them has a long config page but actually most config are the same. Everytime when I need to update something in the common config, I write a groovy script to loop though those jobs and update them one by one. It's a pain because it takes about 5 minutes to update those jobs by the groovy script. I am wondering is there a jenkins plugin(or something else) that I can use to put the common config in one place? jenkins slicing plugin doesn't work well, I think it conflicts with another plugin.
Thanks
Sounds like a job for the job-dsl plugin
From the wiki page
The Jenkins job-dsl-plugin attempts to solve this problem by allowing
jobs to be defined with the absolute minimum necessary in a
programmatic form, with the help of templates that are synced with the
generated jobs. The goal is for your project to be able to define all
the jobs they want to be related to their project, declaring their
intent for the jobs, leaving the common stuff up to a template that
were defined earlier or hidden behind the DSL.

Resources