diff --git a/build-test-snar-ps/build-test-sonar.ps1 b/build-test-snar-ps/build-test-sonar.ps1 index 06901f9..330bf35 100644 --- a/build-test-snar-ps/build-test-sonar.ps1 +++ b/build-test-snar-ps/build-test-sonar.ps1 @@ -1,7 +1,12 @@ +if ([string]::IsNullOrWhiteSpace($env:SONAR_TOKEN)) { + Write-Error "SONAR_TOKEN is not set. Without it the scanner authenticates anonymously and the analysis fails later with 'Not authorized or project not found'." + exit 1 +} + dotnet tool install --global dotnet-sonarscanner dotnet tool install --global dotnet-coverage dotnet restore Ploch.Common.sln -dotnet sonarscanner begin /k:"mrploch_ploch-common" /o:"mrploch" /d:sonar.login="$sonarToken" /d:sonar.cs.opencover.reportsPaths=**/CoverageResults/coverage.opencover.xml /d:sonar.host.url="https://sonarcloud.io" +dotnet sonarscanner begin /k:"mrploch_ploch-common" /o:"mrploch" /d:sonar.token="$env:SONAR_TOKEN" /d:sonar.cs.opencover.reportsPaths=**/CoverageResults/coverage.opencover.xml /d:sonar.host.url="https://sonarcloud.io" dotnet build Ploch.Common.sln --no-incremental --no-restore dotnet test Ploch.Common.sln --verbosity normal --no-build --logger "trx;LogFileName=TestOutputResults.xml" /p:CollectCoverage=true /p:CoverletOutput=./CoverageResults/ "/p:CoverletOutputFormat=cobertura%2copencover" -dotnet sonarscanner end /d:sonar.login="$env:SONAR_TOKEN" \ No newline at end of file +dotnet sonarscanner end /d:sonar.token="$env:SONAR_TOKEN" \ No newline at end of file diff --git a/build-test-sonar/action.yml b/build-test-sonar/action.yml index 931894c..ab45a6c 100644 --- a/build-test-sonar/action.yml +++ b/build-test-sonar/action.yml @@ -3,9 +3,14 @@ description: 'Build and Test .NET Solution with SonarCloud' author: 'Kris Ploch' inputs: dotnet-version: - description: '.NET version' + description: >- + .NET SDK version to install, in actions/setup-dotnet syntax. This value is + always passed to actions/setup-dotnet, which therefore never falls back to + a global.json in the consuming repository. Consumers targeting a different + framework (for example net10.0) must set this explicitly rather than + relying on whichever SDK the runner image happens to ship. required: false - default: 9.0.x + default: 9.0.x solution-path: description: 'Solution path to build' required: true @@ -22,25 +27,31 @@ inputs: description: 'SonarQube Url, defaults to SonarCloud' default: 'https://sonarcloud.io' required: false -outputs: - random-number: - description: "Random number" - value: ${{ steps.random-number-generator.outputs.random-number }} runs: using: "composite" steps: + - name: Validate SonarCloud Token + shell: pwsh + env: + SONAR_TOKEN: ${{ inputs.sonar-token }} + run: | + if ([string]::IsNullOrWhiteSpace($env:SONAR_TOKEN)) { + Write-Output "::error::The 'sonar-token' input is empty. SonarCloud analysis would run anonymously and then fail during post-processing with a misleading 'Not authorized or project not found'. Check that the calling workflow passes a non-empty secret and that the secret is available to this job." + exit 1 + } + Write-Output "SonarCloud token supplied ($($env:SONAR_TOKEN.Length) characters)." - name: Action Properties shell: pwsh run: | $InformationPreference="Continue" Write-Information "Building ${{ inputs.solution-path }} with .NET ${{ inputs.dotnet-version }}" - Write-Information "SonarCloud Organization: ${{ inputs.sonar-organization }} Project Key: ${{ inputs.sonar-project-key }} Login: ${{ inputs.sonar-token }}" - - uses: actions/setup-java@v4 + Write-Information "SonarCloud Organization: ${{ inputs.sonar-organization }} Project Key: ${{ inputs.sonar-project-key }}" + - uses: actions/setup-java@v5 with: distribution: 'zulu' # See 'Supported distributions' for available options java-version: '17' - name: Setup .NET - uses: actions/setup-dotnet@v3 + uses: actions/setup-dotnet@v6 with: dotnet-version: ${{ inputs.dotnet-version }} - name: Restore dependencies @@ -54,7 +65,9 @@ runs: run: dotnet tool install --global dotnet-coverage - name: SonarScanner Begin shell: pwsh - run: dotnet sonarscanner begin /k:"${{ inputs.sonar-project-key }}" /o:"${{ inputs.sonar-organization }}" /d:sonar.login="${{ inputs.sonar-token }}" /d:sonar.cs.opencover.reportsPaths=**/CoverageResults/coverage.opencover.xml /d:sonar.host.url="${{ inputs.sonar-url }}" + env: + SONAR_TOKEN: ${{ inputs.sonar-token }} + run: dotnet sonarscanner begin /k:"${{ inputs.sonar-project-key }}" /o:"${{ inputs.sonar-organization }}" /d:sonar.token="$env:SONAR_TOKEN" /d:sonar.cs.opencover.reportsPaths=**/CoverageResults/coverage.opencover.xml /d:sonar.host.url="${{ inputs.sonar-url }}" - name: Build shell: pwsh run: dotnet build ${{ inputs.solution-path }} --no-restore @@ -63,4 +76,6 @@ runs: run: dotnet test ${{ inputs.solution-path }} --verbosity normal --no-build --logger "trx;LogFileName=test-results.trx" /p:CollectCoverage=true /p:CoverletOutput=./CoverageResults/ "/p:CoverletOutputFormat=cobertura%2copencover" - name: SonarScanner End shell: pwsh - run: dotnet sonarscanner end /d:sonar.login="${{ inputs.sonar-token }}" + env: + SONAR_TOKEN: ${{ inputs.sonar-token }} + run: dotnet sonarscanner end /d:sonar.token="$env:SONAR_TOKEN"