Skip to content
Snippets Groups Projects
Commit a0e34f49 authored by Pierre Smeyers's avatar Pierre Smeyers
Browse files

feat: manage separate $SONAR_PROJECT_KEY and $SONAR_PROJECT_NAME

parent 7052db8b
Branches
Tags
No related merge requests found
......@@ -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
......@@ -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
},
{
......
......@@ -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]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment