Jira post action script to get custom values - jenkins

I have a Jira issue that has different custom fields. I want to trigger a parameterized Jenkins job, after the Jira issue is filled up and moved from Open to In Progress, and the Jenkins job parameters will be each of the field values the Jira issue contains.
I was trying to do this with two approaches:
- Jira Trigger plugin, but I can't find the way to get the Jira issue custom fields values and neither to use them as parameters to trigger Jenkins job.
- Using the post action script on the Jira issue transition, but I'm struggling to know how to get the Jira custom values on the script, to then be used to POST them to Jenkins job

You can use Script Runner plugin for this requirement .
https://marketplace.atlassian.com/apps/6820/scriptrunner-for-jira
This plugin can be used as a listener/post (groovy code) to the desired state transition which will capture the required field values and feed them to Jenkins through API call .

Related

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

Jenkins to trigger another build/project dynamically

Is there a way in Jenkins to trigger another build/project dynamically based on a parameter value in parent project?
The reason for this is that I have 100's of projects and I don't want to use pipeline/conditional post build plugins to link all jobs.
I know of a way using REST API but trying to find is there a way to trigger a single project from those 100?
Easy with Pipeline Plugin. Just define your parameter (say MY_DOWNSTREAM_JOB) and use it inside the groovy script definition:
build MY_DOWNSTREAM_JOB

How to trigger a Jenkins job from a status change in Jira

I have been looking for a while now for a way to trigger a Jenkins job from the status or a ticket/story in Jira changing status. To give a more detailed example when my team moves a ticket to the 'ready for test' column we would like to be able to trigger a sanity test pack in Jenkins, the ideal situation would then be that we are able to post the results (generated as a html) as a comment on the ticket within Jira. Failing that we would like to be able to publish the results as simply pass/fail.
I have recently been looking at the Jenkins Jira plugin but this does not seem to have the functionality to work both ways, in other words it can post results after a job has run but you cannot trigger the job from a change of status in Jira. Is there any such plugin available or is it something that we will need to create ourselves?
Cheers in advance
So, basicly there are webhooks in jira (https://developer.atlassian.com/jiradev/jira-architecture/webhooks). With it you can configure it to trigger specific url on issue status change. The specific url should be jenkins API, for example for triggering a build you should call an external url like (if you are building with parameters): http://server/job/myjob/buildWithParameters?token=TOKEN&PARAMETER=Value
Some more info https://wiki.jenkins-ci.org/display/JENKINS/Remote+access+API
I have written a Jenkins plugin that supports JIRA status change trigger: jira-trigger-plugin.
This plugin also injects an environment variable of JIRA_ISSUE_KEY, so you can utilise this information to publish your result back to JIRA e.g. using REST API.
Please follow as mentioned below:
GIT:
You can set the web hooks for git/ bitbucket /stash on commit which inturn should change the state of task in JIRA.
JIRA:
Once you in JIRA. define the workflow for your task.
In this particular workflow you can set a post-function where the web hook should be configured. In this configuration of webhook in events define JQL as below:
status CHANGED FROM "To Do" TO "ready to test".
In the same mention the job that needs to be fired in the URL section.
You can look through webhooks in Jira :
https://support.atlassian.com/jira-cloud-administration/docs/manage-webhooks/
You also need to add the Generic Webhook Trigger plugin to your Jenkins :
https://plugins.jenkins.io/generic-webhook-trigger/
You can find here an example on how to use the Generic Webhook Trigger plugin with Bitbucket Github and Gitlab. https://github.com/jenkinsci/generic-webhook-trigger-plugin/tree/master/src/test/resources/org/jenkinsci/plugins/gwt/bdd
It involved multiple steps
If you are using Git/Stash/BitBucket (which i implemented),
You can simply
configure the commit hooks in your Stash/Bitbucket
Attach events to your JIRA workflow
Jenkins JOB - post build events - configure Notify
Stash plugin Set the Poll SCM
That's it

Jenkins: How can I populate parameters from database to parameterized job?

I need to pull values from database and show parameters as dropdown in Jenkins parameterized job. Is there any available plugin to achieve this?
Help would be appreciated :)
We have to use Jenkins Dynamic Parameter Plug-in to pull values from database and show parameters as dropdown in Jenkins parameterized job.
LINK:
https://wiki.jenkins.io/display/JENKINS/Dynamic+Parameter+Plug-in
We have to write groovy script to pull values from database and click on above link for more details.

Resources