Jenkins API - buildWithParameters and fileParameter - jenkins

so we're trying to use the Jenkins API to invoke a build with a file parameter. Basically we're trying to give it the files to build in ZIP format. We have a ZIP plugin installed to unzip the build file, but we can't get that far.
Basically we're trying to use the buildWithParameters endpoint, but based on scouring info available online combined with a test I have set up in Postman, it would seem that buildWithParameters only works with parameters that exist on the query string.
If we use the build endpoint, we're able to submit the file successfully, both in a node app we built (using the request library) and in Postman. But if we roll back to buildWithParameters, using the same exact configuration, the file is not processed successfully by Jenkins (we get a file not found error).
Am I right about buildWithParameters only working with query string parameters?
To preempt why not just use the build endpoint: we need the build number back, which we get back from buildWithParameters but don't seem to get back with build.
To reiterate my question: is it possible to use the buildWithParameters endpoint and upload a file parameter? It would seem like it is, but we have not been able to get it to work.

Related

Set Jenkins build cause manually via REST API

I'm looking to update the default build cause when invoking a build via the REST API. I see that this is possible by appending the cause parameter when using the Trigger builds remotely (e.g., from scripts) option, but what about when I make a /build POST request using the normal user:apiToken format?
Essentially I'm looking to replace Started by user xxxx with a custom string. I'll also accept plugin recommendations as a fallback if this isn't possible by default.

Get Sonarqube Analysis Status on a variable (GUI Job)

I couldn't find any solutions to this particular need.
Basically I have a GUI Job and I need the status of the Sonarqube Analysis so I can later send a POST Request with it.
(I'm aware that pipeline exists and works great but because a specific reason I need it to be GUI)
On the pipeline you have the WaitForQualityGate.status(), I've tried using this but no success.
Example of what is desired
Any insights? Thanks in advance
You can use the SonarQube Rest API to get the status.
Whenever you run SonarQube analysis through Jenkins Pipeline, upon the successful analysis you will see report-task.txt created in the workspace folder.
Note: The location of report-task.txt file depends on the tool that was used to generate it. The mvn sonar:sonar task defaults to path target/sonar. In my case, I used sonarscanner to analyse a nodejs project. So the location of report-tast.txt is .scannerwork.
Now, you will get the ceTaskUrl and ceTaskId in report-task.txt. You can use that ceTaskUrl to get the analysisId.
Then, you can use the below api to get the quality gate status using analysisId.
http://<sonarqube_host>/api/qualitygates/project_status?analysisId=$ANALYSIS_ID"
Now, try to get the curl output of the above API into a variable.
If you mean to say that you want a custom variable message to pop up in your Jenkins GUI based on the SonarQube scan status, then that would require you to:
Clone the original Jenkins source code
Add a custom HTML button/div/graphic
Compile the Jenkins code
Build the new code
Execute the generated JAR
Else, you can try some plugins available on Jenkins that would give you the ability to render conditional outputs. No promises on whether they can actually help you change the original GUI.
Any alternative traditional approach wouldn't be able to fulfill your GUI requirement.

Jenkins JSON API - Locate Build, Build Environment, and Build Trigger API data

I am working with the Jenkins API JSON.
I understand the format to retrieve API data in JSON
<Jenkins_URL>/job/<job_name>/api/json
Within the job/<job_name>/configure UI we can configure/add Build triggers, build env, and build data.
I want to be able to view the Build, Build Env, and Build Triggers data in a JSON API.
Is it even possible to get said data? What are alternative ways to get all available data that is found in the configure page of a job?
I think the most straightforward way is to access <Jenkins_URL>/job/<job_name>/config.xml.
Yes, it's not JSON, but you can be sure that this contains everything that was configured on the configuration page.
The XML file is the "native" serialized version of the Job configuration. The JSON API will always require some additional glue that may exist or not exist.

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

Trigger Jenkins job via Slack command - 403_client_error

I'm trying to do a pretty simple thing I believe - trigger a Jenkins job from a slash command in slack. I want to type /dump in slack and that should trigger particular jenkins job. I'm using Slack Slash command to achieve this and the setup is very simple - type command, type URL, select POST/GET method.
I tried two methods - first was to use Build Authorization Token Root Plugin following this tutorial - in this case I configured Slack slash command to make GET request on the URL: http://jenkins_url:8080/job/db-dump-setup/build?token=MY_TOKEN - If I put the URL to my browser, the job gets triggered, if I run the /dump command, slack responds with:
slackbot [3:41 PM]
Darn - that slash command didn't work (error message: `403_client_error`). Manage the command at text.
The second approach I tried was following this tutorial. In this case I created new user in Jenkins and got API token. When running this command from my terminal: curl -X POST http://USER:TOKEN#slack_url:8080/job/db-dump-setup/build, the job gets again triggered, however when configuring the Slack slash command with the same URL and making POST request, I get the same error: 403_client_error
I tried to google this specific error but didn't find anything useful, also there are no more details for the error or any logs which could help me to troubleshoot this further.
Anyone run into something similar and knows how could I get this resolved?
I was able to resolve this so answering my own question in a case someone else runs into this. In order to make the first case work (Using the GET request), I had to enable the "Allow anonymous read access" in Jenkins, in Manage Jenkins -> Configure Global Security as per the screenshot below.
You should not need to enable anonymous access.Your jenkins requires authentication. Update your url like http://user:auth-token#jenkins-host:port.Auth token can be generated at the following URL JENKINS_HOST/me/configure

Resources