Executing build with parameters from Java program - jenkins

I am trying to execute a Jenkins build from my Java program using the Resty framework (using Resty isn't a requirement, just seemed like the easiest way). It works fine for jobs without parameters, including authentication, however I am trying to execute a build with a parameter but I am getting the (non-descript) Error 500 returned from Jenkins server.
URI jenkinsURI = new URI("https://"+jenkinsServer+"/job/bowling%20Q%20build/build?token="+jenkinsToken);
String b = URLEncoder.encode("json={\"parameter\": [{\"name\": \"git_tag\", \"value\": \"v1\"}],\"\":\"\"", "UTF-8");
System.out.println("My Results: "+r.text(jenkinsURI, Resty.content(b)));
Any idea how to do this? I have followed these instructions for sending the JSON and it works fine from curl, but does not from Java Resty.

The problem was that I didn't/can't use the URLEncoder. Once I changed the Resty.content to
System.out.println("My Results: "+r.text(jenkinsURI, Resty.form(Resty.data("json", "{\"parameter\": [{\"name\": \"git_tag\", \"value\": \"1.0.4\"}],\"\":\"\"}"))));
it started working fine.

Related

How can we get result of summary report parameters (throughput, received & sent bytes) in JMeter script through non gui mode?

How can we get result of summary report parameters (throughput, received & sent bytes) in JMeter script through non gui mode? I have to implement the benchmarking on whole script rather than each thread to mark the status of script pass/fail by comparing the result to the static .csv file which contains the value of parameters .Kindly let me know the approach to opt.
The easiest way is going for JMeterPluginsCMD Command Line Tool, it can generate various tables and charts out of JMeter's .jtl results file.
So you will need to add the following command as the Post Build Step:
JMeterPluginsCMD --generate-csv SummaryReport.csv --input-jtl result.jtl --plugin-type AggregateReport
You can install JMeterPluginsCMD Command Line Tool using JMeter Plugins Manager

Pass Jenkins build number to Protractor for SauceLabs

I am running protractor test cases through Jenkins, and using SauceLabs as execution environment. I am using Protractor-Cucumber-Framework. I want to pass build number from Jenkins so that I can pass same to SauceLabs to organize my test execution results.
I tried params as mentioned in this post
https://moduscreate.com/blog/protractor_parameters_adding_flexibility_automation_tests/
in Config.js
params: {
buildNumber:'xyz'
}
for running protractor :
protractor config/config.js --parameters.buildNumber= 1.1 --disableChecks"
using :
browser.params.buildNumber
This gives buildnumber =xyz and not 1.1
Could you please help me here
Update:
Sorry forgot to mention that I am using browser.params.buildNumber in after hook of cucumberjs.
you should use pattern: --params.xxx in cmd line, rather than --parameters.xxx.
In your case, should be: protractor config/config.js --params.buildNumber=1.1 --disableChecks
Note: Don't insert blank space around the =, like --params.name = value, or --params.name= value.
If the parameter value has blank space, you should use double quote to wrapper it, like --params.name="I like to xxx"

Trigger Maven Release Remotely

I want to start a Maven release programmatically from a Java program. This webpage shows how that is done generally. So that's what I did:
final URL url = new URL("http://jenkins/job/MyProject/m2release/submit");
final HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setDoOutput(true);
final String userpass = "User:Password";
final String authentication = "Basic " + DatatypeConverter.printBase64Binary(userpass.getBytes());
urlConnection.setRequestProperty("Authorization", authentication);
try (final OutputStream os = urlConnection.getOutputStream();
final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));) {
writer.write("releaseVersion=1.2.3&");
writer.write("developmentVersion=1.2.4-SNAPSHOT&");
// writer.write("isDryRun=on&"); // uncomment for dry run
writer.write("scmUsername=User&");
writer.write("scmPassword=Password&");
writer.write("scmCommentPrefix=[test]&");
writer.write("json={\"releaseVersion\":\"1.2.3\",\"developmentVersion\":\"1.2.4-SNAPSHOT\",\"isDryRun\":false}&");
writer.write("Submit=Schedule Maven Release Build");
writer.flush();
}
urlConnection.connect();
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(urlConnection.getInputStream(), "UTF-8"))) {
for (String line; (line = reader.readLine()) != null;) {
System.out.println(line);
}
}
This forum suggests "just have a look at the form befor you do a release and you should be able to craft a cURL request", that's what I did to get that far. The release starts at least.
I just don't now how to escape everything. The browser shows spaces as "+", but it does not work if I send the data that way. In fact, neither " ", "+" nor "%20" works as a space.
Still I get Unable to commit files in the build, so I'm pretty sure something is wrong with the username / password / comment prefix. The Jenkins itself returns the log-in page ("Authentication required"), even though the log-in is send.
What is the correct way to trigger a Maven Release on a Jenkins?
Okay, the parameters missing in my old approach were:
writer.write("specifyScmCredentials=on&");
writer.write("specifyScmCommentPrefix=on&");
The statement that comes to mind here is "Not all Jenkins jobs are equal".
The code you have posted simply invokes a Jenkins job called MyProject with the parameters releaseVersion and developmentVersion.
However the code has nothing to do with what MyProject does. It could be a job designed to build a Maven project, or a Gradle project, or a .NET project.
What you want to do (invoke the Maven release plugin) is the responsibility of the Jenkins job itself.
Have a look at the configuration of MyProject, specifically invoking a Maven build step that runs the release plugin.
Useful links
Maven Release Plugin
Building a Maven Project in Jenkins
Providing Parameters to Jenkins Builds
Btw one more valuable thing to mention is that u can pass custom parameters as a part of this
http://jenkins/job/MyProject/m2release/submit?json={} request. In order to do that u have to define query parameter json -> for example
json={"parameter": {"name":"CUSTOM_PARAM_1", "value":"CUSTOM_PARAM_1_VALUE"}}
At the moment of execution it will be treated as parametrized job and u will have your parameter together with
MVN_RELEASE_VERSION=X
MVN_DEV_VERSION=X-SNAPSHOT
MVN_ISDRYRUN=false
CUSTOM_PARAM_1=CUSTOM_PARAM_1_VALUE

Not able to run parameter passed methods in jenkins

i am using java with selenium , maven, testng for my project.
for some functions , i have passed parameters through testng parameters annotations.
while build and executing the project through jenkins,
parameter passed methods are not executed through jenkins.
other methods are executed fine.
Please provide solutions. Awaiting the response
sample code
#Test(priority=1)
#Parameters({"drivername","driverpath","outputpath","URL"})
public void opendriver(#Optional("drivername") String drivername,#Optional("driverpath") String driverpath,#Optional("outputpath") String outputpath,#Optional("URL") String URL)
in the above code i have passed some parameters through testng. while executing the code
through maven it is working fine. but making build in jenkins and execute the script
it is not working. Please suggest and advise

How do I make the jenkins API return more builds?

I have a script that pulls the artifacts from a jenkins job and installs it on our hardware test system. Now, today I need to downgrade to a pretty old version. Unfortunately, the jenkins API only returns the last few builds.
I use the jenkinsapi python API. It fails as follows:
/usr/local/lib/python2.7/dist-packages/jenkinsapi-0.1.6-py2.7.egg/jenkinsapi/job.pyc in get_build(self, buildnumber)
177 def get_build( self, buildnumber ):
178 assert type(buildnumber) == int
--> 179 url = self.get_build_dict()[ buildnumber ]
180 return Build( url, buildnumber, job=self )
181
The python API hits the the url http://jenkins/job/job-name/api/python/. If I do that myself, then I get the following response:
{"actions":[{},{},{},{},{},{},{}],
"description":"text",
"displayName":"job-name",
"displayNameOrNull":None,
"name":"job-name",
"url":"http://jenkins/job/job-name/",
"buildable":True,
"builds":[
{"number":437,"url":"http://jenkins/job/job-name/437/"},
{"number":436,"url":"http://jenkins/job/job-name/436/"},
{"number":435,"url":"http://jenkins/job/job-name/435/"},
{"number":434,"url":"http://jenkins/job/job-name/434/"},
{"number":433,"url":"http://jenkins/job/job-name/433/"},
{"number":432,"url":"http://jenkins/job/job-name/432/"},
{"number":431,"url":"http://jenkins/job/job-name/431/"},
{"number":430,"url":"http://jenkins/job/job-name/430/"},
{"number":429,"url":"http://jenkins/job/job-name/429/"},
{"number":428,"url":"http://jenkins/job/job-name/428/"},
{"number":427,"url":"http://jenkins/job/job-name/427/"},
{"number":426,"url":"http://jenkins/job/job-name/426/"},
{"number":425,"url":"http://jenkins/job/job-name/425/"},
{"number":424,"url":"http://jenkins/job/job-name/424/"},
{"number":423,"url":"http://jenkins/job/job-name/423/"}],
"color":"yellow_anime",
"firstBuild": {"number":311,"url":"http://jenkins/job/job-name/311/"},
"healthReport":[
{"description":"Test Result: 0 tests failing out of a total of 3 tests.","iconUrl":"health-80plus.png","score":100},
{"description":"Build stability: No recent builds failed.","iconUrl":"health-80plus.png","score":100}],
"inQueue":False,
"keepDependencies":False,
"lastBuild":{"number":438,"url":"http://jenkins/job/job-name/438/"},
"lastCompletedBuild":{"number":437,"url":"http://jenkins/job/job-name/437/"},
"lastFailedBuild":{"number":386,"url":"http://jenkins/job/job-name/386/"},
"lastStableBuild":{"number":424,"url":"http://jenkins/job/job-name/424/"},
"lastSuccessfulBuild":{"number":437,"url":"http://jenkins/job/job-name/437/"},
"lastUnstableBuild":{"number":437,"url":"http://jenkins/job/job-name/437/"},
"lastUnsuccessfulBuild":{"number":437,"url":"http://jenkins/job/job-name/437/"},
"nextBuildNumber":439,
"property":[],
"queueItem":None,
"concurrentBuild":False,
"downstreamProjects":[],
"scm":{},
"upstreamProjects":[]}
Now, I wanted to get job number 315. How do I do this?
I ended up using the following workaround:
try:
build=job.get_build(build_no)
except KeyError:
build=jenkinsapi.build.Build('%s%d/' % (job.baseurl, build_no), build_no, job=job)
It's not pretty, but it works.
Are you sure that all builds are present and not deleted? Maybe there are some settings enabled (like delete old builds via limit).. I've tried to hit the URL on my Jenkins instance, it renders all builds (around 150). I've tried both python and XML api versions.

Resources