2

有人知道如何通过 Jenkins 中的 API 或 POST 请求创建新的 jenkins (2.8) 凭据(用于 git 访问的 fe)吗?我曾尝试使用此代码(来自另一个 stackoverflow 主题),但它什么也没做:

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
4

3 回答 3

2

我是这样做的:

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

凭据.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)
于 2016-06-07T17:17:30.790 回答
1

我遇到了同样的问题,经过一些挖掘/测试后,您似乎需要更改它

/credential-store/domain/_/createCredentials

对此

/credentials/store/system/domain/_/createCredentials
于 2016-06-06T18:37:51.073 回答
0

它不起作用:/credential-store/domain/_/api/json

你必须使用这个网址:/credentials/store/system/domain/_/api/json

于 2016-09-06T06:28:46.710 回答