Abraham Mesa
Abraham Mesa

Reputation: 83

Create Job Dynamically in Jenkins

I have a job in jenkins with a configuration, then, with the jenkins API in /cli i can get-job (API method) with an xml structure of my job and then i can create-job (API method) in jenkins with the followed xml.

?xml version='1.0' encoding='UTF-8'?>
<project>
 <actions/>
  <description></description>
  <keepDependencies>false</keepDependencies>
  <properties/>
<scm class="hudson.plugins.git.GitSCM" plugin="[email protected]">
<configVersion>2</configVersion>
<userRemoteConfigs>
  <hudson.plugins.git.UserRemoteConfig>
  <url>https://username:[email protected]/repoowner/project.git</url>
  <credentialsId>550e8400-e29b-41d4-a716-446655440000</credentialsId>
  </hudson.plugins.git.UserRemoteConfig>
</userRemoteConfigs>
....

Even if i give this url tag "https://username:[email protected]/repoowner/project.git" jenkins needs authentication to work, so in credentialsId tag jenkins give an UUID.

I want to be able to create a job dynamically by an external application with a given URL in this format "https://username:[email protected]/repoowner/project.git".

How can it be done?

Thanks.

Upvotes: 1

Views: 3579

Answers (2)

SvenSeemann
SvenSeemann

Reputation: 31

You can get the credentialsId via the API and the credentials-store plugin.

e.g. for credentials in global Domain

${ your-jenkins-domain }/credential-store/domain/_/api/xml

<domainWrapper>
 <credentials>
  <_XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/>
 </credentials>
 <description></description>
 <displayName></displayName>
 <fullDisplayName></fullDisplayName>
 <fullName>credential-store/_</fullName>
 <global>true</global>
 <urlName>_</urlName>
</domainWrapper>

But on some point it is a bit tricky:
when accessing the xml api for global domain the id already has a '_' as prefix. When using other domains the prefix is missing (but in a job a prefix is added... couldn't figure out where the prefix can be found)

e.g. I stored github access data in a separated domain, the credentialsId tag was:

<XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/>

but used in a job id was:

0XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX

Upvotes: 3

Abraham Mesa
Abraham Mesa

Reputation: 83

Actually i solve the problem just creating an generic user in bitbucket, then the UUID its always the same and i can just copy and paste that UUID to the others project.xml files.

Upvotes: 0

Related Questions