Commit 5c0eb785 authored by Protocole's avatar Protocole
Browse files

Merge branch 'feat/gradle_sonarqube' into 'latest'

[New job] 🔎 Gradle with sonarqube to check code quality

See merge request r2devops/hub!216
parents 2fd06ab2 7d8dab64
Loading
Loading
Loading
Loading
+93 −0
Original line number Diff line number Diff line
## Objective

This job allows you to check your code with SonarQube plugin in a 🐘 Gradle project.

## How to use it

1. Ensure that your project have
   [`build.gradle.kts`](https://docs.gradle.org/current/samples/sample_building_java_applications.html){:target="_blank"} 
   file
1. Configure the [SonarQube plugin](https://plugins.gradle.org/plugin/org.sonarqube){:target="_blank"} in your `build.gradle` file
1. Add this job URL inside the `include` list of your `.gitlab-ci.yml` file (see the [quick setup](/use-the-hub/#quick-setup)). You can specify [a fixed version](#changelog) instead of `latest`.
    ```yaml
      - remote: 'https://jobs.r2devops.io/latest/gradle_build.yml'
    ```
1. Well done, your job is ready to work ! 😀

## Job details

* Job name: `gradle_sonarqube`
* Default stage: `static_tests`
* Docker image: [`gradle:jdk11`](https://hub.docker.com/_/gradle){:target="_blank"}
* When: `always`


### Variables

| Name | Description | Default |
| ---- | ----------- | ------- |
| SONAR_URL | URL of Sonar instance  | sonarcloud.io |
| SONAR_TOKEN | API token from your Sonar account to be able to publish report | |
| SONAR_ORG | Organization to which project belongs  | |
| SONAR_PROJECT | Project name in Sonar instance | |
| COVERAGE_PLUGIN | Plugin to use for code coverage analysis | jacoco |
| JSON_MODE | Format to publish report to integrated with Gitlab MR for instance | CODECLIMATE |

### Example of build.gradle.kts file

Following example of `build.gradle.kts` file describes a very simple example of project configuration.
This can easily be generated with the `gradle init` command.

***For a Java project***

```kotlin
import org.gradle.api.JavaVersion.VERSION_11

plugins {
    java
    // Quality control
    id("org.sonarqube") version "3.1.1"    
}

group = "io.r2devops"
version = "1.0.0-SNAPSHOT"
java.sourceCompatibility = VERSION_11
java.targetCompatibility = VERSION_11

repositories {
    jcenter()
    mavenCentral()
}
```

***For a Kotlin project***

```kotlin
import org.gradle.api.JavaVersion.VERSION_11

plugins {
    id("org.jetbrains.kotlin.jvm") version "1.4.21"
    // Quality control
    id("org.sonarqube") version "3.1.1"
}

group = "io.r2devops"
version = "1.0.0-SNAPSHOT"
java.sourceCompatibility = VERSION_11
java.targetCompatibility = VERSION_11
kotlin.target { VERSION_11 }

repositories {
    jcenter()
    mavenCentral()
}

dependencies {
    // Align versions of all Kotlin components
    implementation(platform("org.jetbrains.kotlin:kotlin-bom"))

    // Use the Kotlin JDK 8 standard library.
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
}

```
+24 −0
Original line number Diff line number Diff line
# Job from R2Devops hub --> r2devops.io

stages:
  - static_tests

# Code quality with SonarQube
gradle_sonarqube:
  stage: static_tests
  image: gradle:jdk11
  script:
    gradle sonarqube -Dsonar.host.url=$SONAR_URL -Dsonar.login=$SONAR_TOKEN -Dsonar.projectKey=$SONAR_PROJECT -Dsonar.organization=$SONAR_ORG -Dsonar.branch.name=$CI_COMMIT_REF_NAME -Dsonar.gitlab.json_mode=$JSON_MODE -Dsonar.java.coveragePlugin=$COVERAGE_PLUGIN
  variables:
    SONAR_URL: 'https://sonarcloud.io'
    SONAR_TOKEN: ''
    SONAR_PROJECT: ''
    SONAR_ORG: ''
    COVERAGE_PLUGIN: 'jacoco'
    JSON_MODE: CODECLIMATE
  cache:
    key: "$CI_COMMIT_REF_NAME"
    policy: pull
    paths:
      - build
      - .gradle
+15 −0
Original line number Diff line number Diff line
name: gradle_sonarqube
description: Check your code quality with SonarQube
default_stage: static_tests
icon: 💅
maintainer: yodamad
license: MIT
images:
  "gradle": "jdk11"
tools:
  "Gradle": "$GRADLE_VERSION"
labels:
    - GitLab
    - Gradle
    - Code quality
    - SonarQube
+0 −0

Empty file added.

+121 KiB
Loading image diff...
Loading