diff --git a/README.md b/README.md index e9b52ea..539577a 100644 --- a/README.md +++ b/README.md @@ -77,4 +77,38 @@ script: Note that any of the above environment variables can be set in Travis, and do not need to be included in your .travis.yml. `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` should always be set to your S3 bucket credentials as environment variables in Travis, not this file. +## SonarQube +Included in the repository is a `sonarqube.sh` execution script. This script will execute Gradle builds using SonarQube +analysis either as a PR or as a general version build. Due to the added overhead Sonarqube adds to the build, its recommended to +configure Travis to run the analysis after the general build and deployment. + +**Note:** Usage of this script requires an administrator setting the `SONAR_TOKEN` environment variable in your Travis +configuration. This token can be obtained via your associated SonarCloud console. If the token is not present, the build +will exit with status 1. + +Here is an example `travis.yml` configuration using Sonarqube: + +```yaml +language: java + +branches: + only: + - develop + - /^v\d.*$/ + +jdk: + - openjdk8 + +install: true + +env: + global: + - DEPLOY_SOURCE_DIR=site/build/libs + +before_script: + - git clone https://github.com/perfectsense/travis-s3-deploy.git + +script: + - ./travis-s3-deploy/build-gradle.sh && ./travis-s3-deploy/deploy.sh && ./travis-s3-deploy/sonarqube.sh +``` diff --git a/sonarqube.sh b/sonarqube.sh new file mode 100755 index 0000000..03991c0 --- /dev/null +++ b/sonarqube.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +set -e + +# Set a default JAVA_TOOL_OPTIONS if it hasn't already been specified in .travis.yml +export JAVA_TOOL_OPTIONS="${JAVA_TOOL_OPTIONS:--Xmx4096m}" + +if [[ -z "${SONAR_TOKEN:-}" ]]; then + echo "SonarCloud token not present in the environment. Aborting." + exit 1 +fi + +echo "======================================" +echo "Running SonarQube Analysis" +echo "======================================" + +if [[ "$TRAVIS_PULL_REQUEST" != "false" ]]; then + ./gradlew sonarqube -i -Dsonar.pullrequest.key="$TRAVIS_PULL_REQUEST" +else + version=$(git describe --tags --match "v[0-9]*" --abbrev=0 HEAD || echo "0") + version=${version/v/} + echo "Creating a new analysis for version ${version}" + ./gradlew sonarqube -i -Dsonar.projectVersion="${version}" +fi