TFS2012: Schedule a One Time Team Build–Approach 1
In response to the questions asked on MSDN social, I am writing this blog post. The question was “How to schedule a one off build”. I also posted an alternative approach here
Introduction
When creating a scheduled build, you need to specify the the day of week and time.
But what if you only want to schedule it to run once? That scenario is not supported by TFS out of the box.
Solution
One of the solutions I am proposing in this post is to create a scheduled build definition and a custom activity to reset the build definition trigger to manual when the build is complete. The limitation for this approach is you can’t specify a date (e.g. 22/1/2013). In other words, you can only schedule it for the next 7 days.
I am using TFS2012 and Visual Studio 2012 for this solution. You can still use the same steps for TFS 2010.
Creating Build Activity
Create new project of type Activity Library and name it TeamBuildActivities
Delete Activity1.xaml
Add new item of type Code Activity and name it SetTeamBuildDefinitionTriggerToManual
Add a reference to C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ReferenceAssemblies\v2.0\Microsoft.TeamFoundation.Build.Client.dll
Paste the following code in the SetTeamBuildDefinitionTriggerToManual .cs file
using System.Activities; using System.ComponentModel; using Microsoft.TeamFoundation.Build.Client; namespace TeamBuildActivities { [BuildActivity(HostEnvironmentOption.All)] public sealed class SetTeamBuildDefinitionTriggerToManual : CodeActivity { [Browsable(true)] [DefaultValue(null)] public InArgument<IBuildDetail> BuildDetail { get; set; } // If your activity returns a value, derive from CodeActivity<TResult> // and return the value from the Execute method. protected override void Execute(CodeActivityContext context) { if (BuildDetail == null) { return; } IBuildDetail buildDetail = context.GetValue(BuildDetail); if (buildDetail == null) { return; } buildDetail.BuildDefinition.TriggerType = DefinitionTriggerType.None; buildDetail.BuildDefinition.Save(); } } }
Save and build the project
Copy the output assembly TeamBuildActivities.dll to C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\PrivateAssemblies
Create a build definition
Create your build definition with a scheduled trigger
On the process, click show detail and then the new button
You should rename the New file name, I am keeping the default name in this example
Click OK
Open the Source Control Window
Right click on the new build process template and get latest
Double click on the new build process template to open
Open the Toolbox window
Create a new Tab and call it Custom Activities
Right click on the custom activity area and select Choose Items
Make sure that System.Activities Components Tab is selected
Click on the browse button
Go to the C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\PrivateAssemblies folder and select the TeamBuildActivities.dll and click OK
Drag and drop the activity at the end of the workflow
Type in the BuildDetail argument into the BuildDetail property grid
Save workflow template and then check it in
Create new Folder in TFS Source Control for Build Activities
Go to the Source Control Windows
Create new folder, I am naming it TeamBuildAssemblies
Open the local folder and copy TeamBuildActivities.dll to it
Go back to source control
Right click on the folder you created and select Add Items to folder
Select TeamBuildActivities.dll from the window
Check in the folder
Point Controller to the TeamBuildAssemblies
At this point, we want the team build to load the custom activity we created to initiate an instance of the SetTeamBuildDefinitionTriggerToManual activity
Click on Actions
Then Manage Build Controllers
Click on Properties
Select the path for the TeamBuildAssembles folder
Click OK
Queue a Build
Lets queue a build to simulate the running of a scheduled build
You may get the following error
TF215106: Access denied. NT AUTHORITY\NETWORK SERVICE needs Edit build definition permissions for build definition TeamBuildActivities in team project MyTeamProject to perform the action
To fix the error you need to give “Edit build definition” permission to the build account
Right click on the team build definition
Select Security
Select Build Service Accounts
Change the value for Edit build definition to allow
SCROLL DOWN AND CLICK SAVE CHANGES. For low resolution screens this may not be so intuitive because the button is hidden
Now queue another build
Edit the build definition and click on the Trigger tab
Notice that it is switched to Manual
Nice post. Very useful. I’m not sure you can do this in TFS 2010 though because I don’t think the Build Definition has a TriggerType property in that version.
Further to my previous comment I think you can achieve the same in TFS 2010 using
buildDefinition.ContinuousIntegrationType = ContinuousIntegrationType.None;
Thanks.
I can’t confirm since I don’t have TFS 2010. But thanks for posting, other will find that helpful. Cheers