Reputation: 35
I am using using Microsoft.TeamFoundation.Build.WebApi, Microsoft.TeamFoundation.Core.WebApi, Microsoft.VisualStudio.Services.Common, Microsoft.VisualStudio.Services.WebApi
to trigger pipelines from my asp.net web app.
I can trigger pipelines, get work items, definitions but I am struggling to set a scheduled trigger for a pipeline using buildClient.QueueBuildAsync(build)
I tried using abstract class BuildTrigger in Microsoft.TeamFoundation.Build.WebApi that contains property DefinitionTriggerType enum with schedule on 8 number but how can I implement it to my pipeline before queuing?
This is how I run pipeline:
var credential = new VssBasicCredential(string.Empty, patToken);
var connection = new VssConnection(new Uri("https://dev.azure.com/myteam5468/"), credential);
var buildClient = connection.GetClient<BuildHttpClient>();
var projectClient = connection.GetClient<ProjectHttpClient>();
projectClient.GetProjects();
var project = projectClient.GetProject(projectName).Result;
Console.WriteLine(project.Name);
var definition = buildClient.GetDefinitionAsync(projectName, userSelectedPipelineID).Result;
Console.WriteLine(definition.Name);
var build = new Build()
{
Definition = definition,
Project = project,
};`
//Runs Pipeline
buildClient.QueueBuildAsync(build).Wait();
Console.WriteLine(string.Format("Pipeline {0} started running successfully", definition.Name));
How can we define scheduled triggers using TFS libraries?
Update 1:
buildClient.UpdateDefinitionAsync(definition.Triggers[0].TriggerType = DefinitionTriggerType.Schedule);
Above code as per Daniel's comment takes in the definition for updating the existing definition in the project but I am unable to set the triggerType because it is read-only.
Update 2:
//Create Trigger
ScheduleTrigger trigger = new ScheduleTrigger();
//Create Schedule
Schedule schedule = new Schedule();
schedule.StartHours = 10;
schedule.StartMinutes = 00;
//Add schedule to trigger in a list
trigger.Schedules = new List<Schedule>();
trigger.Schedules.Add(schedule);
Above code is using TeamFoundation.Build.WebApi to create trigger but according to latest comment we can't set triggers using the Same TFS sdk.
What is the REST Api structure for creating and scheduling triggers in the devops repository?
Thanks in advance
Upvotes: 0
Views: 587
Reputation: 35
As per microsoft community this feature is not yet possible with Tfs .net api.
Upvotes: 0
Reputation: 1122
Hi I checked the doc for BuildDefinition.Triggers Property, and it shows that triggers could only be listed and does not have other property.
Property Value
List BuildTrigger
And I also check this sdk in visual studio. And the trigger could only be listed.
So I suppose that you could use rest api to customize your pipeline trigger property in C#. I tried to capture the rest api in UI and found below.
PUT https://dev.azure.com/<org>/<project>/_apis/build/definitions/<pipelineID>?api-version=5.0-preview.6
===============================================================
Update 1/6
During more investigations with set the schedule trigger with rest api, I found modifying the request body is overcomplicated, especially setting the schedule date.
The schedule date in the request body is not using "Monday, Tuesday...Sunday", but using parameter daystobuild
with numbers value like "only Monday as 1
", "only Friday as 16
", "both Saturday and Sunday as 96
".
request body
"triggers": [
{
"branchFilters": [],
"pathFilters": [],
"settingsSourceType": 2,
"batchChanges": false,
"maxConcurrentBuildsPerBranch": 1,
"triggerType": "continuousIntegration"
},
{
"schedules": [
{
"branchFilters": [
"+refs/heads/main"
],
"timeZoneId": "",
"startHours": 6,
"startMinutes": 0,
"daysToBuild": 31,
"scheduleOnlyWithChanges": true
},
{
"branchFilters": [
"+refs/heads/1"
],
"timeZoneId": "",
"startHours": 3,
"startMinutes": 0,
"daysToBuild": "saturday",
"scheduleOnlyWithChanges": true
}
],
"triggerType": "schedule"
}
],
So I suggest that you could set the schedule trigger via Azure DevOps UI directly for both classic and yaml. And yaml pipeline alos got the Cron Definition
And there is also another method for schedule the pipeline programmatically. You could create a console app with rest api method like below.
using RestSharp;
using System;
namespace ConsoleApp18
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
var url = "https://dev.azure.com/{organization}/{project}/_apis/pipelines/{pipelineId}/runs?api-version=7.0";
string pat = "{PAT}";
var client = new RestClient(url);
var request = new RestRequest(url, Method.Post);
request.AddHeader("Authorization", "Basic "+pat);
request.AddHeader("Content-Type", "application/json");
var body = @"{
" + "\n" +
@""":"""
" + "\n" +
@"}";
request.AddParameter("application/json", body, ParameterType.RequestBody);
var response = client.Execute(request);
Console.WriteLine(response.Content);
}
}
}
And after build the app, you could set task schedule for this application. It also supports some special schedules, and in my test, I set as "on Monday and Tuesday every 3 weeks"
Upvotes: 1
Reputation: 59035
You set schedules on the build definition itself. You don't set a schedule on a queued instance of the build. You'd use the UpdateDefinitionAsync
method in the API libraries to accomplish that.
Or if you're using YAML, you define the schedule in the YAML.
Upvotes: 0