|
| 1 | +# DevOps Shield - The ultimate DevSecOps platform designed to secure your DevOps. |
| 2 | +# https://devopsshield.com |
| 3 | +############################################################## |
| 4 | +# This is a DevOps Shield - Application Security - Code Security Template. |
| 5 | + |
| 6 | +# This workflow template uses actions that are not certified by DevOps Shield. |
| 7 | +# They are provided by a third-party and are governed by separate terms of service, privacy policy, and support documentation. |
| 8 | + |
| 9 | +# Use this workflow template for integrating code security into your pipelines and workflows. |
| 10 | + |
| 11 | +# DevOps Shield Workflow Template Details: |
| 12 | +# ------------------------------------------------------------ |
| 13 | +# Code: ADO_SAST_SONARQUBE_DOTNET |
| 14 | +# Name: SonarQube Scan .NET |
| 15 | +# DevSecOpsControls: SAST |
| 16 | +# Provider: SonarSource |
| 17 | +# Categories: Code Scanning |
| 18 | +# Description: |
| 19 | +# SonarQube is an open-source platform developed by SonarSource for continuous inspection of code quality to perform automatic reviews with static analysis of code to detect bugs, code smells, and security vulnerabilities. |
| 20 | +# Read the official documentation to find out more. Requires Azure DevOps SonarQube extension (https://marketplace.visualstudio.com/items?itemName=SonarSource.sonarqube). |
| 21 | +# For more information: |
| 22 | +# https://www.sonarqube.org/ |
| 23 | +# https://github.com/SonarSource/sonarqube |
| 24 | +# ------------------------------------------------------------ |
| 25 | +# Source repository: https://github.com/SonarSource/sonarqube |
| 26 | +############################################################## |
| 27 | + |
| 28 | +name: SonarQube Scan .NET |
| 29 | + |
| 30 | +trigger: |
| 31 | + batch: true |
| 32 | + branches: |
| 33 | + include: [main] |
| 34 | + |
| 35 | +schedules: |
| 36 | + - cron: 0 0 * * 0 |
| 37 | + displayName: Weekly Sunday build |
| 38 | + branches: |
| 39 | + include: [main] |
| 40 | + always: true |
| 41 | + |
| 42 | +parameters: |
| 43 | + - name: sonarQubeServiceConnection |
| 44 | + type: string |
| 45 | + default: "Sonar" |
| 46 | + displayName: "SonarQube Service Connection" |
| 47 | + - name: useClassicDotNet |
| 48 | + type: boolean |
| 49 | + default: true # set to true if you are using classic .NET |
| 50 | + displayName: "Use Classic .NET" |
| 51 | + - name: useWindowsPool |
| 52 | + type: boolean |
| 53 | + default: true # set to true if you need a Windows pool e.g. for classic .NET |
| 54 | + displayName: "Use Windows Pool" |
| 55 | + - name: defaultWindowsPool |
| 56 | + type: string |
| 57 | + default: "windows-2019" |
| 58 | + displayName: "Default Windows Pool" |
| 59 | + - name: defaultLinuxPool |
| 60 | + type: string |
| 61 | + default: "ubuntu-latest" |
| 62 | + displayName: "Default Linux Pool" |
| 63 | + |
| 64 | +variables: |
| 65 | + - name: cliSources |
| 66 | + value: "$(System.DefaultWorkingDirectory)" |
| 67 | + - name: SONARQUBE_PROJECT_VERSION |
| 68 | + value: "1.0" |
| 69 | + - name: dotNetCoreVersion |
| 70 | + value: "9.x" |
| 71 | + - name: buildConfiguration |
| 72 | + value: "Release" |
| 73 | + - name: solution |
| 74 | + value: "**/*.sln" |
| 75 | + - name: buildPlatform |
| 76 | + value: "Any CPU" |
| 77 | + |
| 78 | +stages: |
| 79 | + - stage: SAST |
| 80 | + displayName: Static Analysis Security test |
| 81 | + jobs: |
| 82 | + - job: SAST |
| 83 | + displayName: Static Analysis Security test |
| 84 | + pool: |
| 85 | + ${{ if eq(parameters.useWindowsPool, true) }}: |
| 86 | + vmImage: ${{ parameters.defaultWindowsPool }} |
| 87 | + ${{ if eq(parameters.useWindowsPool, false) }}: |
| 88 | + vmImage: ${{ parameters.defaultLinuxPool }} |
| 89 | + steps: |
| 90 | + - checkout: self |
| 91 | + fetchDepth: 0 |
| 92 | + # if you are using a Windows pool |
| 93 | + - ${{ if eq(parameters.useWindowsPool, true) }}: |
| 94 | + - task: PowerShell@2 |
| 95 | + inputs: |
| 96 | + targetType: "inline" |
| 97 | + script: | |
| 98 | + # Write your PowerShell commands here. |
| 99 | + $orgUrl = $env:System_CollectionUri |
| 100 | + Write-Output "Organization Url: $orgUrl" |
| 101 | +
|
| 102 | + $orgName = $orgUrl.Split('/')[3] |
| 103 | + Write-Output "Organization Name: $orgName" |
| 104 | +
|
| 105 | + $projectName = $env:System_TeamProject |
| 106 | + Write-Output "Project Name: $projectName" |
| 107 | +
|
| 108 | + # get repository name |
| 109 | + $repoName = $env:Build_Repository_Name |
| 110 | + Write-Output "Repository Name: $repoName" |
| 111 | +
|
| 112 | + # default Product Name should be ado org name _ azure devops project name |
| 113 | + $DEFAULT_PRODUCT_NAME = "${orgName}_${projectName}_${repoName}" |
| 114 | + Write-Output "Default Product Name: $DEFAULT_PRODUCT_NAME" |
| 115 | + Write-Output "##vso[task.setvariable variable=DEFAULT_PRODUCT_NAME;isOutput=true]$DEFAULT_PRODUCT_NAME" |
| 116 | +
|
| 117 | + $sonarQubeProjectKey = $DEFAULT_PRODUCT_NAME -replace ' ', '_' |
| 118 | + Write-Output "SonarQube Project Key: $sonarQubeProjectKey" |
| 119 | +
|
| 120 | + $sonarQubeProjectName = $DEFAULT_PRODUCT_NAME |
| 121 | + Write-Output "SonarQube Project Name: $sonarQubeProjectName" |
| 122 | +
|
| 123 | + Write-Output "##vso[task.setvariable variable=SONARQUBE_PROJECT_KEY;isOutput=true]$sonarQubeProjectKey" |
| 124 | + Write-Output "##vso[task.setvariable variable=SONARQUBE_PROJECT_NAME;isOutput=true]$sonarQubeProjectName" |
| 125 | + pwsh: true |
| 126 | + displayName: "Set SonarQube Project Key and Name (Windows Pool)" |
| 127 | + name: setSonarQubeProjectKeyAndName |
| 128 | + # if you are using a Linux pool |
| 129 | + - ${{ if eq(parameters.useWindowsPool, false) }}: |
| 130 | + - script: | |
| 131 | + orgUrl=$(System.CollectionUri) |
| 132 | + echo "Organization Url: $orgUrl" |
| 133 | + orgName=$(echo $orgUrl | cut -d'/' -f4) |
| 134 | + echo "Organization Name: $orgName" |
| 135 | + projectName=$(System.TeamProject) |
| 136 | + echo "Project Name: $projectName" |
| 137 | +
|
| 138 | + # get repository name |
| 139 | + repoName=$(Build.Repository.Name) |
| 140 | + echo "Repository Name: $repoName" |
| 141 | +
|
| 142 | + # default Product Name should be ado org name _ azure devops project name |
| 143 | + DEFAULT_PRODUCT_NAME="${orgName}_${projectName}_${repoName}" |
| 144 | + echo "Default Product Name: $DEFAULT_PRODUCT_NAME" |
| 145 | + echo "##vso[task.setvariable variable=DEFAULT_PRODUCT_NAME;isOutput=true]$DEFAULT_PRODUCT_NAME" |
| 146 | + sonarQubeProjectKey=$(echo $DEFAULT_PRODUCT_NAME | tr ' ' '_') |
| 147 | + echo "SonarQube Project Key: ${sonarQubeProjectKey}" |
| 148 | + sonarQubeProjectName=$DEFAULT_PRODUCT_NAME |
| 149 | + echo "SonarQube Project Name: $sonarQubeProjectName" |
| 150 | + echo "##vso[task.setvariable variable=SONARQUBE_PROJECT_KEY;isOutput=true]$sonarQubeProjectKey" |
| 151 | + echo "##vso[task.setvariable variable=SONARQUBE_PROJECT_NAME;isOutput=true]$sonarQubeProjectName" |
| 152 | + displayName: "Set SonarQube Project Key and Name (Linux Pool)" |
| 153 | + name: setSonarQubeProjectKeyAndName |
| 154 | + # verify the project key and name |
| 155 | + - script: | |
| 156 | + echo "Default Product Name: $(setSonarQubeProjectKeyAndName.DEFAULT_PRODUCT_NAME)" |
| 157 | + echo "SonarQube Project Key: $(setSonarQubeProjectKeyAndName.SONARQUBE_PROJECT_KEY)" |
| 158 | + echo "SonarQube Project Name: $(setSonarQubeProjectKeyAndName.SONARQUBE_PROJECT_NAME)" |
| 159 | + displayName: "Verify SonarQube Project Key and Name" |
| 160 | + - task: SonarQubePrepare@7 |
| 161 | + inputs: |
| 162 | + SonarQube: "${{ parameters.sonarQubeServiceConnection }}" |
| 163 | + scannerMode: "dotnet" |
| 164 | + projectKey: "$(setSonarQubeProjectKeyAndName.SONARQUBE_PROJECT_KEY)" |
| 165 | + projectName: "$(setSonarQubeProjectKeyAndName.SONARQUBE_PROJECT_NAME)" |
| 166 | + projectVersion: "SONARQUBE_PROJECT_VERSION" |
| 167 | + # needed for classic dotnet projects |
| 168 | + - task: NuGetCommand@2 |
| 169 | + condition: eq('${{ parameters.useClassicDotNet }}', true) # only run this task if useClassicDotNet is true |
| 170 | + displayName: "NuGet restore (.NET Framework Classic)" |
| 171 | + inputs: |
| 172 | + restoreSolution: "$(solution)" |
| 173 | + - task: VSBuild@1 |
| 174 | + condition: eq('${{ parameters.useClassicDotNet }}', true) # only run this task if useClassicDotNet is true |
| 175 | + displayName: "Build .NET Framework Classic" |
| 176 | + inputs: |
| 177 | + solution: "$(solution)" |
| 178 | + platform: "$(buildPlatform)" |
| 179 | + configuration: "$(buildConfiguration)" |
| 180 | + # needed for dotnet core projects |
| 181 | + - task: UseDotNet@2 |
| 182 | + condition: eq('${{ parameters.useClassicDotNet }}', false) # only run this task if useClassicDotNet is false |
| 183 | + displayName: "Use .NET Core sdk" |
| 184 | + inputs: |
| 185 | + packageType: "sdk" |
| 186 | + version: "$(dotNetCoreVersion)" |
| 187 | + - task: DotNetCoreCLI@2 |
| 188 | + condition: eq('${{ parameters.useClassicDotNet }}', false) # only run this task if useClassicDotNet is false |
| 189 | + displayName: "Build .NET Core" |
| 190 | + inputs: |
| 191 | + command: "build" |
| 192 | + projects: "**/*.csproj" |
| 193 | + arguments: "--configuration $(buildConfiguration)" |
| 194 | + - task: SonarQubeAnalyze@7 |
| 195 | + - task: SonarQubePublish@7 |
| 196 | + inputs: |
| 197 | + pollingTimeoutSec: "300" |
0 commit comments