Skip to content

ci: fetch Sonar token from Azure Key Vault via OIDC #43

ci: fetch Sonar token from Azure Key Vault via OIDC

ci: fetch Sonar token from Azure Key Vault via OIDC #43

Workflow file for this run

name: SonarCloud QA Gate
on:
workflow_dispatch:
pull_request:
push:
branches:
- main
jobs:
sonarcloud:
runs-on: ubuntu-latest
environment: org-prod
permissions:
id-token: write
contents: read
pull-requests: write
steps:
- uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Azure login (OIDC)
uses: azure/login@v2
with:
client-id: ${{ vars.AZURE_CLIENT_ID }}
tenant-id: ${{ vars.AZURE_TENANT_ID }}
subscription-id: ${{ vars.AZURE_SUBSCRIPTION_ID }}
- name: Resolve SonarCloud token
shell: bash
env:
FALLBACK_SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
run: |
KV_SONAR_TOKEN="$(az keyvault secret show \
--vault-name "${{ vars.AZURE_KEYVAULT_NAME }}" \
--name "sonar-cloud-token" \
--query value -o tsv 2>/dev/null || true)"
TOKEN_SOURCE=""
if [ -n "${KV_SONAR_TOKEN}" ]; then
KV_VALID="$(curl -sS -u "${KV_SONAR_TOKEN}:" https://sonarcloud.io/api/authentication/validate | grep -Eo 'true|false' | head -n1 || true)"
if [ "${KV_VALID}" = "true" ]; then
SONAR_TOKEN="${KV_SONAR_TOKEN}"
TOKEN_SOURCE="keyvault"
fi
fi
if [ -z "${TOKEN_SOURCE}" ] && [ -n "${FALLBACK_SONAR_TOKEN:-}" ]; then
SONAR_TOKEN="${FALLBACK_SONAR_TOKEN}"
TOKEN_SOURCE="github-secret-fallback"
fi
if [ -z "${TOKEN_SOURCE}" ]; then
echo "No valid Sonar token found in Key Vault and no fallback secret available."
exit 1
fi
echo "::notice title=Sonar token source::${TOKEN_SOURCE}"
echo "::add-mask::$SONAR_TOKEN"
echo "SONAR_TOKEN=$SONAR_TOKEN" >> "$GITHUB_ENV"
- uses: actions/setup-python@v6
with:
python-version: '3.12'
- name: Install dependencies
run: make install-dev
- name: Run tests with coverage
run: make coverage-sonar
- name: SonarCloud scan
uses: SonarSource/sonarcloud-github-action@ffc3010689be73b8e5ae0c57ce35968afd7909e8
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ env.SONAR_TOKEN }}
with:
args: >
-Dsonar.host.url=https://sonarcloud.io
-Dsonar.organization=codingworkflow
-Dsonar.projectKey=codingworkflow_claude-code-api
-Dsonar.python.coverage.reportPaths=dist/quality/coverage/coverage.xml
-Dsonar.python.xunit.reportPath=dist/quality/sonar/xunit-report.xml
-Dsonar.enableIssueAnnotation=true
- name: SonarCloud quality gate
uses: SonarSource/sonarqube-quality-gate-action@cf038b0e0cdecfa9e56c198bbb7d21d751d62c3b
with:
scanMetadataReportFile: dist/quality/sonar/scannerwork/report-task.txt
env:
SONAR_TOKEN: ${{ env.SONAR_TOKEN }}
SONAR_HOST_URL: https://sonarcloud.io
timeout-minutes: 5