Deploy remotely with TFS build

It is time to connect together a couple of posts of mine, in the first I simply explained how to deploy a web application to a remote machine with the use of Beyondexec2, in another one I explained how to create a simple tfs build, that actually does not build anything, but execute a simple workflow.

In this post I’ll cover a primitive build workflow to deploy the result of another build. The starting point is having a build called “Demo” that builds a web site and create the installer package, plus the script described here. You need to insert the scripts and the PsExec utility in the source code of your team project, to be available from the build agent during the build, as shown in Figure 1. Note: in this example I’ll use the PsExec tools instead of beyondexecv2, but they are exactly equivalent, PsExex is more maintained tool and works better when execute in services.

Untitled

Figure 1: Include deploy related files into source control system

Now you need to modify the deploy script created in the other blog post, adding all the operations needed to deploy a build, first of all you need to define some more parameters (Figure 2). These one are the number of the build to use (Es. demo_20100607.3) machine name where you want to install and the password of the administrator account of that machine.

image

Figure 2: Parameters of the workflow

Now, since the tools and script to do remote deploy are stored in source control system, the build scripts needs to create a workspace and do a getlatest; to do this, you can reuse the relative section of the standard workflow showed in Figure 3.

Untitled2

Figure 3: Details of workspace management

The steps from Figure 3 is taken from the standard workflow, and it is a common sequence of operation to create a workspace and do a getlatest plus managing some variables. If you run this workflow, as is, you can verify that in the build machine a new workspace is created, and you can browse to the build directory (usually c:\builds\1\teamprojectname\etcetc) to see downloaded files. But before doing this, you need to specify folder to grab in the workspace section of build configuration. As you can see in Figure 4, I simply need to grab the BuildTools subdirectory, because there is no need to do a get latest of project sources, but only of the deploy scripts.

image

Figure 4: Configuration of the workspace
Now I need only to execute the PsExec process to do a remote execution of the script in the machine where I want my web application to be deployed, and this can be done thanks to a simple Invoke Process Activity, as shown in Figure 5.
Untitled

Figure 5: Invoke Process activity permits execution of an external process.

FileName property specify the process to execute, for this example is

SourcesDirectory + "\psexec.exe"

Since SourcesDirectory is the one used to map the workspace, I can execute the psexec directly from there. The other important property is the Arguments one:

"\\" + DeployMachine +  " -u " + DeployMachine + "\administrator -p " + DeployMachinePassword +
" /accepteula -i -f -h -c " + SourcesDirectory + "\deploy\Deployweb.bat " + BuildToUse

This is only a combination of Workflow Parameters to create the argument list, the /accepteula parameter is needed because the psexec shows an eula that should be accepted, and clearly there is noone to click on accept when executed on a service Smile, then the option –c force a file to be copied to remote computer and executed. After the Invoke process, in Figure 6 I showed the end of the workflow, with a condition that verifies if the PsExec return value is zero (success) or greater than zero (error).

Untitledw

Figure 6: Check return value of PsExec and fail the build if greater than zero.

The SetBuildProperties activity permits to set a property of the build, in this situation I set the status as Failed. Now you can configure a build, configure parameters and see the result.

image

Figure 7: Log of a successful build.

The only drawback is that you only see output of the psexec program and does not see the output of the execution of DeployWeb.Bat on remote machine. Since you can specify, machine and build number to use, this is a good build script to deploy something on remote machine with a simple click.

alk.

P.S. this is the first post following Adam Cogan’s SSW Rules (thanks Adam, you rock)

– the balloon rule, instead of walls of text

http://sharepoint.ssw.com.au/Standards/Communication/RulesToBetterEmail/Pages/HowToUseBalloons.aspx

– the figure/caption rule

http://www.ssw.com.au/ssw/Standards/Rules/RulesToBetterWebsitesLayout.aspx#AlwaysAddFiguretoImages

Wrap a MsBuild Custom task inside a custom action

If you have an MSBuild custom task that you want to reuse in a TFS 2010 build workflow, you have two solution. The first is using the MsBuild activity as I described in this post, but this approach has a lot of limitations.

First of all it is clumsy, because you have to pass custom task parameters as arguments to msbuild, but the worst problem is that you lose the ability to use output properties of the custom task. Suppose you have a TinyUrl custom task, that takes an url as input and gives back the tined version, this custom task has this implementation.

image

Now suppose you do not have this source code, so you really need to use the MsBuild Custom Task; if you simply use the MsBuild activity as described in the previous post, how can you grab the TinedUrl output property and pass its value to the workflow engine?

To solve this problem you can use another approach to reuse a Custom MsBuild task in a tfs 2010 build, because you can wrap the task execution in a custom activity. First of all we need to fool the Custom MsBuild Task that it is executing inside MSBuild. A first problem is, how can I intercept the inner calls to Log.LogMessage or Log.LogWarning that are inside the CustomTask and pass them to the workflow engine? The solution is this simple class.

class WorkflowBuildEngine : IBuildEngine
{
    public CodeActivityContext Context { get; set; }

    public WorkflowBuildEngine(CodeActivityContext context)
    {
        Context = context;
    }

   ...

    public void LogErrorEvent(BuildErrorEventArgs e)
    {
        Utils.LogError(e.Message, Context);
    }

    public void LogMessageEvent(BuildMessageEventArgs e)
    {
        Utils.LogMessage(e.Message, Context, BuildMessageImportance.Normal);
    }

    public void LogWarningEvent(BuildWarningEventArgs e)
    {
        Utils.LogWarning(e.Message, Context);
    }

    public string ProjectFileOfTaskNode
    {
        get { throw new NotImplementedException(); }
    }

It Implements IBuildEngine, its constructor requires a CodeActivityContext that is used inside the LogErrorEvent, LogMessageEvent and LogWarningEvent methods to forward log message issued by the custom task to the workflow engine. In this way every log that takes place inside the MsBuild custom Action gets forwarded into the workflow engine. Finally you need to create the TinyUrl custom activity that wraps the custom MsBuild task:

public sealed class TinyUrl : CodeActivity<String>
{
    [RequiredArgument]
    public InArgument<string> Url { get; set; }

      protected override String Execute(CodeActivityContext context)
    {
        TinyUrlTask wrappedTask = new TinyUrlTask();
        WorkflowBuildEngine engine = new WorkflowBuildEngine(context);
        wrappedTask.BuildEngine = engine;
        wrappedTask.Url = Url.Get(context);
        if (!wrappedTask.Execute())
        {
            Utils.LogError("Tiny url task failed", context);
        }
       return wrappedTask.TinedUrl;
    }
}

The first important aspect is that it inherits from CodeActivity<String> instead from a simple CodeActivity, this because this activity will return a string (the tined url) so the type parameter instruct the workflow on the return type of the action. The execute is different too because it should return the result of the action. As you can see the first operation is creating the MsBuild custom task and a WorkflowBuildEngine that gets assigned to the BuildEngine property of the task. After the Engine is ok, you need to populate all the input properties of the MsBuild Custom task, and then call execute.

If the return value of execute is false the action logs the error (so the build partially fails) and finally returns the value to the caller because output properties of MsBuild custom Tasks are simple properties, so the tined url is in the TinedUrl property of the task. The good part of this technique is that you can use this action from the graphical designer.

image

If you compare with the approach that uses MsBuild Activity you have several advantages. First you can use the graphical designer, then you can edit the property with the full editor of workflow foundation and finally you can use output properties. I inserted a WriteBuildMessage after the TinyUrl Custom Activity to verify if the TinedUrl property is correctly set by the action. If you run the build you can verify that everything is good.  I placed two TinyUrl custom activity inside the workflow, the second one tiny the url www.c.com, just to trigger the warning inside the MsBuild custom Task.

image

If you look at the first picture of this post, you can verify that the warning “There is no need to tiny the url because is less than 20 chars” is a warning issued internally by the custom MsBuild task, and you are looking at it thanks to the WorkwlofBuildEngine class that forward MsBuild log calls to workflow environment.

alk.

Tags:

Log warning and errors in a custom action

Some time ago I blogged about logging in custom action for TFS build 2010, I left out some details. Suppose you want to create a warning or an error and not a simple message, you need to create a specialized version of the LogWarning that logs a real warning.

image

You can do the same with errors.

image

These two methods permit you to log warnings and errors during a custom build action execution, let’s see how they affect the output. First of all you can verify that when you log an error the build partially succeeds

image

The error and warning are reported in the detailed report with their right icons

image

And they are also reported in the “View Summary” of the build

image

Next time I’ll explain you how to wrap a msbuild Custom Task in a custom action.

Alk.

Tags:

Branching policies

I just read this post of Martin Fowler, and I found it very interesting. In my opinion, even small projects will greatly benefit from Continuous Integration. Despite of the Branching policies that you choose, having a machine for CI is vital during the lifetime of a project.

Usually I do not like very much CherryPicking even if sometimes it cannot be avoided. In the Promiscuous Integration model, people are doing CherryPicking from other branches and this scares me. The purpose of a branch is to keep changes isolated until they are ready to be moved in the trunk, or to keep copies of specific version of the software, and usually merging between branches can be problematic. Some Source control system do not permit to merge changes between branches that are not contiguous.

In the example made by Fowler, with Promiscuous Integragion, DrPlum and Reverend Green works together in two different branches, but since they have great communication, they periodically merge from one branches to another, but this scheme is a little confused in my opinion.

A better strategy could be this one. Since DrPlum and Reverend Green have great communication, it can be possible to work in different way. Say DrPlum decide to create a new big feature, so he creates a branch and starts to work on that branch. Suppose he do not want to integrate in the trunk until it is completely finished, so he do not use CI.  He ask to other members of the team if someone is working on a branch, all people say no, so he create a new branch and start working on it.

After some time Reverend Green want also to create a new big feature, he ask to the team, and DrPlum says him that he is also working on a feature that have some code in common with the new feature of REverend Green. At this time both Reverend Green and Dr Plum create another branch from the original branch of DrPlum. The situation is the following, we have the trunk, then we have the first branch of DRplum (call it B1), from this branch we have two other branches, one for Reverend Green (B2G), and the other for DrPlum (B2P). Now we can configure a Continuos Integration Machine to integrate B1. Now both DrPlum and Reverend green are working on isolated branches, but they merge changes often with B1, so they never need a big merge. In this scheme B1 acts like a trunk for both developers. As bugfix or changes are made in the trunk, one of them bring those changes in B1, verify that all tests are good, and when B1 is stable again, each one propagates changes from B1 to his own branch.

With such a scheme, two developers can work together on new feature that have code in common, without the risk of big merge, using Continuos Integration, but avoiding put stuff in the trunk until the work is finished. Say Reverend Green has finished his feature, he first integrate all trunk changes in B1, verify that everything is ok, then merge changes from B1 to B2G, when everything is ok he merge remaining changes from B2G to B1 (in the meanwhile DrPlum could have changed B1), then from B1 to the trunk, and the game is done. The same is done from DrPlum when he finished.

I admit that I never used such a complex scheme, because I prefer to have developers continuously merge in the trunk, and thanks to CC.net integration problems are mitigated.

Alk.

Tags: