【问题标题】:Jenkins pipeline, pass branchname to gradle buildJenkins管道,将分支名称传递给gradle build
【发布时间】:2018-06-27 08:12:09
【问题描述】:

多分支 jenkins 管道调用 gradle build。
需要将 BRANCH_NAME 传递给 build.gradle 中的 sonarqube 任务:

sonarqube {
  properties {
    property "sonar.host.url", "https://sonar"
    property "sonar.projectKey", "com.foo.bar:Foobar"
    property "sonar.projectName", "com.foo.bar-Foobar"
    property "sonar.binaries", "build"
    property "sonar.branch.name", "${branchname}" <---------------
    property "sonar.branch.target", "master"
  }
}

我试过了:

  stage('Sonarscan') {
    withSonarQubeEnv('SONAR') {
      bat "gradlew -Pbranchname=${BRANCH_NAME} sonarqube --info"
    }
  }

输出:

* What went wrong:
A problem occurred evaluating root project 'fooBar'.
> Cannot get property 'branchname' on extra properties extension as it does not exist

  stage('Sonarscan') {
    withSonarQubeEnv('SONAR') {
      bat "gradlew -Dbranchname=${BRANCH_NAME} sonarqube --info"
    }
  }

输出:

* What went wrong:
A problem occurred evaluating root project 'fooBar'.
> Could not get unknown property 'branchname' for root project 'sfrToolbarContract' of type org.gradle.api.Project.

还有:

  stage('Sonarscan') {
    withSonarQubeEnv('SONAR') {
      def branchname = "${BRANCH_NAME}"
      bat "gradlew sonarqube --info"
    }
  }

但管道作业只是挂起。
如何将 Jenkinsfile 中的属性传递给 gradle build ?

【问题讨论】:

    标签: jenkins gradle groovy sonarqube jenkins-pipeline


    【解决方案1】:

    我觉得可以直接在命令行传递SonarQube参数:

    stage('Sonarscan') {
      withSonarQubeEnv('SONAR') {
        bat "gradlew sonarqube -Dsonar.branch.name=${BRANCH_NAME} --info"
      }
    }
    

    【讨论】:

    • 需要注意的是分支插件必须安装,否则你会得到:验证项目反应器失败:要使用属性“sonar.branch.name”,分支插件是必需的但不是安装。查看分支支持的文档:redirect.sonarsource.com/doc/branches.html.
    猜你喜欢
    • 2017-10-10
    • 2015-12-23
    • 1970-01-01
    • 1970-01-01
    • 2018-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多