9d13003281c136f435630327790c3d7a16dc8f33
[releng/global-jjb.git] / shell / maven-sonar.sh
1 #!/bin/bash -l
2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2017 The Linux Foundation and others.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Eclipse Public License v1.0
8 # which accompanies this distribution, and is available at
9 # http://www.eclipse.org/legal/epl-v10.html
10 ##############################################################################
11 echo "---> maven-sonar.sh"
12 # This script builds a Maven project and deploys it into a staging repo which
13 # can be used to deploy elsewhere later eg. Nexus staging / snapshot repos.
14
15 # DO NOT enable -u because $MAVEN_PARAMS and $MAVEN_OPTIONS could be unbound.
16 # Ensure we fail the job if any steps fail.
17 set -xe -o pipefail
18 set +u
19
20 export MAVEN_OPTS
21
22 declare -a params
23 params+=("--global-settings $GLOBAL_SETTINGS_FILE")
24 params+=("--settings $SETTINGS_FILE")
25
26 # Disable SC2086 because we want to allow word splitting for $MAVEN_* parameters.
27 # shellcheck disable=SC2086
28 _JAVA_OPTIONS="$JAVA_OPTS" $MVN $MAVEN_GOALS \
29     -e -Dsonar \
30     ${params[*]} \
31     $MAVEN_OPTIONS $MAVEN_PARAMS
32
33 if [ "$SONAR_HOST_URL" = "https://sonarcloud.io" ]; then
34     params+=("-Dsonar.projectKey=$PROJECT_KEY")
35     params+=("-Dsonar.organization=$PROJECT_ORGANIZATION")
36     params+=("-Dsonar.login=$API_TOKEN")
37     if [ "$SCAN_DEV_BRANCH" = "True" ]; then
38         echo "Will scan short lived branch ..."
39         # shellcheck disable=SC2236
40         if [ ! -z ${GERRIT_CHANGE_NUMBER+x} ]; then
41             GERRIT_SHORT_LIVED_BRANCH=${GERRIT_CHANGE_NUMBER}-${GERRIT_PATCHSET_NUMBER}
42             lowercase_SONARCLOUD_QUALITYGATE_WAIT=$(echo "$SONARCLOUD_QUALITYGATE_WAIT" | tr '[:upper:]' '[:lower:]')
43             params+=("-Dsonar.analysis.gerritProjectName=$PROJECT")
44             params+=("-Dsonar.branch.target=$GERRIT_BRANCH")
45             params+=("-Dsonar.branch.name=$GERRIT_SHORT_LIVED_BRANCH")
46             params+=("-Dsonar.qualitygate.wait=$lowercase_SONARCLOUD_QUALITYGATE_WAIT")
47         fi
48     fi
49 fi
50
51 if [ "$SET_JDK_VERSION" != "$SONARCLOUD_JAVA_VERSION" ]; then
52     export SET_JDK_VERSION="$SONARCLOUD_JAVA_VERSION"
53     bash <(curl -s https://raw.githubusercontent.com/lfit/releng-global-jjb/master/shell/update-java-alternatives.sh)
54     source /tmp/java.env
55 fi
56
57 # Disable SC2086 because we want to allow word splitting for $MAVEN_* parameters.
58 # shellcheck disable=SC2086
59 "$MVN" $SONAR_MAVEN_GOAL \
60     -e -Dsonar -Dsonar.host.url="$SONAR_HOST_URL" \
61     ${params[*]} \
62     $MAVEN_OPTIONS $MAVEN_PARAMS