How do I get azure DevOps to put the cloudflare url in a PR comment

85 views Asked by At

My company has an Azure DevOps pipeline which publishes the development branch to cloudflare and prints a link to that deployment in the log.

I would like that link to appear in a PR comment so that the dev doesn't need to click through to the logs to find it.

How do I achieve this?

1

There are 1 answers

1
Miao Tian-MSFT On BEST ANSWER

We can use this REST API Pull Request Thread Comments - Create in the pipeline.

Result:

enter image description here

First of all, make sure your build service has permissions to contribute to pull requests in your repository. Otherwise, you may get the error like this

"message":"TF401027: You need the Git 'PullRequestContribute' permission to perform this action. Details: identity 'Build\\xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx', scope 'repository'.

There are two build service accounts ProjectName Build Service (OrganizationName) and Project Collection Build Service (OrganizationName). It depends on your pipeline configuration. The doc is here. If you are not sure which account you are using, you can search the id xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx in the error message and you will see the account.

enter image description here

If you are using the yaml pipeline, here is the sample. The first task is to set your link as variable DeployURL. Then we can use the variable in the following tasks.

trigger: none
jobs:
- job: Job_1
  displayName: Agent job 1
  pool:
    vmImage: windows-2019
  steps:
  - checkout: self

  - task: PowerShell@2
    displayName: set variable DeployURL
    inputs:
      targetType: inline
      script: >-
        $url="https://dev.azure.com/"
        
        Write-Host "##vso[task.setvariable variable=DeployURL]$url"
  - task: PowerShell@2
    displayName: check the variable DeployURL
    inputs:
      targetType: inline
      script: Write-Host $(DeployURL)
  - task: PowerShell@2
    condition: eq(variables['Build.Reason'], 'PullRequest')
    displayName: write comment to the PR with DeployURL
    env:
        MY_ACCESS_TOKEN: $(System.AccessToken)
    inputs:
      targetType: inline
      script: >-
        $url = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/git/repositories/$($env:BUILD_REPOSITORY_NAME)/pullRequests/$($env:SYSTEM_PULLREQUEST_PULLREQUESTID)/threads?api-version=5.1"

        Write-Host "URL: $url"

        $body = @"
              {
                  "comments": [
                    {
                      "parentCommentId": 0,
                      "content": "The DeployURL is  $(DeployURL)",
                      "commentType": 1
                    }
                  ],
                  "status": 4
                }
        "@

        $result = Invoke-RestMethod -Uri $url -Method POST -Headers @{Authorization = "Bearer $(System.AccessToken)"} -Body $Body -ContentType application/json

If you are using the classic build pipeline, please check the Allow scripts to access the OAuth token.

enter image description here

The Custom condition is set to only run the task when the build reason is pullrequest.

enter image description here