【问题标题】:Jenkins pipeline - mvn command doesn't read withCredentials variables properlyJenkins 管道 - mvn 命令无法正确读取 withCredentials 变量
【发布时间】:2018-10-07 00:46:06
【问题描述】:

我正在尝试将 jgitflow 插件用于 maven 并通过 Jenkins 管道运行发布。

插件配置:

<plugin>
     <groupId>external.atlassian.jgitflow</groupId>
     <artifactId>jgitflow-maven-plugin</artifactId>
     <version>1.0-m5.1</version>
     <configuration>
        <username>${git.user}</username>
        <password>${git.password}</password>
        <enableSshAgent>true</enableSshAgent>
        <autoVersionSubmodules>true</autoVersionSubmodules>
        <noDeploy>true</noDeploy>
        <releaseBranchVersionSuffix>-RELEASE</releaseBranchVersionSuffix>
     </configuration>
</plugin>

问题是当我传递 jgitflow 的凭据时。

withCredentials([usernamePassword(credentialsId: 'my_credentials_id', passwordVariable: 'USERNAME', usernameVariable: 'PASSWORD')]) {
    sh "git checkout develop"
    sh "mvn -f jgitflow:release-start -B -U -DskipTests -DnoDeploy=true -DpushReleases=false -Dgit.user=$USERNAME -Dgit.password=$PASSWORD"
    sh "mvn -f jgitflow:release-finish -B -U -Dmaven.javadoc.skip=true -DskipTests -DnoDeploy=true -DpushReleases=true -Dgit.user=$USERNAME -Dgit.password=$PASSWORD"
 }

上面的设置不起作用,但如果我明确地传递用户名和密码而不是变量,一切都很好。是不是我用错了?

【问题讨论】:

  • 您是如何为凭据生成管道脚本的?
  • 使用 Jenkins 的“管道语法”页面。我尝试调用echo "$USERNAME",它给了我一个掩码形式的用户名,所以我认为语法是正确的。
  • 您可以尝试按照以下语法添加:withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: '&lt;CREDENTIAL_ID&gt;', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']])
  • 它有效,谢谢。我必须阅读有关凭据绑定的更多信息。
  • 太棒了!我会更新它作为答案。帮助我批准,以便其他人也可以受益

标签: jenkins jenkins-pipeline jgitflow-maven-plugin


【解决方案1】:

在某些情况下,管道脚本生成的输出很棘手,不能按原样使用。使用以下代码访问凭据:

withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: '<CREDENTIAL_ID>', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']])

而不是生成的代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-20
    • 2021-04-09
    • 1970-01-01
    • 2021-02-03
    • 2020-08-28
    • 2020-05-24
    • 1970-01-01
    相关资源
    最近更新 更多