Scan CodeScan Cloud projects in Azure DevOps
Prerequisites
- CodeScan version 4.4+ 
- An Azure DevOps Project for your Salesforce code 
Install the CodeScan Cloud Extension
- In the Azure DevOps app, go to the - Marketplaceand then select- Browse Marketplace.
- Search for - CodeScan, select the- CodeScan Cloudextension and then click- Get it free.
- Select your account and complete the installation. 
Setup
- On your - Projectdashboard screen, select- Pipeline > Pipelinesand create a new Pipeline.
- Once you are in the "Where is your code?" page, click Use the classic editor to create a pipeline without YAML. However, this setting may not be available available until after the following steps are taken. 
- Navigate to your Organization / Project settings. Then under the Pipelines section, choose "Settings." In the General section, toggle off the "Disable creation of classic build pipelines" and "Disable creation of classic release pipelines" settings.  
Then follow the instructions below for your source code location.
Azure DevOps Repo
- Select - Azure Repos Gitand your- Repository name.
- For your default branch, select the - branchyou would like to check pull requests against. Keep this branch name in mind, we will use it later in the setup. Click- Continue.
- On the - Select a Templatepage, select the- Salesforce with CodeScan Cloudtemplate and click- Apply.
- In the Agent pool dropdown menu, select - Azure Pipelines.
- In the - Agent Specificationdropdown menu, select- ubuntu-20.04.
- Click the - Prepare Analysis on CodeScan Cloudsection and create a new service endpoint.- Add your CodeScan server URL (e.g., https://app.codescan.io/) 
- You will need a token from your CodeScan Cloud account for this step. Learn how to create security token HERE. 
- Add a name for your connection. 
- Make sure to verify the connection before leaving the pop-up. 
 
- Select your new - Service Endpointand the- Organizationyou would like to connect to from the dropdown menu. If you are not sure, the Organization Key is available at the top left of your- Organizationpage.
- Select - Use standalone scannerunder the- Choose a way to run the analysis.
- Under - Mode, select the- make sure Manually provide configurationcheckbox.
Now, in CodeScan Cloud we can set up the project.
- In your selected CodeScan Cloud organization, navigate to - Administration > Projects Management.
- Click - Create Project.
- Enter your desired - Project Nameand- Project Keyand click- Create. Keep these in mind, we'll need them in a second.
- Click on your new empty project and navigate to - Administration > Branches and Pull Requests.
- Change your main branch name to the name of the default branch that you selected. 
Now, back to Azure DevOps.
- Enter the - Project Nameand- Project Keyyou just created.
- Click - Save and Queueand let the analysis complete to see your results in CodeScan Cloud.
Triggering Builds from an Azure DevOps Repository
To trigger the builds, you will need to create a build policy on the branch you would like to check pull requests against.
- Navigate to - Repos > Branches.
- Click on the - Moremenu for the desired branch and click- Branch Policies.
- In the - Build Validationsection,- add a new build policy.
- Select your - new pipelineand select- Automatic for the Trigger settingsand choose your policy requirements.
This pipeline will now run when:
- Pull requests are created against the branch 
- Pull requests are updated 
- Pull requests are merged. 
The project branches on CodeScan Cloud will be updated accordingly.
Breaking the Builds
To break the builds based on the Quality Gate once this analysis has run, you can add a PowerShell script to the pipeline.
- First create a Security Token and add it as a variable named - CODESCAN_TOKENin your pipeline Variables menu.
- Add a PowerShell step to your pipeline after the Publish Quality Gate step and add the following script inline, changing the below parameters: - <<project_key>>to your actual project key.- {codescan_instance_url}:Your instance's URL, for example, https://app.codescan.io/ for US region, https://app-eu.codescan.io/ for EU region or https://app-aus.codescan.io/ for AUS region.
 $token = [System.Text.Encoding]::UTF8.GetBytes($env:CODESCAN_TOKEN + ":")
 $base64 = [System.Convert]::ToBase64String($token)
 $basicAuth = [string]::Format("Basic {0}", $base64)
 $headers = @{ Authorization = $basicAuth }
 
 Write-Host "Pull Request ID:$($env:SYSTEM_PULLREQUEST_PULLREQUESTID)"
 
 $Target = "$env:SYSTEM_PULLREQUEST_PULLREQUESTID"
 $URL = "{codescan_instance_url}/api/qualitygates/project_status?projectKey=<<project_key>>&pullRequest={0}"
 
 if( !$Target)
 {
   $Target = "$env:BUILD_SOURCEBRANCH"
   $Target = $Target.Replace('refs/heads/','')
   $URL = "{codescan_instance_url}/api/qualitygates/project_status?projectKey=<<project_key>>&branch={0}"
 } 
 
 $URL = [string]::Format($URL, $Target)
 
 $result = Invoke-RestMethod -Method Get -Uri $URL  -Headers $headers
 $result | ConvertTo-Json | Write-Host
  
 if ($result.projectStatus.status -eq "OK") {
 Write-Host "Quality Gate Succeeded"
 }else{
 throw "Quality Gate failed"
 }The pipeline will now fail if the quality gates for the project are not passed.
Last updated
Was this helpful?

