Azure Pipeline Check Time

Viewed 100

I have an Azure DevOps build pipeline that is triggered after a build completion of another pipeline. I only want to execute my pipeline if it is started in a specific time range, e.g. between 10 and 11 pm. How can I set up my pipeline to only execute it if the current time is between the specified time range?

1 Answers

Only thing that comes to my mind is a custom solution using powershell.Check the time restrictions and if they return true then execute below.

We can trigger a release from Powershell by calling the REST API(Releases - Create) but cannot pass process variable (settable at release time).

Because we have to provide the specific artifact version ID(which artifact be used to trigger the release) in the request body. Once the artifact version ID is provided,the variable is no longer useful.

You can reference below PowerShell script to trigger a release:

Param(
   [string]$collectionurl = "https://{account}.vsrm.visualstudio.com",
   [string]$projectName = "GIT",
   [string]$user = "username",
   [string]$token = "password",
   [string]$releasedDefinitionId = "3" 

)

# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))

function CreateJsonBody
{
    $value = @"
{
 "definitionId":$releasedDefinitionId,
 "artifacts":[{"alias":"_BitBucketDev",
                "instanceReference":{"id":"454",
                       "name":"454",
                       "definitionId":"58",
                       "sourceBranch":"master",
                    }
                }
            ],

 "isDraft":false,
 "manualEnvironments":[]
"@

 return $value
}

$json = CreateJsonBody

$uri = "$($collectionurl)/$($projectName)/_apis/Release/releases?api-version=4.1-preview.6"
$result = Invoke-RestMethod -Uri $uri -Method Post -Body $json -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}

$ReleaseID = $result.id

Write-Host "ReleaseID:" $ReleaseID
Related