从詹金斯(Jenkins)将凭据传递给Maven

从詹金斯(Jenkins)将凭据传递给Maven

问题描述:

我有一个运行Jenkins的节点,该节点使用maven构建代码. Jenkins作业是一个声明性管道脚本. Maven需要从需要证书才能访问的私有存储库中下载依赖项.凭据存储在Jenkins凭据管理器中.

I have a node running Jenkins that builds code with maven. The Jenkins job is a declarative pipeline script. Maven needs to download dependencies from private repositories which require credentials to access. The credentials are stored in the Jenkins credential manager.

如何将这些凭据传递给Maven,以便Maven可以使用这些凭据从私有存储库正确下载依赖项.

How can I pass these credentials to maven so that maven can correctly download dependencies from private repos using these credentials.

通过将Jenkins凭据注入您的环境,然后将这些凭据传递给Maven,您可以使用Jenkins凭据访问私有存储库.

By injecting Jenkins credentials into your environment, and then passing those credentials to maven, you can access private repos using Jenkins credentials.

步骤:

  1. 如果还没有创建一对新的Jenkins凭证(我正在使用id"test-creds")
  2. 使用该问题的说明:如何通过环境变种.在需要凭据的maven命令周围,使用"withCredentials"块.然后将这些凭据传递给Maven.
  1. Create a new pair of Jenkins credentials if you haven't already (I am using the id "test-creds")
  2. Using the instructions from this question : How to pass Maven settings via environmental vars. Around the maven command that requires the credentials, use a "withCredentials" block. And then pass those credentials in to maven.

    withCredentials([usernamePassword(credentialsId: 'test-creds', passwordVariable: 'PASSWORD_VAR', usernameVariable: 'USERNAME_VAR')])
    {
        sh 'mvn clean install -Dserver.username=${USERNAME_VAR} -Dserver.password=${PASSWORD_VAR}'
    }

  1. 在您的 settings.xml 文件中,引用以下变量:
  1. In your settings.xml file, reference these variables:

    <servers>
        <server>
            <id>yourRepoName</id>
            <username>${server.username}</username>
            <password>${server.password}</password>
        </server>
    </servers>

  1. 如果需要指定settings.xml文件,则可以在maven命令中使用-s-gs标志.
  1. If you need to specify a settings.xml file, you can use the -s or -gs flag in your maven command.