How to authenticate xray from jenkins pipeline script - jenkins

For Xray on Jira server/datacenter ... Need authentication token to push the results from jenkins pipeline ... How to generate the token or if there is any other way to authenticate and push the test results

In Jira server/datacenter, usually the authentication is done using a Jira's user credentials (i.e., username and password). It's also possible to use Personal Access Tokens (in Jira datacenter only), but this is not that common; besides, these Personal Access tokens are totally unrelated with the authentication tokens used in Xray cloud in case you've seen that.
So, and first of all, you need to install the Xray Jenkins connector/plugin.
Then you need to configure a Xray instance, where you specify the server URL and the credentials (i.e., Jira username + password).
Then you can either use the UI to setup your build , on a freestyle project for example, and use the task "Xray: Results Import Task" as a build step.
If you're using declarative pipelines, on a pipeline type of project, then you can generate the pipeline syntax as follows:
Access Jenkins' Snippet Generator to faciliate the process of generating the pipeline script code.
You may access it, from the pipeline syntax link available from within your project.
Then select "step: General build step", "Xray: Results Import Task" and fill out the values you want.. it will then allow you to generate the script that you can include in your pipeline.
If you don't want to use the Xray Jenkins connector/plugin, you could also invoke the Xray REST API directly and perform a HTTP POST request to submit the test report, using the Jira user credentials and basic authentication. This could be achieved by invoking, for example, "curl" utility as a build step.

Related

XRAY integration with jenkins

Need step to integrate XRAY with jenkins , as per the official documentation we need to give JIRA url ,select cloud or server/data center and select the credentials of JIRA account , then the configuration id will be get generated ... So we need to use that config id in pipeline
Is there any other way just to add username/password of jira in jenkins credentials and connect with XRAY from jenkins
If you are using the Xray Jenkins plugin you must provide the mandatory information to communicate with Xray as described here: https://docs.getxray.app/display/XRAYCLOUD/Integration+with+Jenkins.
You also have the possibility to use the Xray API and control the parameters you need to ask for, if you pre-configure some parameters on the code and only ask for login and password. To connect to Xray you will need some mandatory parameters that are described here: https://docs.getxray.app/display/XRAYCLOUD/Version+1, how many of those you can have pre-defined is up to your usage.
First, please check if you are using Xray on Jira cloud or Jira server/datacenter.
To submit results from Jenkins you have 3 options:
use the Xray Jenkins plugin UI itself, and configure a project to submit the test results; this is pretty straighforward
use the Xray Jenkins plugin but from the pipeline; for that you need to generate the pipeline script
use either the Jenkins UI or the pipeline to invoke the API by hand; for this you don't need the Xray Jenkins plugin, you just need some tool such as "curl" to perform the HTTP requests (see example for GitLab and Xray cloud here, and for Xray server/DC here)
Note that for Xray in Jira cloud, you need to use client_id and client_secret to perform an initial authentication request (in case you're doing the requests by hand). These are obtained from an API key defined on Xray global settings.
For Xray on Jira server/datacenter, you may use Jira credentials (e.g. Jira username + password). It only requires you to perform one request and submit the test results.

How to enable remote trigger in jenkins file for a pipeline job?

How to enable the "Trigger builds remotely (e.g., from scripts)" section in a Jenkins file for a pipeline job. Want to achieve the same task of freestyle project in pipeline.
Jenkins Pipeline job also provides the same option for remote build trigger. There is no need for additional plugins or workarounds.
Jenkins jobs can also be triggered by user level authentication tokens. To enable user level authentication token please follow below steps.
Log in to Jenkins.
Click you name (Upper-right corner).
Click Configure (Left-side menu).
Use "Add new Token" button to generate a new one then name it.
Note: You must copy the token when you generate it, You cannot view the token afterwards.
Revoke old tokens when no longer needed.

How can a Jenkins input step be completed via APIs?

I have a Jenkins pipeline defined that includes an input step. A human can provide the input by clicking in the Jenkins UI and there is an HTTP endpoint to provide input as well.
Is it possible to provide the input via Groovy API calls? For instance, could a parallel step in the same pipeline provide the input values? Or, could a completely different build provide input values via Groovy code?
The reason I'd like to use Groovy is to keep the input providing entirely in the Jenkins system and avoid having to provide authentication credentials for the HTTP endpoint.
We had a similar problem (one pipeline should be able to trigger input steps in other pipelines).
This worked in the Jenkins script console and should work in a pipeline:
import org.jenkinsci.plugins.workflow.support.steps.input.*
def build = Jenkins.instance.getItemByFullName("TestInputPipeline").getLastBuild()
def action = build.getAction(InputAction.class)
action.getExecutions().get(0).proceed([])
TestInputPipeline is the name of a test pipeline with a single input.
If your input has parameters, you will probably be able to provide them with the map in the proceed call.
This Input Step Plugin test code helped us: https://github.com/jenkinsci/pipeline-input-step-plugin/blob/master/src/test/java/org/jenkinsci/plugins/workflow/support/steps/input/InputStepRestartTest.java
JavaDoc can be found here: https://javadoc.jenkins.io/plugin/pipeline-input-step/index.html

Publish the Screenshot/PDF of SonarQube Analysis Result from Jenkins to Confluence

I have a Jenkins job that will invoke SonarQube analysis on code pulled from a Bitbucket repository whenever there is any changes on the Bitbucket repository. At the moment, I was able to use the Confluence Publisher plugin to publish the URL of the build job to Confluence as a comment.
I was wondering if there is any way I could use Jenkins to screenshot the SonarQube analysis report (like in SonarQube web UI) and publish it directly to Confluence? (So that the user does not need to use the URL to go to the specific Jenkins build job, and then get the SonarQube analysis report URL, and navigate to the page to view the report)
Thank you.
Instead of using a screenshot, you can send a curl request from Jenkins to SonarQube REST API (5.3 and above) to get the project status from the quality gate after the code has been uploaded and analysed. The endpoint you'd likely want to use is: /api/qualitygates/project_status?prjectKey=<projectKey>&branch=<branch>
According to the web API docs:
Get the quality gate status of a project or a Compute Engine task.
Either 'analysisId', 'projectId' or 'projectKey' must be provided
The different statuses returned are: OK, WARN, ERROR, NONE. The NONE status is returned when there is no quality gate associated with the analysis.
Returns an HTTP code 404 if the analysis associated with the task is not found or does not exist.
Requires one of the following permissions:
'Administer System'
'Administer' rights on the specified project
'Browse' on the specified project
Once you have these results available, you can create a Jenkins user in Confluence and have Jenkins send a POST request to create a page or a comment with the results obtained.

Deploying jenkins jobs for dynamically created gitlab repos

Setup:
1 Jenkins server and 1 gitlab server that I own
Expected usage:
- We provision repositories for users on gitlab using some scripts when a user signs up with our portal.
- As soon as these users submit some code in the repository, we have to run some junit tests on them and email the users with a the test results in a clean way where they can see the passed and failed results with appropriate links which they can click on to get details of the tests.
Requirements for. jenkins:
1. Ability to send html junit test results with clickable test result links
2. Based on whether the junit test fails or passes, call two different python scripts.
Questions:
1. What is the best way to go about 1 and 2 requirements
2. Should I create a job template and create a job per dynamically created repo or can I use same job to run tests on multiple repos
References :
These were the links we referred.But as a newbie to jenkins couldn't come up with clear solutions.
1.https://wiki.jenkins.io/display/JENKINS/Email-ext+plugin#Email-extplugin-Jellycontent
2.https://gist.github.com/nightspotlight/7cfa6af7c7989857f336f742cdcb443b
3.https://learn2automate.blog/2017/05/19/how-to-set-jenkins-job-status-based-on-test-case-pass/
Send some links in your mail using jenkins is simple
Install https://wiki.jenkins.io/display/JENKINS/Email-ext+plugin
Install https://wiki.jenkins.io/display/JENKINS/Build+With+Parameters+Plugin in order to expose a job as link with params
Create some job in jenkins and in the post-build section add editable email notification
[![enter image description here][1]][1]
in In Default Content of editable email notification you must write the email body that will be sent to your users. If your requirement is send a link to allow user to open another jenkins page, you could write the following :
In order to execute python scripts click in this link
http://your-jenkins.com/job/my-job-python/parambuild/?param1=test1&param2=etc
Your mail provider will render this text as html.
Finally you need to create some job with name my-job-python with parameters param1, param2 or whatever. This job will execute your python scripts when user clicks on email links sent by jenkins.

Resources