Skip to content

Commit

Permalink
SC-1063 provide orb that adds support for SonarCloud in CircleCI
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-vanbraband-sonarsource authored and Janos Gyerik committed Oct 9, 2019
1 parent 6bb66df commit 0d45fc5
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 0 deletions.
47 changes: 47 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
version: 2.1
orbs:
orb-tools: circleci/[email protected]
sonarcloud: sonarsource/sonarcloud@dev:alpha
jobs:
test:
parameters:
image:
type: string
docker:
- image: <<parameters.image>>:latest
steps:
- checkout
- sonarcloud/scan
workflows:
publish-dev:
jobs:
- orb-tools/pack:
source-dir: src/main
destination-orb-path: orb.yml
workspace-path: orb.yml
artifact-path: orb.yml
- orb-tools/publish-dev:
context: Publishing Orb
orb-name: sonarsource/sonarcloud
requires: [orb-tools/pack]
- test:
name: test-node
image: node
context: SonarCloud Analysis
requires: [orb-tools/publish-dev]
- test:
name: test-circleci-python
image: circleci/python
context: SonarCloud Analysis
requires: [orb-tools/publish-dev]
- orb-tools/publish:
context: Publishing Orb
orb-ref: sonarsource/[email protected]
attach-workspace: true
requires: [test-node, test-circleci-python]
filters:
branches:
only: master



44 changes: 44 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Eclipse
.classpath
.project
.settings

# Gradle
build/
.gradle/
.gradletasknamecache
classes/
out/

# IntelliJ
*.iws
*.iml
*.ipr
.idea/

# Maven
target/

# Mac OS X
.DS_Store
.AppleDouble
.LSOverride
Icon
._*
.Spotlight-V100
.Trashes

# MSWindows
Thumbs.db
ehthumbs.db
Desktop.ini
$RECYCLE.BIN/

# NPM/yarn
npm-debug.log*
npm.tar.gz
yarn-debug.log*
yarn-error.log*

# SonarQube/SonarCloud
.sonar/
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,28 @@
# sonarcloud-circleci-orb
Support of SonarScanner CLI in CircleCI

## SonarCloud Orb
The SonarCloud Orb can be used with any linux based docker image that includes the command line tools `curl` and `unzip`.

To connect to your SonarCloud project on `sonarcloud.io` you need to setup an api token. We recommend to setup a CircleCI context in your organization named `sonarcloud` that contains a variable with key `SONAR_TOKEN` and the api token as the value.
### Usage examples
```yaml
version: 2.1
orbs:
sonarcloud: sonarsource/[email protected]
jobs:
build:
docker:
- image: 'circleci/python:3.7.4'
steps:
- checkout
- sonarcloud/scan
workflows:
my-workflow:
jobs:
- build:
context: sonarcloud
```
### Publishing a new version
To publish a new version of the orb you need to change the version found in .circleci/config.yml to the new version. The CirceCI build pipeline will then publish the new version of the orb.
5 changes: 5 additions & 0 deletions sonar-project.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sonar.projectKey=SonarSource_sonarcloud-circleci-orb
sonar.projectName=sonarcloud-circleci-orb
sonar.projectVersion=1.0
sonar.sources=.
sonar.organization=sonarsource
2 changes: 2 additions & 0 deletions src/main/@orb.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
version: 2.1
description: Detect bugs and vulnerabilities in your repository
42 changes: 42 additions & 0 deletions src/main/commands/scan.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
description: Detect bugs and vulnerabilities
parameters:
sonar_token_variable_name:
description: the name of the environment variable where the SonarCloud API token is stored
default: SONAR_TOKEN
type: env_var_name
cache_version:
description: increment this value if the cache is corrupted and you want to start with a clean cache
default: 1
type: integer
steps:
- run:
name: Create cache directory if it doesn't exist
command: mkdir -p /tmp/cache/scanner
- restore_cache:
keys:
- v<<parameters.cache_version>>-sonarcloud-scanner-4.1.0.1829
- run:
name: SonarCloud
command: |
set -e
VERSION=4.1.0.1829
SONAR_TOKEN=$<<parameters.sonar_token_variable_name>>
SCANNER_DIRECTORY=/tmp/cache/scanner
export SONAR_USER_HOME=$SCANNER_DIRECTORY/.sonar
OS="linux"
echo $SONAR_USER_HOME
if [[ ! -x "$SCANNER_DIRECTORY/sonar-scanner-$VERSION-$OS/bin/sonar-scanner" ]]; then
curl -Ol https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-$VERSION-$OS.zip
unzip -qq -o sonar-scanner-cli-$VERSION-$OS.zip -d $SCANNER_DIRECTORY
fi
chmod +x $SCANNER_DIRECTORY/sonar-scanner-$VERSION-$OS/bin/sonar-scanner
chmod +x $SCANNER_DIRECTORY/sonar-scanner-$VERSION-$OS/jre/bin/java
$SCANNER_DIRECTORY/sonar-scanner-$VERSION-$OS/bin/sonar-scanner
environment:
SONARQUBE_SCANNER_PARAMS: '{"sonar.host.url":"https://dogfood-core.sc-dev.io"}'
- save_cache:
key: v<<parameters.cache_version>>-sonarcloud-scanner-4.1.0.1829
paths: /tmp/cache/scanner
17 changes: 17 additions & 0 deletions src/main/examples/scan-docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
description: Use the sonarcloud orb to detect bugs and vulnerabilities in your repository
usage:
version: 2.1
jobs:
build:
docker:
- image: node:latest
steps:
- checkout
- sonarcloud/scan
orbs:
sonarcloud: sonarsource/[email protected]
workflows:
main:
jobs:
- build:
context: sonarcloud

0 comments on commit 0d45fc5

Please sign in to comment.