mystack
mystack

Reputation: 5482

Schedule a Azure devops release in multiple timings

I am able to schedule the release using Rest API call . Is there any way to Queue it to run Multiple times.THe code i tried is given below.

$timinglist=@(1:30,2:30,3:30)

foreach($time in $timinglist)
 {
    $PATtoken= 'PAT'
    Write-Host "Initialize Autnetication COntext" -ForegroundColor DarkBlue
    $token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($PATtoken)"))
    $header=@{authorization= "Basic $token" }

    $defurl = "https://vsrm.dev.azure.com/Organization/Project/_apis/release/definitions/13?api-version=5.1" 


   $definition = Invoke-RestMethod -Uri $defurl -Method Get -Headers $header
   $hour=$time.Split(":")[0]
   $minute=$time.Split(":")[1]

   $hash = @(
   @{ 
      triggerType="schedule";
      schedule = @{"daysToRelease"="31";"timeZoneId"="India Standard Time";"startHours"=$hour;"startMinutes"=$minute}
   })
   $definition.triggers = $hash     

   $json = @($definition) | ConvertTo-Json -Depth 99 

   $updatedef = Invoke-RestMethod  -Uri $defurl  -Method Put -Body $json -ContentType "application/json" -Headers $header
   Write-Host ($updatedef.triggers | ConvertTo-Json -Depth 99)
}

My objective is to queue a release at 1:30 2:30 and 3:30 . But with the above code it is running only at 3:30 and other two are not happening.

Upvotes: 1

Views: 277

Answers (1)

Lorenzo Isidori
Lorenzo Isidori

Reputation: 2047

You are overriding the triggers property every time you send the request. So the last value wins over the old ones.

triggers property is an array of BuildTrigger, you don't need to execute 3 request, just one! This is the triggers documentation.

EDIT:

I am not a powershell wizard but you should create an array of BuildTrigger object like this:

$hash = @(
   @{ 
      triggerType="schedule";
      schedule = @{"daysToRelease"="31";"timeZoneId"="India Standard Time";"startHours"=$hour1;"startMinutes"=$minute1}
   },
   @{ 
      triggerType="schedule";
      schedule = @{"daysToRelease"="31";"timeZoneId"="India Standard Time";"startHours"=$hour2;"startMinutes"=$minute2}
   },
   @{ 
      triggerType="schedule";
      schedule = @{"daysToRelease"="31";"timeZoneId"="India Standard Time";"startHours"=$hour3;"startMinutes"=$minute3}
   }
   )

Upvotes: 1

Related Questions