Search code examples
azure-devopsazure-pipelinesschedule

How to get scheduled build\release data using REST API


Would like to get what is the next expected Build/Release and get the information using API then project it some form of Dashboard (With the collected information) rather than checking from Azure DevOps > Pipelines > Builds option.

Scheduler has been enabled for both Build & Release pipelines and it is getting triggered successfully as well. I'm able to collect build/release information using REST API, however i can get the info of processed/completed builds/releases.

Based on the requirement, i just want to get the next scheduled build/release details (any info about them is fine) as I have enabled scheduler for them.

If Scheduler just helps in triggering the pipelines, is there any way we can retrieve that information programmatically?


Solution

  • How to get scheduled build\release data using REST API

    You could use Rest API to Get a build definition to get the build schedule information:

    GET https://dev.azure.com/{organization}/{project}/_apis/build/definitions/{definitionId}?api-version=5.1
    

    For example, I use following Powershell scripts to get the build schedule information:

    $url = "https://dev.azure.com/<MyOrganization>/<MyProject>/_apis/build/definitions/55?api-version=5.1"
    $buildPipeline= Invoke-RestMethod -Uri $url -Headers @{   
     Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
    } -Method Get
    
    $scheduledstartHours= $buildPipeline.triggers.schedules.startHours
    $scheduledstartMinutes= $buildPipeline.triggers.schedules.startMinutes
    $scheduledNumber= $buildPipeline.triggers.schedules.daysToBuild
    $scheduledJobId= $buildPipeline.triggers.schedules.scheduleJobId
    
    Write-Host This is start Hours: $scheduledstartHours
    Write-Host This is start Minutes: $scheduledstartMinutes
    Write-Host This is scheduled Number: $scheduledNumber
    Write-Host This is scheduled Job Id: $scheduledJobId
    

    As the test result:

    enter image description here

    Note:

    • When to build is indicated by buildNnumber when you select more than 1 day in the schedule, but MS does not provide a comparison table.
    • When you use above scripts, you need go to the Agent Phase and select Allow Scripts to Access OAuth Token. See Use the OAuth token to access the REST API

    For the release schedule information, you could use the Get a Release Definition as same.

    Hope this helps.