Skip to content

CLI

sonar-cli exposes every service and method of the SDK as a subcommand:

sonar-cli [global flags] <service> <method> [flags]

The list of services, and of the methods of each one, is on the services page. --help works at every level:

sonar-cli --help
sonar-cli projects --help
sonar-cli issues search --help

Authentication

Authentication is passed through global flags, or through environment variables (the recommended way in scripts and CI). The --url is mandatory.

FlagEnvironment variableDescription
--urlSONAR_CLI_URLSonarQube server URL
--tokenSONAR_CLI_TOKENAuthentication token (recommended)
--usernameSONAR_CLI_USERNAMEUsername for basic authentication
--passwordSONAR_CLI_PASSWORDPassword for basic authentication
# Using flags
sonar-cli --url https://sonar.example.com --token mytoken projects search

# Using environment variables
export SONAR_CLI_URL=https://sonar.example.com
export SONAR_CLI_TOKEN=mytoken
sonar-cli projects search
Values passed on the command line are visible to the other users of the machine (in ps, in the shell history): prefer the environment variables for the token and the password.

Other global flags

FlagDefaultDescription
--outputjsonOutput format: json, yaml or table
--timeout30sHTTP request timeout (Go duration)

Examples

# List all projects
sonar-cli projects search

# Search for critical issues in a project
sonar-cli issues search --projects my-project --severities CRITICAL,MAJOR

# Get the quality gate status of a project
sonar-cli qualitygates project-status --project-key my-project

# Create a user token
sonar-cli user-tokens generate --login john --name "ci-token"

# Search rules
sonar-cli rules search --languages go --severities MAJOR

# Get the system health
sonar-cli system health

Output formats

# JSON (default), for jq and scripting
sonar-cli projects search --output json | jq '.components[].key'

# YAML
sonar-cli projects search --output yaml

# Table, for a quick look in the terminal
sonar-cli projects search --output table
KEY              NAME             QUALIFIER  VISIBILITY
my-project       My Project       TRK        public
another-project  Another Project  TRK        private

Pagination

Methods returning paginated results accept --all, which fetches every page and merges the results:

# First page only (default)
sonar-cli issues search --projects my-project

# Every page
sonar-cli issues search --projects my-project --all

# Manual pagination
sonar-cli projects search --page 2 --page-size 50

Method flags are named after the fields of the SDK options (PageSize becomes --page-size), so the API reference also documents the flags of every method.

Shell completion

Services, methods and flags can be completed with Tab:

# Bash
sonar-cli completion bash > ~/.local/share/bash-completion/completions/sonar-cli

# Zsh
sonar-cli completion zsh > "${fpath[1]}/_sonar-cli"

# Fish
sonar-cli completion fish > ~/.config/fish/completions/sonar-cli.fish

PowerShell:

sonar-cli completion powershell | Out-String | Invoke-Expression