Sample PR integrations in CI
We have captured here typical use cases of Pull Request Integration:
The Target branch must have builds already reported to Sealights, otherwise the prConfig command will fail.
If your development process includes pull requests merged to features branch, you may want to exclude them from the scope if Sealights analysis or enforce a build to be reported with the creation of the feature branch (via a webhook).
Maven Build for a Java application
In the example below, we simply create the buildSessionId
outside the JSON command using the relevant parameters.
In the sample script below, please update lines 7 and 8 before executing it.
#!/bin/bash
echo "Downloading Sealights Agents..."
wget -nv https://agents.sealights.co/sealights-java/sealights-java-latest.zip
unzip -o sealights-java-latest.zip
# sample values
export SL_APP_NAME="myApp"
export SL_PACKAGES_INCLUDED="*com.mycompany.*"
# Pull request parameters per the Sealights documentation
if [ -n "${BITBUCKET_PR_ID}" ]; then
#Pull Request context (Bitbucket environment variables)
java -jar sl-build-scanner.jar -prConfig -token $SL_TOKEN -appname $SL_APP_NAME \
-targetBranch "origin/$BITBUCKET_PR_DESTINATION_BRANCH" -pullRequestNumber $BITBUCKET_PR_ID \
-latestCommit $BITBUCKET_COMMIT -repoUrl "https://bitbucket.mycompany.int/$BITBUCKET_REPO_SLUG" \
-pi $SL_PACKAGES_INCLUDED
else
#Regular Build
java -jar sl-build-scanner.jar -config -token $SL_TOKEN -appname $SL_APP_NAME \
-branchname "${GIT_BRANCH}" -buildname "${BUILD_NUMBER}" -pi $SL_PACKAGES_INCLUDED
fi
echo '{
"token": ${SL_TOKEN},
"createBuildSessionId": false,
"buildSessionIdFile": "./buildSessionId.txt",
"filesIncluded": "*.class",
"filesExcluded": "*test-classes*",
"recursive": true,
"includeResources": true,
"testStage": "Unit Tests",
"labId": null,
"executionType": "full",
"logEnabled": false,
"logDestination": "console",
"logLevel": "off",
"logFolder": "/tmp/sl-logs",
"sealightsJvmParams": {},
"enabled": true,
"filesStorage": "/tmp"
}' > slmaven.json
echo "Updating POM with Sealights..."
java -jar sl-build-scanner.jar -pom -configfile slmaven.json -workspacepath .
Azure DevOps Pipeline for a DotNet application
In the sample script below, if required, please update lines 1 to 4 and 25-26 before executing it.
$sldomain="mycompany.sealights.co"
$slagenttoken="$(AgentToken.Sandbox)"
$APP_NAME="MyApplication"
$APP_NAMESPACE="CalculatorServer.Controllers"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Write-Output "Download the Sealights DotNet agent version set in settings..."
$agentversion = ((iwr -Uri https://$($sldomain)/api/v2/agents/dotnet/recommended -Headers @{'Accept' = 'application/json'; 'Authorization' = "Bearer $($slagenttoken)"}).Content `
| ConvertFrom-Json | Select-Object agent).agent.version
iwr -OutFile sealights-dotnet-agent.zip -Uri http://agents.sealights.co/SL.DotNet/SL.DotNet-$($agentversion).zip
Expand-Archive .\sealights-dotnet-agent.zip -DestinationPath SL.DotNet -Force
Write-Output "Sealights agent version used is: $(Get-Content .\SL.DotNet\version.txt)"
#Retrieve the same name of target branch as reported in Sealights Dashboard by removing the uncecessary prefix
$PR_TARGET_BRANCH="$(System.PullRequest.TargetBranch)".Replace("refs/heads/","")
#Retrieve the last Commit Hash from the PR branch and not the one from the ADO local Merge (via git log history)
$PR_LAST_COMMIT=$(git log -2 --format=%H).Split(" ")[1]
$PR_NUMBER="$(System.PullRequest.PullRequestId)"
$REPO_URL="$(System.PullRequest.SourceRepositoryURI)"
Write-Output "`n*** Create PR BSID ***"
.\SL.DotNet\x64\SL.DotNet.exe prConfig --appName $APP_NAME --pullRequestNumber $PR_NUMBER --targetBranch $PR_TARGET_BRANCH --latestCommit $PR_LAST_COMMIT --repositoryUrl $REPO_URL `
--includeNamespace $APP_NAMESPACE --buildSessionIdFile $(Build.ArtifactStagingDirectory)\SealightsBuildSessionId.txt --token $($slagenttoken) --logEnabled true --logAppendConsole true --ignoreCertificateErrors true
# Base path where all relative paths should start from. By default the agent searches for the solution file and uses its path for this value
$SOLUTION_DIR="$(Build.Repository.LocalPath)"
#Path to the source workspace
$SOURCE_DIR="$(Build.Repository.LocalPath)\src"
Write-Output "`n*** Prepare for MSBuild ***"
.\SL.DotNet\x64\SL.DotNet.exe prepareForMsBuild --buildSessionIdFile $(Build.ArtifactStagingDirectory)\SealightsBuildSessionId.txt --workspacePath $SOURCE_DIR `
--baseDir $SOLUTION_DIR --ignoreGeneratedCode true --debugMode true --logEnabled true --logAppendConsole true --ignoreCertificateErrors true --token $($slagenttoken) --scm git --scmProvider vsts