Set Jenkins build cause manually via REST API - jenkins

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.

Related

Jenkins API - buildWithParameters and fileParameter

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.

How to pass JOB parameters using Jenkins HTTP POST plugin

I want to pass the Build Name and the BUILD ID to the REST API using Jenkins HTTP POST plugin
how to pass the parameters to it?
I am passing:
http://localhost:55223/api/Demo?BuildName=${JOB_NAME}&BuildID=${BUILD_ID}
I am receiving an error
It looks like the Plugin does not expand environment variables, as evidenced by it's source code. As the plugin has not been updated in 2 years, I don't think there's any chance of the developer adding this anytime soon. If you are still wanting to use the plugin, you could make the changes changes necessary to expand the environment variables and then build it from source. For that, I would recommend looking at the Jenkins classes hudson.EnvVars and hudson.model.Run. More specifically, the Run method getEnvironment(TaskListener listener) and the EnvVars method expand(String s).

Not able to access custom parameters passed to the jenkins build script

I want to add some parameters from the web hooks into the mail sent from the Jenkins. I tried solutions provided in StackOverflow and elsewhere. No success yet.
I have the following in place:
Checked This is a parameterized build
The build script looks like http://JENKINS_URL/job/android02/buildWithParameters?token=<My Token>&PARAM=<My Custom Params>
In the mail content, I try to access the custom parameters by ${PARAM}, $PARAM.
In the mail, however, I'm not getting the values I'm setting. If I set default values for PARAM, it is correctly displayed in the mail I receive. I tried http://JENKINS_URL/job/android02/build?token=<My Token>&PARAM=<My Custom Params> URL as well thinking just in case if it works. The mail is sent as Editable email Notification config.
Basically, everything is working except that I'm not able to access the custom parameters I have passed via Trigger builds remotely option.
Edit1:
If I keep a default value, say, test for the parameter, PARAM, in the mail received, I can see test displayed. But I need to get the value I'm passing in the build script.
From the "tool tip" that you get when you click on the question mark next to "":
${ENV,var="VARIABLENAME"}
Expands to an environment variable (specified here as VARIABLENAME) from the build environment. Note that this does not include any variables set by the build scripts themselves, only those set by Jenkins and other plugins.
So, use ${ENV,var="PARAM"}.

Disabling and enabling jobs in jenkins

Currently in order to enable or disable a job, a user must have Job Configure permissions in the Matrix-based security configuration.We would like to be able to manage the enable / disable job permission independently from the job configure permission.
There are some nightly jobs that we want every user to be able to enable and disable the project without touching/breaking the configuration.
Thanks
Provide a script for the users that will do this using the credentials of 'root' user and set only the execute bit on the script so that no one can read/copy it.
At least 3 ways to make a script:
HTTP POST request:
1.
curl -X POST http(s)://<your_jenkins_url>/jenkins/job/<nightly-build_job_name>/disable
2.
Use python JenkinsAPI.
Documentation is very good, easy to understand much like the API.
3.
The third one can be a script which will use jenkins-cli: accepted answer describes this well .
The Job Configure permission is bounded to the disable/enable function in each job, that's true.
One alternative to disable/enable jobs without the corresponding permission is to create new jobs which do this internally. For example, a job that needs job names as parameters, and disables them.
You could use curl + credentials of a Jenkins user with the Job Configure permission.
You could use plugins. For example, this script using the Job DSL Plugin:
job("jobname"){
using("jobname")
disabled(true)
}
For other options, check out this question.
You may try to install this plugin to get the enable/disable button for the individual project-
I checked in my Jenkins and I could see this:
But when I checked under plugins section I don't see this extra column plugin installed. Probably this is the default behavior in latest versions of Jenkins.

Provide userName and shortDescription in Jenkins remote API job triggering

I know how to provide build parameters:
wget --post-data='json={"parameter": {"name": "testparam", "value": "HELLO"}}' http://jenkins/job/Job1/build?delay=0sec
But, is it possible to provide a shortDescription and userName in a Jenkins remote API build request via wget/curl?
How should it look like in json or xml? Is there any manual/guidance on the net?
I will use this in along with the problem described in Trigger dynamic set of jobs. I want to provide triggered job with the calling job name and build number.
You may consider using Jenkins CLI (http://[jenkins-host]/cli for help in the browser). You can specify a user to a build CLI command. I'm not sure what you mean by short description when starting a build, though.
Update: Please see Jenkins Wiki Authenticating Scripted Clients. I've created a user foobar ('full name' Foo Bar) and tried the following:
wget --auth-no-challenge --http-user=foobar --http-password=[apiToken] http://jenkins.yourcompany.com/job/your_job/build
Where the token is obtained from user configuration page: http://localhost:8081/user/foobar/configure. It worked. The user has to exist, though. Also, you must specify --auth-no-challenge option, otherwise it kicks off the build as anonymous. The status description says Started by user Foo Bar.
Another Update If everything else fails, you may consider the following workaround: start all builds via the Parameterized Trigger Plugin with an additional boolean parameter that tells the triggered job whether to run or not. In case the job is asked not to run it would fail immediately and call a 'clean-up' job passing to it the build info; the clean-up job then will delete the build from the system.

Resources