diff --git a/README.md b/README.md index 9598700576ddb6ec7dc6c689a561ecb59af0931d..8c9cc60100010561077f9dcd966608071f41fb1a 100644 --- a/README.md +++ b/README.md @@ -26,10 +26,12 @@ It is bound to the `test` stage, and uses the following variables: | ------------------------ | ------------------------------- | ----------------------------- | | `SONAR_SCANNER_IMAGE` | The Docker image used to run [sonar-scanner](https://docs.sonarqube.org/latest/analysis/scan/sonarscanner/) | `sonarsource/sonar-scanner-cli:latest` | | `SONAR_HOST_URL` | SonarQube server url | _none_ (disabled) | +| `SONAR_PROJECT_KEY` | SonarQube Project Key (might also be set in the `sonar-project.properties` file) | fallbacks to `$CI_PROJECT_PATH_SLUG` (see below) | +| `SONAR_PROJECT_NAME` | SonarQube Project Name (might also be set in the `sonar-project.properties` file) | fallbacks to `$CI_PROJECT_PATH` (see below) | | :lock: `SONAR_TOKEN` | SonarQube authentication [token](https://docs.sonarqube.org/latest/user-guide/user-token/) (depends on your authentication method) | _none_ | | :lock: `SONAR_LOGIN` | SonarQube login (depends on your authentication method) | _none_ | | :lock: `SONAR_PASSWORD` | SonarQube password (depends on your authentication method) | _none_ | -| `SONAR_BASE_ARGS` | SonarQube [analysis arguments](https://docs.sonarqube.org/latest/analysis/analysis-parameters/) | `-Dsonar.projectKey=${CI_PROJECT_PATH_SLUG} -Dsonar.projectName=${CI_PROJECT_PATH} -Dsonar.projectBaseDir=. -Dsonar.links.homepage=${CI_PROJECT_URL} -Dsonar.links.ci=${CI_PROJECT_URL}/-/pipelines -Dsonar.links.issue=${CI_PROJECT_URL}/-/issues` | +| `SONAR_BASE_ARGS` | SonarQube [analysis arguments](https://docs.sonarqube.org/latest/analysis/analysis-parameters/) | `-Dsonar.links.homepage=${CI_PROJECT_URL} -Dsonar.links.ci=${CI_PROJECT_URL}/-/pipelines -Dsonar.links.issue=${CI_PROJECT_URL}/-/issues` | | :lock: `SONAR_GITLAB_TOKEN` | GitLab [access token](https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html) with `api` scope. When set, activates the [Sonar GitLab plugin](https://github.com/gabrie-allaigre/sonar-gitlab-plugin/#plugins-properties) integration. | _none_ | | `SONAR_BRANCH_ANALYSIS_DISABLED` | Set to `true` to disable automatic [Pull Request Analysis](https://docs.sonarqube.org/latest/analysis/pull-request/) and [Branch Analysis](https://docs.sonarqube.org/latest/branches/overview/) | _none_ (enabled) | | `SONAR_GITLAB_ARGS` | Extra arguments to use with [Sonar GitLab plugin](https://github.com/gabrie-allaigre/sonar-gitlab-plugin/#plugins-properties) | `-Dsonar.gitlab.url=${CI_SERVER_URL} -Dsonar.gitlab.user_token=${SONAR_GITLAB_TOKEN} -Dsonar.gitlab.project_id=${CI_PROJECT_ID} -Dsonar.gitlab.commit_sha=${CI_COMMIT_SHA} -Dsonar.gitlab.ref_name=${CI_COMMIT_REF_NAME}` | @@ -64,8 +66,13 @@ It will then simply append the `SONAR_GITLAB_ARGS` (overridable) to the SonarQub Comments added to GitLab will appear as owned by the user associated to the GitLab [access token](https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html). -### How should I configure other SonarQube arguments ? +### Configuring SonarQube project key, project name and other parameters -The simplest and recommended way to configure other SonarQube [analysis arguments](https://docs.sonarqube.org/latest/analysis/analysis-parameters/) -(and even [language specific args](https://docs.sonarqube.org/latest/analysis/languages/overview/)) is to use a -`sonar-project.properties` file at the root of your repository. +You shall define your SonarQube project key and project name in a `sonar-project.properties` file located at the root of your repository (as respectively `sonar.projectKey` and `sonar.projectName` entries), although they might alternately be set as `$SONAR_PROJECT_KEY` and `$SONAR_PROJECT_NAME` variables. + +Note that when not explictly set, the template will use `$CI_PROJECT_PATH_SLUG` and `$CI_PROJECT_PATH` as fallback project key and project name. + +The `sonar-project.properties` file is also the recommended way to configure other SonarQube [analysis parameters](https://docs.sonarqube.org/latest/analysis/analysis-parameters/) +as well as [language specific parameters](https://docs.sonarqube.org/latest/analysis/languages/overview/). + +Each _to-be-continuous_ build template shall briefly document the supported language-specific SonarQube parameters. \ No newline at end of file diff --git a/kicker.json b/kicker.json index 22f59322b5c30c7b25ede1be083e31859e91b8c7..94666c8f1e84b147e7dcbfe0bb445f75b2c6ea54 100644 --- a/kicker.json +++ b/kicker.json @@ -15,6 +15,16 @@ "description": "SonarQube server url", "mandatory": true }, + { + "name": "SONAR_PROJECT_KEY", + "description": "SonarQube Project Key (might also be set in the `sonar-project.properties` file)", + "advanced": true + }, + { + "name": "SONAR_PROJECT_NAME", + "description": "SonarQube Project Name (might also be set in the `sonar-project.properties` file)", + "advanced": true + }, { "name": "SONAR_TOKEN", "description": "SonarQube authentication [token](https://docs.sonarqube.org/latest/user-guide/user-token/) (depends on your authentication method)", @@ -33,7 +43,7 @@ { "name": "SONAR_BASE_ARGS", "description": "SonarQube [analysis arguments](https://docs.sonarqube.org/latest/analysis/analysis-parameters/)", - "default": "-Dsonar.projectKey=${CI_PROJECT_PATH_SLUG} -Dsonar.projectName=${CI_PROJECT_PATH} -Dsonar.projectBaseDir=. -Dsonar.links.homepage=${CI_PROJECT_URL} -Dsonar.links.ci=${CI_PROJECT_URL}/-/pipelines -Dsonar.links.issue=${CI_PROJECT_URL}/-/issues", + "default": "-Dsonar.links.homepage=${CI_PROJECT_URL} -Dsonar.links.ci=${CI_PROJECT_URL}/-/pipelines -Dsonar.links.issue=${CI_PROJECT_URL}/-/issues", "advanced": true }, { diff --git a/templates/gitlab-ci-sonar.yml b/templates/gitlab-ci-sonar.yml index fdcf668234f981668b55d4b1e4f8eab38de6a731..77e34dad01db54c4b11700fd965d994922b78ad6 100644 --- a/templates/gitlab-ci-sonar.yml +++ b/templates/gitlab-ci-sonar.yml @@ -51,9 +51,6 @@ variables: # see: https://docs.sonarqube.org/latest/analysis/analysis-parameters/ # default uses branch analysis: https://docs.sonarqube.org/latest/branches/overview/ SONAR_BASE_ARGS: >- - -Dsonar.projectKey=${CI_PROJECT_PATH_SLUG} - -Dsonar.projectName=${CI_PROJECT_PATH} - -Dsonar.projectBaseDir=. -Dsonar.links.homepage=${CI_PROJECT_URL} -Dsonar.links.ci=${CI_PROJECT_URL}/-/pipelines -Dsonar.links.issue=${CI_PROJECT_URL}/-/issues @@ -296,6 +293,13 @@ stages: fi } + # determines whether the given SonarQube param is defined + # either in the sonar-project.properties or in the $SONAR_BASE_ARGS variable + function has_sonar_param() { + sonar_param="$1" + grep -e "^sonar\.${sonar_param}[ \t]*[:=]" sonar-project.properties > /dev/null || echo "$SONAR_BASE_ARGS" | grep -e "-Dsonar\.${sonar_param}=" > /dev/null + } + unscope_variables # ENDSCRIPT @@ -323,20 +327,40 @@ sonar: - eval_java_proxy_args script: - sonar_autodetect_mr - - if [[ "$SONAR_GITLAB_TOKEN" ]]; then sonar_extra_args=$SONAR_GITLAB_ARGS; fi + - | + if [[ -z "$SONAR_PROJECT_KEY" ]] && ! has_sonar_param projectKey + then + log_info "No Sonar Project Key explicitly set: use default \\e[33;1m$CI_PROJECT_PATH_SLUG\\e[0m" + log_info "If you want another value, either set the 'sonar.projectKey' entry in your sonar-project.properties, or set it as \$SONAR_PROJECT_KEY" + export SONAR_PROJECT_KEY="$CI_PROJECT_PATH_SLUG" + fi + - | + if [[ -z "$SONAR_PROJECT_NAME" ]] && ! has_sonar_param projectName + then + log_info "No Sonar Project Name explicitly set: use default \\e[33;1m$CI_PROJECT_PATH\\e[0m" + log_info "If you want another value, either set the 'sonar.projectName' entry in your sonar-project.properties, or set it as \$SONAR_PROJECT_NAME" + export SONAR_PROJECT_NAME="$CI_PROJECT_PATH" + fi - | if [[ "$SONAR_URL" ]] && [[ -z "$SONAR_HOST_URL" ]] then - log_warn '$SONAR_URL variable defined: use $SONAR_HOST_URL instead (see doc)' + log_warn '$SONAR_URL variable detected: use $SONAR_HOST_URL instead (see doc)' export SONAR_HOST_URL="$SONAR_URL" fi - | if [[ "$SONAR_AUTH_TOKEN" ]] && [[ -z "$SONAR_TOKEN" ]] then - log_warn '$SONAR_AUTH_TOKEN variable defined: use $SONAR_TOKEN instead (see doc)' + log_warn '$SONAR_AUTH_TOKEN variable detected: use $SONAR_TOKEN instead (see doc)' export SONAR_TOKEN="$SONAR_AUTH_TOKEN" fi - - sonar-scanner ${TRACE+-Dsonar.verbose=true} $java_proxy_args ${SONAR_LOGIN+-Dsonar.login=$SONAR_LOGIN} ${SONAR_PASSWORD+-Dsonar.password=$SONAR_PASSWORD} $SONAR_BASE_ARGS $sonar_extra_args $sonar_mr_args + - >- + sonar-scanner ${TRACE+-Dsonar.verbose=true} $java_proxy_args + ${SONAR_LOGIN+-Dsonar.login=$SONAR_LOGIN} + ${SONAR_PASSWORD+-Dsonar.password=$SONAR_PASSWORD} + ${SONAR_PROJECT_KEY+-Dsonar.projectKey=$SONAR_PROJECT_KEY} + ${SONAR_PROJECT_NAME+-Dsonar.projectName=$SONAR_PROJECT_NAME} + ${SONAR_GITLAB_TOKEN+$SONAR_GITLAB_ARGS} + $SONAR_BASE_ARGS $sonar_mr_args rules: - !reference [.test-policy, rules]