我正在尝试使用以下curl命令将私钥上传到Jenkins SSH凭证:
JENKINS_PRIVATE_KEY_AS_STRING='-----BEGIN RSA PRIVATE KEY----- MIIEo...2qwu8diE -----END RSA PRIVATE KEY-----'
curl -H $CRUMB -X POST "http://$USER:$API_TOKEN@$JENKINS_HOST/credentials/store/system/domain/_/createCredentials" \
--data-urlencode "json={
"": "0",
"credentials": {
"scope": "GLOBAL",
"id": "jenkins-ssh",
"username": "jenkins",
"password": "",
"privateKeySource": {
"stapler-class": "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey$DirectEntryPrivateKeySource",
"privateKey": "$JENKINS_PRIVATE_KEY_AS_STRING",
},
"description": "jenkins-ssh-credentials",
"stapler-class": "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey"
}
}"
但是当我运行上面的命令时,它失败并显示:
curl: option -----END: is unknown
curl: try 'curl --help' or 'curl --manual' for more information
为什么变量$JENKINS_PRIVATE_KEY_AS_STRING
中的字符串引起错误,该如何解决?
基于以下答案,我也尝试过:
curl -H $CRUMB -X POST "http://$USER:$API_TOKEN@$JENKINS_HOST/credentials/store/system/domain/_/createCredentials" \
--data-urlencode "json={
\"\": \"0\",
\"credentials\": {
\"scope\": \"GLOBAL\",
\"id\": \"jenkins-ssh\",
\"username\": \"jenkins\",
\"password\": \"\",
\"privateKeySource\": {
\"stapler-class\": \"com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey$DirectEntryPrivateKeySource\",
\"privateKey\": \"$JENKINS_PRIVATE_KEY_AS_STRING\",
},
\"description\": \"jenkins-ssh-credentials\",
\"stapler-class\": \"com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey\"
}
}"
但这会导致错误:
: Specified type class com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey is not assignable to the expected class com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey$PrivateKeySource
at org.kohsuke.stapler.RequestImpl$TypePair.convertJSON(RequestImpl.java:671)
Caused: java.lang.IllegalArgumentException: Failed to instantiate class com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey$PrivateKeySource from {"stapler-class":"com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey","privateKey":"-----BEGIN RSA PRIVATE KEY----- MIIEo...2qwu8diE -----END RSA PRIVATE KEY-----"}
at org.kohsuke.stapler.RequestImpl$TypePair.convertJSON(RequestImpl.java:680)
at org.kohsuke.stapler.RequestImpl.bindJSON(RequestImpl.java:478)
at org.kohsuke.stapler.RequestImpl.instantiate(RequestImpl.java:777)
Caused: java.lang.IllegalArgumentException: Failed to convert the privateKeySource parameter of the constructor public com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey(com.cloudbees.plugins.credentials.CredentialsScope,java.lang.String,java.lang.String,com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey$PrivateKeySource,java.lang.String,java.lang.String)
但不确定其是否与卷曲部分有关。
答案 0 :(得分:0)
您需要修正引号:
v
--data-urlencode "json={
"": "0",
^
相反,转义出现在参数字符串中的"
个字符:
--data-urlencode "json={
\"\": \"0\",
[...]
答案 1 :(得分:0)
根据帖子找到了解决方案:
Store JSON directly in bash script with variables?
因此,诀窍是这样引用变量:"'"$JENKINS_PRIVATE_KEY_AS_STRING"'"
。完整的工作示例变为:
curl -H $CRUMB -X POST "http://$USER:$API_TOKEN@$JENKINS_HOST/credentials/store/system/domain/_/createCredentials" \
--data-urlencode 'json={
"": "0",
"credentials": {
"scope": "GLOBAL",
"id": "jenkins-ssh",
"username": "jenkins",
"password": "",
"privateKeySource": {
"stapler-class": "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey$DirectEntryPrivateKeySource",
"privateKey": "'"$JENKINS_PRIVATE_KEY_AS_STRING"'",
},
"description": "jenkins-ssh-credentials",
"stapler-class": "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey"
}
}'