Sonar 1 min read

Create and Rotate SonarQube Analysis Tokens

Create and Rotate SonarQube Analysis Tokens
Sonar · November 30, 2026

Version note: references describe SonarQube Server 2025.2 behaviour. Check your server, scanner and edition requirements. These examples were not run against a live SonarQube server.

What this guide covers

An analysis token lets a scanner send results without using your account password. Treat the token as a password and limit what it can access.

Before you start

You need permission to analyze a project and access to your pipeline’s secret settings. Use a project-specific analysis token when your SonarQube version supports it.

Step 1: Create a limited token

Create a token for the intended project. Set an expiry date and record who will replace it. Do not use an administrator’s broad account token for a routine scan.

Step 2: Store it as a secret

Save SONAR_TOKEN and the intended SONAR_HOST_URL in the pipeline configuration. Do not write the token in a properties file, workflow source or command output. Run:

set +x
set -eu
: "${SONAR_HOST_URL:?Configure the approved SonarQube URL}"
: "${SONAR_TOKEN:?Inject a project analysis token}"
sonar-scanner

set +x prevents Bash from printing expanded commands. Other tools can still leak secrets, so do not dump the environment.

Step 3: Limit which jobs receive it

Give the secret only to jobs running trusted code. Do not send it to unreviewed pull requests or use a privileged workflow to execute their code. A token restriction does not replace repository permissions.

Step 4: Replace and revoke

Create a replacement token, update the pipeline secret, and run a successful analysis. Revoke the old token only after the replacement works. On a test job, confirm the revoked token fails without printing it.

Check the result

The new token should submit analysis; the revoked token should not. If a token is exposed, revoke it immediately and review its use before creating another.

References

PB
Written by

Parul Bedi

Notes and guides by Parul Bedi.

Conversation

0 comments

No comments yet. Start the conversation below.

Leave a comment

Replying to a comment.
Be kind. Markdown is not supported in comments.