Search code examples
azure-devopsazure-pipelinessentry

Azure DevOps integration in Sentry: Associate commits


Did someone manage to integrate Azure DevOps in Sentry (sentry.io)? I stuck on "Associate commits with a Release" (see: https://docs.sentry.io/workflow/releases/?platform=browser#associate-commits-with-a-release)

I can not figure out a way how I can tell Sentry (by API) which commit ids are associated with a current release/deploy. How can I add a task to the pipeline which will post the commit ids to Sentry API? Or is there some other way to do it?


Solution

  • In azure devops, the Powershell task also support curl. So, you can execute the api in powershell task of VSTS pipeline directly.

    In release pipeline, there has a pre-defined release variable, it stores the commit id which is associated with the current release pipeline: $(Release.Artifacts.{alias}.SourceVersion). Here alias is the artifacts name, and you can get it by getting $(Release.PrimaryArtifactSourceAlias).

    First, create variables like this:

    enter image description here

    Then you can apply the variable $(id) into that API, and execute the api in powershell task:

    "refs": [{
     "commit":"$(id)"
     }]
    

    Now, the commit id could be packed into the body of this api, and send to the Sentry server.


    If there has multiple commits associate with this release, since the variable $(Release.Artifacts.{alias}.SourceVersion) I mentioned above only store the latest commit message, here you may need add additional scripts to get what you want by Build id.

    In release pipeline, with $(Build.BuildId) you can get the corresponding buildid which associate with this release. And then, you could get the commits(changes) by using this API:

    GET https://dev.azure.com/{organization}/{project}/_apis/build/changes?fromBuildId={fromBuildId}&toBuildId={toBuildId}&api-version=5.1-preview.2
    

    enter image description here

    You could apply these powershell script into your task without change anything because this script is universal among powershell-ise, powershell command line and powershell task in VSTS.

    $token = "{PAT token}"
    $url="https://dev.azure.com/{org name}/{project name}/_apis/build/changes?fromBuildId={id1}&toBuildId={id2}"
    $token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
    $response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Get
    Write-Host "results = $($response.value.id | ConvertTo-Json -Depth 100)"
    

    Now, you could get the list of commits which associate with the build and corresponding release.