Jenkins execute shell how to get the user who run the job - jenkins

I want sned a message to the executer,when the job tigger and complete. But the first question is how to get the user who run the job.
I look at the list of available environment variables. But I can't find the variables than I wanted.

I think this plugin can help you: Build User Vars Plugin
Take a look to the usage example. This will tell you how the configuration works.

Related

How to interactively pass values between Ansible and Jenkins while the Job is Running

My Jenkins UI takes parameter a list of files/folders along with wildcards from users that they wish to delete.
Jenkins then passes the files to be deleted across all hosts by invoking ansible-playbook.
My requirement is to prompt user on Jenkins for confirmation before deleting a file / folder.
Thus, how rm -rfi /tmp/moht /var/log*/data.dat prompts interactively asking the user confirmation before deleting the files; is what I Wish to prompt on Jenkins for each Host.
Thus, for the above I expect Jenkins to prompt like below:
Are you sure you want to delete
/tmp/moht
/var/log14Mar/data.dat
I'm aware of input function in Jenkins for prompt.
I'm also aware of an ansible command module that can be used to fire rm -rfi command.
I'm also aware of how to timeout or terminate the Jenkins Job upon user input. However, in this case I would love if the user input Yes / No could be sent back to the target host via ansible and action gets performed accordingly.
I understand that this may be too much an ask but other feasible solutions or suggestions are also appreciated.
Can you please suggest how can I achieve the requirement?
I strongly feel that using Jenkins as interactive tool is a bad idea. But if you really like to implement bad ideas with unsuitable tools, you can try to use matrix job [1] based on files you want to remove.
Jenkins will create a list of jobs with a job for each file you'd like to remove. You ask confirmation and then execute an Ansible playbook to remove a file.
[1] https://plugins.jenkins.io/matrix-project/
It's horribly inefficient and you should not do it like this.

Is there a way to use parameters in Jenkins job description field?

I have a job creator job that creates other jobs in jenkins and I would like to use the created jobs name in the description.
For example for name "JobXX" i would like to have:
"This is documentation for JobXX etc.."
In "Execute shell" build step I can refer to the jobs name via $JOB_NAME parameter but that does not seem to work in the description field.
Any ideas on how to do this? Or if it's even possible?
Never used it, but take a look to the Project Description Setter Plugin:
https://wiki.jenkins.io/display/JENKINS/Project+Description+Setter+Plugin
In this blog it explains better how to use it:
http://www.tothenew.com/blog/setting-dynamic-project-description-and-build-description-in-jenkins/
Among the different variables you can use there is {PROJECT_NAME} which fits your needs.
Besides this, this plugin allows you also to automatically set descriptions to each specific build.

Modify notifications on a running build in jenkins

I occasionally want to get notified when a particular jenkins job that is building finishes. Is there any way to do this?
Scripting it through the API would be fine. I already have the jenkins IRC bot that notifies me of many things, so if I could just dynamically modify the running job build, that would be enough to do what I want -- I'm just having a hard time finding how to accomplish that.
AFAIK, you cannot change a job's config while it's running.
Here is an idea: Use a post-build step to check for an external resource status (like a file containing an action by text) and running an action based on the content of the file.
The external file can be modified while the build is running, so when the post-build is executed, it will follow the logic defined based on the content of the file.
I hope this helps.
You can use email notifier, It will send you an email
https://wiki.jenkins-ci.org/display/JENKINS/Email-ext+plugin

Jenkins plugin code that should excute before any kind of job is run in Jenkins

I am new to Jenkins plugin development. M trying to write a plugin that should be executed before any Multi configuration type job runs in Jenkins.
In this plugin I want to write rules that will check what configuration parameters user has selected while submitting the job, based on selected parameters, I want to decide whether to allow the job to run or to restrict it.
User should be shown reason as to why that job cannot be run in the Console Output.
Does anyone have any ideas which class I need to extend or which interface I need to implement in order to get a hook into Jenkins job run?
You could look at the Matrix Execution Strategy which allows for a groovy script to select which matrix combinations to run. I would think if your script threw an exception it would stop the build.
For background, the multi configuration projects run a control job (or flyweight) which runs the SCM phase then starts all the actual combinations. This plugin runs after the flyweight SCM checkout.
If nothing else, this will give you a working plugin to start from
Disclaimer: I wrote this plugin
Blocked queue job plugin was what I needed
Out of the box that plugin supports two ways to block the jobs -
Based on the result of last run of another project.
Based on result of last run of the current project
In that plugin the BlockQueueItemTaskDispatcher.java extends Jenkin's QueueTaskDispatcher providing us a hook into Jenkins logic to allow or block the jobs present in the queue from running.
I used this plugin as a starting point for developing a new plugin that allows us to restrict project based on parameters selected and the current time. Ultimate goal is to restrict production migrations from running during the day.
Overriding the isBlocked() method of QueueTaskDispatcher gave access to hudson.model.Queue.Item instance as an argument to me. Then I used the Item instance's getParams method to get access to build parameters selected by the user at runtime. Parsed the lifecyle value from it. Checked the current time. If lifecycle was Production and current time was day time then restricted the job by returning non null CauseOfBlockage from isBlocked() method. If that condition was false, then returnedCauseOfBlockage as null allowing the queued job to run.

Get result of a build step in Hudson/Jenkins to re-use it in another one

My question may be silly but I've been trying several ways and I still can't do what I want, i.e.:
use the scp target of Ant to target a remote machine and execute
a script there
this script creates a dynamic list of files
get this list of files (only their names) back in Hudson to use it in the next build step (another scp from Ant)
I tried to use environment variables but they are interpreted by Hudson so I'm stuck here...
Globally my question would be: how to get a result from an Ant build step ?
Thanks for your ideas,
Emmanuel
You may find File parameter useful. This allows you to create an input file, pass it to build. You may need to write script/ant script to process the file though.
In the long term you may evaluate a Hudson farm. This will allow to create tasks that span multiple machines , pass results around. (https://wiki.jenkins-ci.org/display/JENKINS/Plugins)
You can get the ID(s) of the job that triggered your job via the API and fetch their status.

Resources