How to send additional data elements in Jenkins Notification Plugin? - jenkins

I use Jenkins Pipeline Jobs and invoke build using its remote API's.
I also use Build Notification plugin to invoke my API once the build is complete for further downstream automation. As per the plugin documentation, it provides a fixed set of data elements from the build. However as part of the build, the Job has generated some data elements which I need to provide it back to my API which gets invoked by Notification plugin (As part of the JSON Payload). Can someone help me how do pass additional data elements through this plugin? or any better ways of doing it?
For example,
1. When the pipeline job is configured with notification endpoint, the jenkins config XML has the following entry
<com.tikal.hudson.plugins.notification.HudsonNotificationProperty plugin="notification#1.11">
<endpoints>
<com.tikal.hudson.plugins.notification.Endpoint>
<protocol>HTTP</protocol>
<format>JSON</format>
<url>http://localhost/api/postStatus</url>
<event>finalized</event>
<timeout>30000</timeout>
<loglines>20</loglines>
</com.tikal.hudson.plugins.notification.Endpoint>
</endpoints>
</com.tikal.hudson.plugins.notification.HudsonNotificationProperty>
A pipeline script just builds an image and the image ID has to be sent in the notification

I did not find a perfect solution in the existing Jenkins Notification Plugin. However the solution that I used it to pass the data as part of the Log Text and parse the information in the other side.

Related

Jenkins trigger job by scanning a file or rest api

I have a requirement where Jenkins job should trigger based on contents in the file or scanning pattern in the email.
Do we have plugin like that?
There is a plugin File found trigger, build will be triggered based on a file found in the given location. Anything similar for the above ask.
Please note that I don't want to run as a scheduler. We need more like event driven

How to programmatically generate config.xml from Jenkinsfile?

Jenkins has the ability to upload new jobs via its REST API. Those new jobs require an XML document which, to the best of my searching, has no schema available.
When creating jobs as part of an SCM repo, you can include a Jenkinsfile and it automatically gets translated into a job with the config.xml filled out.
I tried creating a minimal config.xml and including the Jenkinsfile content in the <script>…</script> section of the xml file. This works for trivial jobs, but does not work for jobs that have parameters: The job gets uploaded as a parameterless job. The first time you trigger a build of the job, it fails - but then the job turns into a job-with-parameters, and can properly be built.
How do I convert a Jenkinsfile, possibly with parameters or other "advanced" features, into a working config.xml file on the first try? Or, alternatively, is it possible to directly upload the Jenkinsfile to the Jenkins REST API to create the job?
Thanks in advance,
— Johnson

Storing Jenkins pipeline job metadata?

Is there a way where to store some metadata from Jenkins pipeline job, e.g:
We have a Jenkinsfile which builds a gradle project, creates docker image and pushes it to google cloud
Then a "Subjob" is launched which runs integration tests (IT) on that docker image. Subjob receives a couple of parameters (one of them - the generated docker image name)
Now sometimes that IT job fails, and I would like to re-run it from the main job view, so idealy:
we have a plugin which renders a custom button in blue ocean UI on the main job
By clicking that button a subjob is invoked again with the same parameters (plugin queries the jenkins api, get params of this job, and resubmits the subjob).
The problem ? How to get/set those parameters. I could not seem to find a mechanism for that, expect artifact storage. I could get away with that by creating a simple json/text file and uploading it as artifact, and then retrieving it in my plugin, but maybe there is a better way?
Stage restart is not coming to Scripted Pipelines so that does not look like ant option.
Maybe you can use the Jenkins API to get the details of the build?
https://your_jenkins_url.com/job/job_name/lastBuild/api/json?pretty=true
Instead of lastBuild you can also use the build number or one of lastStableBuild, lastSuccessfulBuild, lastFailedBuild, lastUnstableBuild, lastUnsuccessfulBuild, lastCompletedBuild
There is a parameters key there with all parameter names and values used in the build.
More details on https://your_jenkins_url.com/job/job_name/api/
Also, any reason you can't use the replay button in the IT job?

Trigger jenkins from JIRA-workflow

we have jenkins CICD automated pipelines working great. we also trigger it via REST API with build Parameters.
Now, we have JIRA dashboard having custom workflow created where developers moves or changes queue of issues/stories to "ReadyForDeployment" once development is completed.
Now here, once developer moves queue to "ReadyForDeployment" :-
JIRA should prompt for 3 fields (ENV, PACKAGE_NAME_APP and
PACKAGE_NAME_DB) those will be placed in jenkins URL like
blahblahblah/buildWithParameters?ENV=${ENV}& PACKAGE_NAME_APP=${PACKAGE_NAME_APP}&PACKAGE_NAME_DB=${PACKAGE_NAME_DB}
Basically upon queue change,webhook should be triggered whcih
will take input/parameters from JIRA itself.
Please let me know your thoughts on this.
It depends on your requirement whether its correct or not. But it can be possible to trigger jenkins from Jira useing Jira-weebhook and passing the parameters.
https://developer.atlassian.com/jiradev/jira-apis/webhooks

To get build status through environment variable

I am using jenkins for continous integration.
For my build purpose, i triggering email using Ant task. I am not able to find an environment variable to pass ant for sending email build status(success/failure/stable).
i want to know how can i get environment variable for build status?..if not available, what is the alternative option for build status?
Thanks in Advance
varghese
Why use ANT to send emails from Jenkins when you have two great plugins that does just that?
The default mail notification is quite good, and if you would like to have more control
I suggest using the Email-ext plugin which is very comprehensive.
If still wish to use ANT to sent your mail-notifications including the status
you will have to break your process into two steps,
where the first part runs the build and the second one runs the ANT script to report the status.
In this case, you will need to trigger the second job from the first job via the Parameterized Build plugin -
see my answer here:
trigger other configuration and send current build status with Jenkins
The build status is not set until the job has finished running, so there is no easy way to push build status to a process triggered within the build itself. You could pull build status via the API, but this would have to be an externally triggered process due to the constraint mentioned above. Any reason you aren't using the built in email support or one of the excellent email extension plugins such as this one?

Resources