Skip to content

Instantly share code, notes, and snippets.

@lobbin
Last active February 28, 2022 08:00
Show Gist options
  • Save lobbin/62f6f8f8e696b700dfdf5dfe2143d200 to your computer and use it in GitHub Desktop.
Save lobbin/62f6f8f8e696b700dfdf5dfe2143d200 to your computer and use it in GitHub Desktop.
# NOTE: You also need to a service connection under project settings.
# Server Url: http://localhost:9000
# Token: Anything (will be replaced further down)
#
# In <FIXME:uuid> below, add the uuid of the added service connection.
# There's also <Name>, <Org> and <Project> to be replaced.
#
# NOTE: A lot of things bearing when running this in a PR pipeline.
# Especially publishing and reporting.
#
stages:
- stage:
dependsOn: []
displayName: SonarQube Analyze
jobs:
- job:
steps:
- script: |
docker run -d --name sonarqube -e SONAR_ES_BOOTSTRAP_CHECKS_DISABLE=true -p 9000:9000 mc1arke/sonarqube-with-community-branch-plugin:lts
displayName: Starting SonarQube
- task: NodeTool@0
inputs:
versionSpec: 12.x
displayName: Install Node.js
- script: |
npm install -g json
displayName: Running npm install
- script: |
x=1
while [ $x -le 10 ]
do
TOKEN=`curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "name=token" -u admin:admin localhost:9000/api/user_tokens/generate 2>/dev/null`
RETVAL=$?
if [ $RETVAL -eq 0 ] && [[ "$TOKEN" != *"errors"* ]]; then
TOKEN=`echo $TOKEN | json token`
echo "##vso[task.setvariable variable=SQ_TOKEN]$TOKEN"
echo "##vso[task.setendpoint id=<FIXME:uuid>;field=authParameter;key=username]$TOKEN"
break
fi
x=$(( $x + 1 ))
sleep 10
done
curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "name=<Name>&organisation=<Org>&project=<Project>" -u admin:admin localhost:9000/api/projects/create 2>/dev/null
displayName: Waiting for SonarQube to come alive
- task: SonarQubePrepare@5
inputs:
SonarQube: 'SonarQube on Localhost'
scannerMode: 'CLI'
configMode: 'manual'
cliProjectKey: '<Project>'
cliSources: 'src'
extraProperties: |
sonar.tests=src/test
sonar.test.inclusions=**/*.steps.js
sonar.log.level=debug
- task: SonarQubeAnalyze@5
- task: SonarQubePublish@5
inputs:
pollingTimeoutSec: '300'
- script: |
wget https://github.com/cnescatlab/sonar-cnes-report/releases/download/4.0.0/sonar-cnes-report-4.0.0.jar 2>/dev/null
FULL_BRANCH=$(Build.SourceBranch)
BRANCH=`echo "${FULL_BRANCH/refs\/heads\//}"`
mkdir sonarqube-analysis-report
java -jar sonar-cnes-report-4.0.0.jar -m -f -b "$BRANCH" -p <Project> -t $(SQ_TOKEN) -o sonarqube-analysis-report
rm -rf sonarqube-analysis-report/conf
displayName: Generating SonarQube reports
- task: PublishPipelineArtifact@1
inputs:
targetPath: '$(System.DefaultWorkingDirectory)/sonarqube-analysis-report'
artifact: 'SonarQube Analysis Report'
publishLocation: 'pipeline'
displayName: Publishing SonarQube report
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment