Monday, May 9, 2022

Using Azure Devops Invoke REST API task in yaml

 Documentation on using the Invoke REST API Azure DevOps task is sparse so I am documenting my solution; hopefully I can spare someone some time and headaches.

First, to be clear, this task is categorized in the GUI as a gate so the Get use case is for true/falseness. This means it is not possible to access any part of the response outside of this step. Although it would be straight forward to script calling a rest method, it is appealing to have a simple step that handles all that and I just configure a condition. It is worth noting, if you don't need to parse the response then Invoke REST API post is a great way for fire and forget type messaging - like sending a teams notification.

In my scenario, I want to automate release branch creation and steps required around that in our corporate processes. Before creating the branch, I want to make sure that Master is not ahead of Develop. Ahead count is available in the Git Stats API. There is a sample response in the documentation that shows that aheadCount is a root property. 

These are the steps I followed

  • Configure a generic service connection without user or password/token information.
  • Create a yaml file with the stages and jobs needed. Note that this is a Server task.
  • Under steps, add an Invoke REST API step, the gui gives some guidance here but remove the default headers and add an Authorization header containing "Bearer $(System.AccessToken)".
  • Add the url, using system variables for simpler re-use.
  • Set the success criteria to "eq(root['aheadCount'], 0)"
  • Following jobs or tasks in the stage should have a dependsOn configured for this task or job.
The GUI interface looks like this

 

The yaml looks like this. I use a simple delay step just to test the gate quickly before adding the rest of the logic.


stages:
  - stageRelease_Branch
    displayNameCreate release branch
    conditionalways()
    jobs:
      - jobEntryGate
        displayNameEntry gate
        poolServer
        steps:      
        - taskInvokeRESTAPI@1
          displayNameCheck ahead count is 0
          inputs:
            connectionType'connectedServiceName'
            serviceConnection'AzureDevOpsRest'
            method'GET'
            headers: |
              {
              "Authorization": "Bearer $(system.AccessToken)"
              }
            urlSuffix'/$(System.TeamProject)/_apis/git/repositories/$(Build.Repository.ID)/stats/branches?name=master&api-version=6.0'
            waitForCompletion'false'
            successCriteria"eq(root['aheadCount'], 0)" 
      - jobone_min_delay
        poolServer
        dependsOn
        - EntryGate
        steps:
        - taskDelay@1
          inputs:
            delayForMinutes'1'



No comments:

Post a Comment