Programmatically add the new cred into the credentials.xml file

544 views
Skip to first unread message

Tanya Fernandez

unread,
Jul 30, 2015, 8:51:31 AM7/30/15
to Jenkins Developers

Hi,


I am trying to programmatically create an Jenkins job with code being checked out fromSVN. So for adding new SVN credentials I programmatically add the new credentials into the credentials.xml file present in $JENKINS_HOME folder and link the corresponding customized ID to the job using the <credentials> tag in job config file. 


I have tried to create a plugin for implement this. This I based on the credentials plugin using the Jenkins CLI commands.

If there is any there any other approach to this I’d like to know. Basically I have automated the job creation process and want to be able to programmatically add new credentials on job creation in Jenkins. 

Problems faced:


I ahve tried to build the credentials plugin after addition of a couple of syso statements. This was to check inputs the plugin takes to eventually modify credentials.xml file.


Specifically what I was looking for: In the doConfigSubmit() function of SystemCredentialsProvider class; the data req.getSubmittedForm() consists of or what the StaplerRequest's   bindJSONToList(DomainCredentials.class, data.get("domainCredentials")) function returns. This is so i can programatically provide this data rather than from the current UI input structure.


May I please get an opinion on this. Any help would be much appreciated.

Jesse Glick

unread,
Jul 30, 2015, 1:02:12 PM7/30/15
to Jenkins Dev
On Thu, Jul 30, 2015 at 8:51 AM, Tanya Fernandez
<tanyafer...@gmail.com> wrote:
> I have tried to create a plugin for implement this. This I based on the
> credentials plugin using the Jenkins CLI commands.

You mean, you are trying to implement
https://issues.jenkins-ci.org/browse/JENKINS-28407? I am not really
clear on what you are trying to accomplish. Write a new plugin?
Enhance an existing plugin? Run something from a particular server’s
script console? Something else?

For the simplest cases you can use something like:

https://github.com/jenkinsci/credentials-plugin/blob/e4f1234fe1b3c35e322782d4d005df38de29d943/src/test/java/com/cloudbees/plugins/credentials/SystemCredentialsProviderTest.java#L36

Tanya Fernandez

unread,
Aug 7, 2015, 7:38:34 AM8/7/15
to Jenkins Developers
Hi,
Yes I was trying to write a new plugin. A simple one to add new credentials into the credentials.xml file programmatically. It was supposed to mimic the credentials plugin, like a subset of it.
I went through the SystemCredentialsProviderTest.java#L36 and found exactly what i was looking for which is the following code snippet:

SystemCredentialsProvider.getInstance().getCredentials().add(new UsernamePasswordCredentialsImpl(scope, id, description, name, passwd));
 after this i used the 
SystemCredentialsProvider.getInstance().save();
and got the results i wanted.

This took in the credentials I wanted to add to the xml file and updated the same.
Thanks so much for the really quick and extremely helpful reply.
Reply all
Reply to author
Forward
0 new messages