jenkins-job-builder 在使用凭据插件时生成错误 xml

jenkins-job-builder generates wrong xml when using credentials plugin

我们正在使用 jenkins-job-builder 来生成 jenkins 作业,当我们尝试使用 ssh-credentials 插件时,我们遇到了一个持续的问题。 当我们创建一个尝试使用 ssh 密钥的新作业时,该作业失败并显示 java.io.IOException: [ssh-agent] Could not find specified credentials 但如果我们点击 ConfigureSave 它就会开始工作。

在 credentials.xml 中,键定义如下所示:


 <com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey plugin="ssh-credentials@1.15">
          <scope>GLOBAL</scope>
          <id>jenkins-key</id>
          <description>Jenkins user private key</description>
          <username>root</username>
          <privateKeySource class="com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey$DirectEntryPrivateKeySource">
            <privateKey>{A_WORKING_PRIVATE_KEY}</privateKey>
          </privateKeySource>
   </com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey>

关于文档 (https://docs.openstack.org/infra/jenkins-job-builder/wrappers.html?highlight=credentials#wrappers.ssh-agent-credentials),使用已定义凭据的作业定义应如下所示:

- wrapper:
    name: jenkins-key
    wrappers:
      - ssh-agent-credentials:
          user: 'root'

[...]

- job:
    name: jobxyz
    disabled: false
    project-type: freestyle
    node: jenkins-slave
    wrappers:
        - jenkins-key
    builders:
      - shell: |
          [...]

生成的作业 xml 中的相应部分如下所示:

   <buildWrappers>
      <com.cloudbees.jenkins.plugins.sshagent.SSHAgentBuildWrapper>
        <user>root</user>
        <ignoreMissing>false</ignoreMissing>
      </com.cloudbees.jenkins.plugins.sshagent.SSHAgentBuildWrapper>
    </buildWrappers> 

但是当我们尝试 运行 作业时,我们得到了上述错误:

FATAL: 
java.io.IOException: [ssh-agent] Could not find specified credentials
    at com.cloudbees.jenkins.plugins.sshagent.SSHAgentBuildWrapper.preCheckout(SSHAgentBuildWrapper.java:209)
    at jenkins.scm.SCMCheckoutStrategy.preCheckout(SCMCheckoutStrategy.java:76)
    at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:498)
    at hudson.model.Run.execute(Run.java:1818)
    at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
    at hudson.model.ResourceController.execute(ResourceController.java:97)
    at hudson.model.Executor.run(Executor.java:429)
FATAL: [ssh-agent] Could not find specified credentials
java.io.IOException: [ssh-agent] Could not find specified credentials
    at com.cloudbees.jenkins.plugins.sshagent.SSHAgentBuildWrapper.preCheckout(SSHAgentBuildWrapper.java:209)
    at jenkins.scm.SCMCheckoutStrategy.preCheckout(SCMCheckoutStrategy.java:76)
    at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:498)
    at hudson.model.Run.execute(Run.java:1818)
    at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
    at hudson.model.ResourceController.execute(ResourceController.java:97)
    at hudson.model.Executor.run(Executor.java:429)
Finished: FAILURE

如果我们转到 jenkins 网页并点击 configure & save 而不更改配置中的任何内容,则作业的新 xml 将更改为:

  <buildWrappers>
      <com.cloudbees.jenkins.plugins.sshagent.SSHAgentBuildWrapper plugin="ssh-agent@1.17">
        <credentialIds>
          <string>jenkins-key</string>
        </credentialIds>
        <ignoreMissing>false</ignoreMissing>
      </com.cloudbees.jenkins.plugins.sshagent.SSHAgentBuildWrapper>
    </buildWrappers>

我很确定这不应该像这样工作,但我有点不确定下一步该怎么做。 以下是一些版本:

Jenkins Job Builder 版本:2.10.1

詹金斯版本。 2.172

SSH 凭据版本:1.15

似乎最终解决方案是忘记使用凭据包装器,而是插入原始 xml:

wrappers:
        - raw:
            xml: |
              <com.cloudbees.jenkins.plugins.sshagent.SSHAgentBuildWrapper plugin="ssh-agent@1.17">
                  <credentialIds>
                      <string>jenkins-key</string>
                  </credentialIds>
                  <ignoreMissing>false</ignoreMissing>
              </com.cloudbees.jenkins.plugins.sshagent.SSHAgentBuildWrapper>

这会生成正确的 xml。