2016-06-06 8 views
2

Kann jemand eine neue jenkins (2.8) Anmeldeinformationen (zB für einen Git-Zugang) über API oder POST-Anfrage in Jenkins erstellen? Ich habe versucht, diesen Code zu verwenden (von einem anderen Stackoverflow Thema), aber es tut nichts:Wie erstelle ich eine jenkins credentials via API?

import json 
import requests 

def main(): 
    data = { 
     'credentials': { 
      'scope': "GLOBAL", 
      'username': "jenkins", 
      'privateKeySource': { 
       'privateKey': "-----BEGIN RSA PRIVATE KEY-----\nX\n-----END RSA PRIVATE KEY-----", 
       'stapler-class': "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey$DirectEntryPrivateKeySource" 
      }, 
      'stapler-class': "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey" 
     } 
    } 

    payload = { 
     'json': json.dumps(data), 
     'Submit': "OK", 
    } 
    r = requests.post("http://%s:%d/credential-store/domain/_/createCredentials" % (localhost, 8080), data=payload) 
    if r.status_code != requests.codes.ok: 
     print r.text 

Antwort

0

ich in der gleichen Ausgabe lief und nach ein wenig Graben/Testen es scheint, dass Sie diese

ändern müssen
/credential-store/domain/_/createCredentials 

dieser

/credentials/store/system/domain/_/createCredentials 
2

ich tat es auf diese Weise:

java -jar /tmp/jenkins-cli.jar -s http://localhost:8080/ \ 
groovy /tmp/credentials.groovy id username password 

credentials.groovy

import jenkins.model.* 
import com.cloudbees.plugins.credentials.* 
import com.cloudbees.plugins.credentials.common.* 
import com.cloudbees.plugins.credentials.domains.* 
import com.cloudbees.plugins.credentials.impl.* 

domain = Domain.global() 
store = Jenkins.instance.getExtensionList('com.cloudbees.plugins.credentials.SystemCredentialsProvider')[0].getStore() 

usernameAndPassword = new UsernamePasswordCredentialsImpl(
    CredentialsScope.GLOBAL, 
    args[0], 
    "", 
    args[1], 
    args[2] 
) 

store.addCredentials(domain, usernameAndPassword) 
+0

Ist es möglich, von Java? Wenn Sie es mit Java machen können, sagen Sie mir bitte Ihre Antwort. – user2434741

0

funktioniert Es ist nicht: /credential-store/domain/_/api/json

Sie haben diese URL zu verwenden: /credentials/store/system/domain/_/api/json

Verwandte Themen