Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
217 lines (204 sloc) 9.51 KB
parameters:
job:
name: ''
displayName: ''
pool: ''
build:
configuration: 'Release'
sonar:
enabled: True
buildBreaker:
enabled: True
jobs:
- job: ${{ parameters.job.name }}
displayName: ${{ parameters.job.displayName }}
continueOnError: False
pool: ${{ parameters.pool }}
workspace:
clean: all
steps:
# Install specific .NET Core SDK version.
# See more here: https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/tool/dotnet-core-tool-installer?view=azure-devops.
# Installing a specific .NET Core SDK version is needed to avoid installing a .NET Core global tool in a following task and then have Azure DevOps complain that it cannot find it.
# This issue is documented here: https://github.com/Microsoft/azure-pipelines-tasks/issues/8291.
- task: DotNetCoreInstaller@0
name: install_dotnetcore_sdk
displayName: Install .NET Core SDK
enabled: True
inputs:
packageType: 'sdk'
version: $(DotNetCore_SDK_Version)
# Install sonarscanner tool to be able to perform a SonarQube analysis.
# See more here: https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+MSBuild.
# See more about "script" step here: https://docs.microsoft.com/en-us/azure/devops/pipelines/scripts/cross-platform-scripting?view=azure-devops&tabs=yaml
# and here: https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/command-line?view=vsts&tabs=yaml#yaml-snippet.
# The long YAML line has been splitted using "block chomping indicator", as documented here: https://stackoverflow.com/a/3790497.
- script: >-
dotnet tool install dotnet-sonarscanner
--global
--version $(SonarScanner_Version)
name: install_sonarscanner
displayName: Install SonarQube static code analyzing CLI tool
enabled: ${{ parameters.sonar.enabled }}
env:
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: $(DotNetSkipFirstTimeExperience)
DOTNET_CLI_TELEMETRY_OPTOUT: $(DotNetCliTelemetryOptOut)
COREHOST_TRACE: $(CoreHostTrace)
# Prepare SonarQube analysis.
# See more here: https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+MSBuild.
#
# You need to define a variable group named "SonarQube" and add several variables:
# - SonarProjectKey - contains the key used for connecting to the project hosted on SonarCloud
# - SonarProjectVersion - specifies the version to attach to this SonarQube analysis
# Then map this group to the "variables" section of this pipeline.
#
# The analysis will make use of the branch name via the "sonar.branch.name" command parameter.
# See more here: https://sonarcloud.io/documentation/branches/overview/#analysis.
- script: >-
dotnet-sonarscanner begin
/k:"$(CurrentProject.Sonar.ProjectKey)"
/v:"$(CurrentProject.Version)"
/s:"$(Build.SourcesDirectory)/Build/SonarQubeAnalysis.xml"
/d:sonar.login="$(CurrentProject.Sonar.Token)"
/d:sonar.branch.name="$(Build.SourceBranchName)"
name: prepare_sonarqube_analysis
displayName: Prepare SonarQube analysis
enabled: ${{ parameters.sonar.enabled }}
env:
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: $(DotNetSkipFirstTimeExperience)
DOTNET_CLI_TELEMETRY_OPTOUT: $(DotNetCliTelemetryOptOut)
COREHOST_TRACE: $(CoreHostTrace)
# Compile source code.
# See more here: https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/build/dotnet-core-cli?view=vsts.
- script: >-
dotnet build $(Build.SourcesDirectory)/Todo.sln
--configuration ${{ parameters.build.configuration }}
name: build_sources
displayName: Build sources
enabled: True
env:
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: $(DotNetSkipFirstTimeExperience)
DOTNET_CLI_TELEMETRY_OPTOUT: $(DotNetCliTelemetryOptOut)
COREHOST_TRACE: $(CoreHostTrace)
# Run unit tests and collect code coverage metrics.
# See more about running selective tests here: https://docs.microsoft.com/en-us/dotnet/core/testing/selective-unit-tests#xunit.
- script: >-
dotnet test $(Build.SourcesDirectory)/Todo.sln
--no-build
--configuration ${{ parameters.build.configuration }}
--filter "FullyQualifiedName~UnitTests"
--test-adapter-path "."
--logger "xunit;LogFilePath=TodoWebApp.UnitTests.xunit.xml"
/p:CollectCoverage=True
/p:CoverletOutputFormat=opencover
/p:CoverletOutput="TodoWebApp.UnitTests.opencover.xml"
/p:Include="[TodoWebApp]*"
name: run_unit_tests
displayName: Run unit tests
enabled: True
env:
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: $(DotNetSkipFirstTimeExperience)
DOTNET_CLI_TELEMETRY_OPTOUT: $(DotNetCliTelemetryOptOut)
COREHOST_TRACE: $(CoreHostTrace)
# Run integration tests and collect code coverage metrics.
- script: >-
dotnet test $(Build.SourcesDirectory)/Todo.sln
--no-build
--configuration ${{ parameters.build.configuration }}
--filter "FullyQualifiedName~IntegrationTests"
--test-adapter-path "."
--logger "xunit;LogFilePath=TodoWebApp.IntegrationTests.xunit.xml"
/p:CollectCoverage=True
/p:CoverletOutputFormat=opencover
/p:CoverletOutput="TodoWebApp.IntegrationTests.opencover.xml"
/p:Include="[TodoWebApp]*"
name: run_integration_tests
displayName: Run integration tests
enabled: True
env:
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: $(DotNetSkipFirstTimeExperience)
DOTNET_CLI_TELEMETRY_OPTOUT: $(DotNetCliTelemetryOptOut)
COREHOST_TRACE: $(CoreHostTrace)
# Publish test results.
# See more here: https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/test/publish-test-results?view=vsts&tabs=yaml.
- task: PublishTestResults@2
displayName: Publish test results
name: publish_test_results
enabled: True
inputs:
testResultsFormat: 'xUnit'
testResultsFiles: '$(Build.SourcesDirectory)/Tests/**/*.xunit.xml'
mergeTestResults: True
buildConfiguration: ${{ parameters.build.configuration }}
publishRunAttachments: True
# Install reportgenerator tool to be able to generate code coverage related reports.
- script: >-
dotnet tool install dotnet-reportgenerator-globaltool
--global
--version $(ReportGenerator_Version)
name: install_code_coverage_report_generator
displayName: Install code coverage report generator tool
enabled: True
env:
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: $(DotNetSkipFirstTimeExperience)
DOTNET_CLI_TELEMETRY_OPTOUT: $(DotNetCliTelemetryOptOut)
COREHOST_TRACE: $(CoreHostTrace)
# Generate code coverage report:
# - combine several OpenCover coverage data files into one single file in Cobertura format
# - generate coverage HTML report ready to be displayed by Azure DevOps
# - generate coverage badge to be displayed on GitHub project home page
# See more here: https://github.com/danielpalme/ReportGenerator#usage.
# See more about the output formats here: https://github.com/danielpalme/ReportGenerator/wiki/Output-formats.
# In order to be able to see the report inside the Code Coverage tab on Azure DevOps,
# enable Azure Boards for your project, as documented here: https://developercommunity.visualstudio.com/solutions/403137/view.html.
- script: >-
reportgenerator
"-reports:$(Build.SourcesDirectory)/Tests/**/*.opencover.xml"
"-targetdir:$(Build.SourcesDirectory)/.CoverageResults/Report"
"-reporttypes:Cobertura;HtmlInline_AzurePipelines;Badges"
name: generate_code_coverage_report
displayName: Generate code coverage report
enabled: True
# Publish code coverage report.
# See more here: https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/test/publish-code-coverage-results?view=vsts.
- task: PublishCodeCoverageResults@1
name: publish_code_coverage_report
displayName: Publish code coverage report
enabled: True
inputs:
codeCoverageTool: 'Cobertura'
summaryFileLocation: '$(Build.SourcesDirectory)/.CoverageResults/Report/Cobertura.xml'
reportDirectory: '$(Build.SourcesDirectory)/.CoverageResults/Report'
# Upload SonarQube report to external service, SonarCloud.
# See more here: https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+MSBuild.
- script: >-
dotnet-sonarscanner end
/d:sonar.login="$(CurrentProject.Sonar.Token)"
name: upload_sonarqube_report
displayName: Upload SonarQube report
enabled: ${{ parameters.sonar.enabled }}
env:
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: $(DotNetSkipFirstTimeExperience)
DOTNET_CLI_TELEMETRY_OPTOUT: $(DotNetCliTelemetryOptOut)
COREHOST_TRACE: $(CoreHostTrace)
# Runs a PowerShell script to break the build in case Sonar quality gate has failed.
# See more here: https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/powershell?view=vsts.
- task: PowerShell@2
name: sonar_build_breaker
displayName: Run Sonar build breaker
# See more about conditions here: https://docs.microsoft.com/en-us/azure/devops/pipelines/process/conditions?view=azdevops&tabs=yaml.
condition: |
and
(
eq( ${{ parameters.sonar.enabled }}, True),
eq( ${{ parameters.sonar.buildBreaker.enabled }}, True)
)
inputs:
targetType: 'filePath'
filePath: '$(Build.SourcesDirectory)/Build/SonarBuildBreaker.ps1'
arguments: >-
-SonarToken "$(CurrentProject.Sonar.Token)"
-DotSonarQubeFolder "$(Agent.BuildDirectory)/.sonarqube"
errorActionPreference: stop
failOnStderr: True
workingDirectory: $(Build.SourcesDirectory)
You can’t perform that action at this time.