如何在脚本控制台中列出所有Jenkins凭据?

如何在脚本控制台中列出所有Jenkins凭据?,jenkins,groovy,mercurial,Jenkins,Groovy,Mercurial,我想让詹金斯从BitBucket克隆我的mercurial项目。不会的,因为它说凭证有问题——好吧,bitbucket拒绝Jenkins提供的任何东西 我几乎100%肯定詹金斯没有提供它应该提供的东西,因为当我跑步时 hg clone --ssh="ssh -i /path/to/my/key" ssh://hg@bitbucket.org/my-org/my-repo 好的。/path/to/my/key的内容是我在Jenkins的凭证管理器中放入的密钥。我已经验证了它在我的jenkinsc

我想让詹金斯从BitBucket克隆我的mercurial项目。不会的,因为它说凭证有问题——好吧,bitbucket拒绝Jenkins提供的任何东西

我几乎100%肯定詹金斯没有提供它应该提供的东西,因为当我跑步时

hg clone --ssh="ssh -i /path/to/my/key" ssh://hg@bitbucket.org/my-org/my-repo
好的。
/path/to/my/key
的内容是我在Jenkins的凭证管理器中放入的密钥。我已经验证了它在我的jenkins
credentials.xml文件中

然而,当我试着管理我的工作时?克隆失败是因为

remote: Host key verification failed.
这让我相信问题在于通过mercurial插件传递的内容。但是我在日志中看不到它,因为它是某种类型的,只是显示为
******

因此,我想确保Hg插件的凭证实际上是我的
credentials.xml
文件中的凭证

到目前为止,我已经做到了:

import com.cloudbees.plugins.credentials.CredentialsProvider
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials


def creds = CredentialsProvider.all()
print creds
这给了我一个凭证提供者的列表。。。但我不知道下一步该去哪里。我一直沉浸在文档中,试图找出如何获取我想要的凭据信息。。。但是没有骰子


(如何)在Groovy脚本控制台中显示我的Jenkins凭据列表?我想要一个可用凭据列表,并找到了以下内容:

def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials( com.cloudbees.plugins.credentials.common.StandardUsernameCredentials.class, Jenkins.instance, null, null );

for (c in creds) {
   println(c.id + ": " + c.description)
}
从这里得到:

这是快速脏代码,它将在jenkins上打印一些凭证类型,如果需要更多,可以使用此处定义的不同类:


这对我来说非常有效

def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
      com.cloudbees.plugins.credentials.Credentials.class
)

for (c in creds) {
  println(c.id)
  if (c.properties.description) {
    println("   description: " + c.description)
  }
  if (c.properties.username) {
    println("   username: " + c.username)
  }
  if (c.properties.password) {
    println("   password: " + c.password)
  }
  if (c.properties.passphrase) {
    println("   passphrase: " + c.passphrase)
  }
  if (c.properties.secret) {
    println("   secret: " + c.secret)
  }
  if (c.properties.privateKeySource) {
    println("   privateKey: " + c.getPrivateKey())
  }
  println("")
}

这是一个组合脚本,将此主题的多个发现连接在一起。它列出了Jenkins所有作用域的所有凭据,而不仅仅是根作用域。希望能有帮助

import com.cloudbees.plugins.credentials.Credentials


Set<Credentials> allCredentials = new HashSet<Credentials>();


def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
      com.cloudbees.plugins.credentials.Credentials.class
);


allCredentials.addAll(creds)


Jenkins.instance.getAllItems(com.cloudbees.hudson.plugins.folder.Folder.class).each{ f ->
 creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
      com.cloudbees.plugins.credentials.Credentials.class, f)
  allCredentials.addAll(creds)

}


for (c in allCredentials) {
  println(c.id)
  if (c.properties.username) {
    println("   description: " + c.description)
  }
  if (c.properties.username) {
    println("   username: " + c.username)
  }
  if (c.properties.password) {
    println("   password: " + c.password)
  }
  if (c.properties.passphrase) {
    println("   passphrase: " + c.passphrase)
  }
  if (c.properties.secret) {
    println("   secret: " + c.secret)
  }
  if (c.properties.privateKeySource) {
    println("   privateKey: " + c.getPrivateKey())
  }
  println("")
}
导入com.cloudbees.plugins.credentials.credentials
Set allCredentials=new HashSet();
def creds=com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
com.cloudbees.plugins.credentials.credentials.class
);
allCredentials.addAll(creds)
Jenkins.instance.getAllItems(com.cloudbees.hudson.plugins.folder.folder.class)。每个{f->
creds=com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
com.cloudbees.plugins.credentials.credentials.class,f)
allCredentials.addAll(creds)
}
对于(所有凭证中的c){
println(c.id)
if(c.properties.username){
println(“说明:+c.说明)
}
if(c.properties.username){
println(“用户名:”+c.username)
}
if(c.properties.password){
println(“密码:+c.password”)
}
if(c.properties.passphrase){
println(“密码短语:+c.passphrase”)
}
if(c.properties.secret){
println(“秘密:+c.secret)
}
if(c.properties.privateKeySource){
println(“privateKey:+c.getPrivateKey())
}
println(“”)
}

这是我根据已接受的答案修改的脚本。我们使用azure凭据,这将打印它们。如果找不到密码类型,它还将打印
c.properties

    com.cloudbees.plugins.credentials.Credentials.class
)

for (c in creds) {
  println(c.id)
  if (c.properties.username) {
    println("   description: " + c.description)
  }
  if (c.properties.username) {
    println("   username: |" + c.username + "|")
  }
  else if (c.properties.password) {
    println("   password: |" + c.password + "|")
  }
  else if (c.properties.passphrase) {
    println("   passphrase: |" + c.passphrase + "|")
  }
  else if (c.properties.secret) {
    println("   secret: |" + c.secret + "|")
  }
  else if (c.properties.privateKeySource) {
    println("   privateKey: " + c.getPrivateKey())
  }
  else if (c.properties.clientId) {
    println("   clientId    : " + c.getClientId())
    println("   tenant      : " + c.getTenant())
    println("   subscription: " + c.getSubscriptionId())
    println("   secret      : " + hudson.util.Secret.fromString(c.getClientSecret()))
  }
  else {
    println("unknown secret type")
    println(c.properties)
  }
  println("")
}

您应该在答案中添加相关信息,以防止链接丢失。这不会在文件夹中获得任何凭据。这是一个有用的答案和代码片段,可以实现此目的。直到我删除了大部分的println:s,Thx@vkozyrevI才开始工作。不断出现50倍的错误。我只留下了
println(c.id)
,但这没关系,因为我只是在寻找一个特定的id。
    com.cloudbees.plugins.credentials.Credentials.class
)

for (c in creds) {
  println(c.id)
  if (c.properties.username) {
    println("   description: " + c.description)
  }
  if (c.properties.username) {
    println("   username: |" + c.username + "|")
  }
  else if (c.properties.password) {
    println("   password: |" + c.password + "|")
  }
  else if (c.properties.passphrase) {
    println("   passphrase: |" + c.passphrase + "|")
  }
  else if (c.properties.secret) {
    println("   secret: |" + c.secret + "|")
  }
  else if (c.properties.privateKeySource) {
    println("   privateKey: " + c.getPrivateKey())
  }
  else if (c.properties.clientId) {
    println("   clientId    : " + c.getClientId())
    println("   tenant      : " + c.getTenant())
    println("   subscription: " + c.getSubscriptionId())
    println("   secret      : " + hudson.util.Secret.fromString(c.getClientSecret()))
  }
  else {
    println("unknown secret type")
    println(c.properties)
  }
  println("")
}