Jenkins管道,将branchname传递给gradle构建

问题描述 投票:2回答:1

multibranch jenkins管道调用gradle构建。 需要将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构建?

jenkins gradle groovy sonarqube jenkins-pipeline
1个回答
2
投票

我想你可以直接在命令行上传递SonarQube参数:

stage('Sonarscan') {
  withSonarQubeEnv('SONAR') {
    bat "gradlew sonarqube -Dsonar.branch.name=${BRANCH_NAME} --info"
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.