From 96f71cea2304da35db5c54b324019b027988071d Mon Sep 17 00:00:00 2001 From: Thanh Ha Date: Wed, 6 Feb 2019 19:32:00 -0500 Subject: [PATCH] Add support for SonarCloud If SonarCloud is used the old method is automatically disabled so that Sonar is not run twice. Example Usage: - project: name: yangtools-sonar jobs: - gerrit-maven-sonar project: 'yangtools' project-name: 'yangtools' branch: 'master' mvn-settings: 'yangtools-settings' mvn-opts: '-Xmx1024m' sonarcloud: true sonarcloud-project-key: KEY sonarcloud-project-organization: ORGANIZATION sonarcloud-api-token: TOKEN Issue: RELENG-1721 Change-Id: Ica1cd29bfc9246f2452370dd6ceb8b7e5b8e5329 Signed-off-by: Thanh Ha --- .jjb-test/lf-maven-jobs/maven-sonarcloud.yaml | 15 ++++++ docs/jjb/lf-maven-jobs.rst | 30 ++++++++++++ jjb/lf-maven-jobs.yaml | 56 ++++++++++++++++++++-- .../notes/sonarcloud-support-7f0d1dcacff44ba4.yaml | 5 ++ shell/maven-sonar.sh | 18 +++++-- 5 files changed, 115 insertions(+), 9 deletions(-) create mode 100644 .jjb-test/lf-maven-jobs/maven-sonarcloud.yaml create mode 100644 releasenotes/notes/sonarcloud-support-7f0d1dcacff44ba4.yaml diff --git a/.jjb-test/lf-maven-jobs/maven-sonarcloud.yaml b/.jjb-test/lf-maven-jobs/maven-sonarcloud.yaml new file mode 100644 index 00000000..e367729b --- /dev/null +++ b/.jjb-test/lf-maven-jobs/maven-sonarcloud.yaml @@ -0,0 +1,15 @@ +--- +- project: + name: example-sonarcloud + jobs: + - gerrit-maven-sonar + + project: 'sonarcloud' + project-name: 'sonarcloud' + branch: 'master' + mvn-settings: 'sonarcloud-settings' + mvn-opts: '-Xmx1024m' + sonarcloud: true + sonarcloud-project-key: KEY + sonarcloud-project-organization: ORGANIZATION + sonarcloud-api-token: TOKEN diff --git a/docs/jjb/lf-maven-jobs.rst b/docs/jjb/lf-maven-jobs.rst index 25258aef..aca0f5f6 100644 --- a/docs/jjb/lf-maven-jobs.rst +++ b/docs/jjb/lf-maven-jobs.rst @@ -27,6 +27,20 @@ Runs Sonar against a Maven project. :mvn-version: Version of Maven to execute Sonar with. :mvn-settings: Maven settings.xml file containing credentials to use. +lf-infra-maven-sonarcloud +------------------------- + +Runs Sonar against a Maven project and pushes results to SonarCloud. + +:Required Parameters: + + :java-version: Version of Java to execute Sonar with. + :mvn-version: Version of Maven to execute Sonar with. + :mvn-settings: Maven settings.xml file containing credentials to use. + :sonarcloud-project-key: SonarCloud project key. + :sonarcloud-project-organization: SonarCloud project organization. + :sonarcloud-api-token: SonarCloud API Token. + lf-maven-build -------------- @@ -324,6 +338,8 @@ directory is then used later to deploy to Nexus. :gerrit_release_triggers: Override Gerrit Triggers. +.. _maven-sonar: + Maven Sonar ----------- @@ -363,6 +379,14 @@ interest in that kind of support. :mvn-opts: Sets MAVEN_OPTS. (default: '') :mvn-params: Additional mvn parameters to pass to the cli. (default: '') :mvn-version: Version of maven to use. (default: mvn35) + :sonar-mvn-goals: Maven goals to run for sonar analysis. + (default: sonar:sonar) + :sonarcloud: Whether or not to use SonarCloud ``true|false``. + (default: false) + :sonarcloud-project-key: SonarCloud project key. (default: '') + :sonarcloud-project-organization: SonarCloud project organization. + (default: '') + :sonarcloud-api-token: SonarCloud API Token. (default: '') :stream: Keyword that can be used to represent a release code-name. Often the same as the branch. (default: master) :submodule-recursive: Whether to checkout submodules recursively. @@ -372,6 +396,12 @@ interest in that kind of support. :gerrit_sonar_triggers: Override Gerrit Triggers. + +SonarCloud Example: + +.. literalinclude:: ../../.jjb-test/lf-maven-jobs/maven-sonarcloud.yaml + :language: yaml + Maven Verify ------------ diff --git a/jjb/lf-maven-jobs.yaml b/jjb/lf-maven-jobs.yaml index 9c8b953e..46a078a0 100644 --- a/jjb/lf-maven-jobs.yaml +++ b/jjb/lf-maven-jobs.yaml @@ -817,6 +817,10 @@ mvn-params: '' mvn-version: mvn35 sonar-mvn-goal: 'sonar:sonar' + sonarcloud: false + sonarcloud-project-key: '' + sonarcloud-project-organization: '' + sonarcloud-api-token: '' stream: master submodule-recursive: true submodule-timeout: 10 @@ -871,10 +875,31 @@ builders: - lf-infra-pre-build - - lf-infra-maven-sonar: - java-version: '{java-version}' - mvn-settings: '{mvn-settings}' - mvn-version: '{mvn-version}' + # With SonarCloud + - conditional-step: + condition-kind: boolean-expression + condition-expression: '{sonarcloud}' + steps: + - shell: echo 'Using SonarCloud' + - lf-infra-maven-sonarcloud: + java-version: '{java-version}' + mvn-settings: '{mvn-settings}' + mvn-version: '{mvn-version}' + sonarcloud-project-key: '{sonarcloud-project-key}' + sonarcloud-project-organization: '{sonarcloud-project-organization}' + sonarcloud-api-token: '{sonarcloud-api-token}' + # With SonarQube + - conditional-step: + condition-kind: not + condition-operand: + condition-kind: boolean-expression + condition-expression: '{sonarcloud}' + steps: + - shell: echo 'Using SonarQube' + - lf-infra-maven-sonar: + java-version: '{java-version}' + mvn-settings: '{mvn-settings}' + mvn-version: '{mvn-version}' publishers: - lf-jacoco-report @@ -900,6 +925,29 @@ - ../shell/maven-sonar.sh - lf-provide-maven-settings-cleanup +- builder: + name: lf-infra-maven-sonarcloud + # Run a Sonar build with Maven + builders: + - lf-maven-install: + mvn-version: '{mvn-version}' + - lf-update-java-alternatives: + java-version: '{java-version}' + - inject: + # Switch this to the sonar wrapper when JJB 2.0 is available + properties-content: | + SONAR_HOST_URL=https://sonarcloud.io + PROJECT_KEY={sonarcloud-project-key} + PROJECT_ORGANIZATION={sonarcloud-project-organization} + API_TOKEN={sonarcloud-api-token} + - lf-provide-maven-settings: + global-settings-file: global-settings + settings-file: '{mvn-settings}' + - shell: !include-raw-escape: + - ../shell/common-variables.sh + - ../shell/maven-sonar.sh + - lf-provide-maven-settings-cleanup + - job-template: name: '{project-name}-sonar' id: gerrit-maven-sonar diff --git a/releasenotes/notes/sonarcloud-support-7f0d1dcacff44ba4.yaml b/releasenotes/notes/sonarcloud-support-7f0d1dcacff44ba4.yaml new file mode 100644 index 00000000..ba8b4676 --- /dev/null +++ b/releasenotes/notes/sonarcloud-support-7f0d1dcacff44ba4.yaml @@ -0,0 +1,5 @@ +--- +features: + - | + Add support for pushing Sonar results to SonarCloud. Refer to + :ref:`Maven Sonar docs ` for details. diff --git a/shell/maven-sonar.sh b/shell/maven-sonar.sh index 58572341..ae280b28 100644 --- a/shell/maven-sonar.sh +++ b/shell/maven-sonar.sh @@ -19,18 +19,26 @@ set +u export MAVEN_OPTS +declare -a params +params+=("--global-settings $GLOBAL_SETTINGS_FILE") +params+=("--settings $SETTINGS_FILE") + # Disable SC2086 because we want to allow word splitting for $MAVEN_* parameters. # shellcheck disable=SC2086 $MVN clean install \ -e -Dsonar \ - --global-settings "$GLOBAL_SETTINGS_FILE" \ - --settings "$SETTINGS_FILE" \ + ${params[*]} \ $MAVEN_OPTIONS $MAVEN_PARAMS +if [ "$SONAR_HOST_URL" = "https://sonarcloud.io" ]; then + params+=("-Dsonar.projectKey=$PROJECT_KEY") + params+=("-Dsonar.organization=$PROJECT_ORGANIZATION") + params+=("-Dsonar.login=$API_TOKEN") +fi + # Disable SC2086 because we want to allow word splitting for $MAVEN_* parameters. # shellcheck disable=SC2086 -$MVN $SONAR_MAVEN_GOAL \ +"$MVN" $SONAR_MAVEN_GOAL \ -e -Dsonar -Dsonar.host.url="$SONAR_HOST_URL" \ - --global-settings "$GLOBAL_SETTINGS_FILE" \ - --settings "$SETTINGS_FILE" \ + ${params[*]} \ $MAVEN_OPTIONS $MAVEN_PARAMS -- 2.16.6