TFS Build not transforming web.config as expected

筅森魡賤 提交于 2019-11-27 17:37:10

Previously I had been doing something similar to the other answers. However, I just found what seems to be a better solution to this problem. Just add "/p:UseWPP_CopyWebApplication=true /p:PipelineDependsOnBuild=false" to your MSBuild arguments. I just tried this out on one of my TFS builds and it works just fine.

I found this great tip here: http://www.andygeldman.com/index.php/2011/10/web-and-app-config-transformations-with-tfs-build.

Danny

TFS Team Build 2010 does not automatically transform your Web.configs. You need to add a custom workflow activity to your build process template to accomplish this.

Edwald Hofman has a good blog that explains how to modify TFS 2010 Build Process Templates, so I won't go in depth on that here.

http://www.ewaldhofman.nl/post/2010/04/29/Customize-Team-Build-2010-e28093-Part-4-Create-your-own-activity.aspx

After you figure out how to add custom activities to your build process template, add the following activity into your workflow, I added the activity after "Drop FIles to Drop Location". It utilizes the Microsoft.Web.Publishing.Tasks assembly (located: C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\Web) to perform the transformations:

/// <summary>
/// Transforms configuration files using TransformXml
/// </summary>
[BuildActivity(HostEnvironmentOption.All)]
public sealed class WebConfigTransform : CodeActivity
{
    #region Public Properties

    /// <summary>
    /// The binaries folder
    /// </summary>
    [RequiredArgument]
    public InArgument<string> BinariesLocation { get; set; }

    #endregion

    #region Overrides of CodeActivity

    /// <summary>
    /// When implemented in a derived class, performs the execution of the activity.
    /// </summary>
    /// <param name="context">The execution context under which the activity executes.</param>
    protected override void Execute(CodeActivityContext context)
    {
        var binariesFolder = context.GetValue(BinariesLocation);

        foreach (var sourceFolder in Directory.GetDirectories(Path.Combine(binariesFolder, "_PublishedWebsites")))
        {
            var sourceFile = Path.Combine(sourceFolder, "Web.config");
            if (File.Exists(sourceFile))
            {
                var filesToTransform = Directory.GetFiles(sourceFolder, "Web.*.config");


                foreach (var fileToTransform in filesToTransform)
                {

                    var tempSourceFile = Path.GetTempFileName();
                    var tempTransformFile = Path.GetTempFileName();

                    File.Copy(sourceFile, tempSourceFile, true);
                    File.Copy(fileToTransform, tempTransformFile, true);

                    var transformation = new TransformXml
                    {
                        BuildEngine = new BuildEngineStub(),
                        Source = tempSourceFile,
                        Transform = tempTransformFile,
                        Destination = fileToTransform
                    };

                    transformation.Execute();
                }
            }
        }
    }

    #endregion
}

You will need to pass it the droplocation in the work flow. When you add it the the work flow, right click on the activity, then go to properties, and paste "DropLocation" (VB Expression) into the property "BinaryLocation"

NOTE: You'll need to create a BuildEngineStub class that implements the IBuildEngine interface in order to use the MSBuild task. Here is what I used

public class BuildEngineStub : IBuildEngine
{
        #region IBuildEngine Members

        public bool BuildProjectFile(string projectFileName, string[] targetNames,
                                     IDictionary globalProperties,
                                     IDictionary targetOutputs)
        {
            throw new NotImplementedException();
        }

        public int ColumnNumberOfTaskNode
        {
            get { return 0; }
        }

        public bool ContinueOnError
        {
            get { return false; }
        }

        public int LineNumberOfTaskNode
        {
            get { return 0; }
        }

        public string ProjectFileOfTaskNode
        {
            get { return ""; }
        }

        public void LogCustomEvent(CustomBuildEventArgs e)
        {
            Console.WriteLine("Custom: {0}", e.Message);
        }

        public void LogErrorEvent(BuildErrorEventArgs e)
        {
            Console.WriteLine("Error: {0}", e.Message);
        }

        public void LogMessageEvent(BuildMessageEventArgs e)
        {
            Console.WriteLine("Message: {0}", e.Message);
        }

        public void LogWarningEvent(BuildWarningEventArgs e)
        {
            Console.WriteLine("Warning: {0}", e.Message);
        }

        #endregion
    }
Danny

Here's what I've been using. The current TransformXml task has a bug where it leaves files open. Read more here.

You can call this task and deploy for each configuration you're working with.

<Target Name="TransformWebConfig">

    <PropertyGroup>
        <_tempSourceFile>$([System.IO.Path]::GetTempFileName())</_tempSourceFile>
        <_tempTransformFile>$([System.IO.Path]::GetTempFileName())</_tempTransformFile>
    </PropertyGroup>

    <Copy SourceFiles="$(_websiteDirectory)\Web.config" DestinationFiles="$(_tempSourceFile)"/>
    <Copy SourceFiles="$(_websiteDirectory)\Web.$(_transformConfiguration).config" DestinationFiles="$(_tempTransformFile)"/>

    <MSBuild.Community.Tasks.Attrib Files="$(_websiteDirectory)\Web.config" ReadOnly="false" />

    <TransformXml Source="$(_tempSourceFile)"
                  Transform="$(_tempTransformFile)"
                  Destination="$(_websiteDirectory)\Web.config"
                  StackTrace="false" />
</Target>

Try not to set the Build Platform - basicly delete "Any CPU" in ItemToBuild and select MSBuild platform as "Auto"

Kn0wit

The Drop does not actually do any transforming. What you need is to add /p:DeployOnBuild=Trueto the MSBuild Arguments.

This will create a Package that can then be used to install the website either via command line or using the IIS Import Application wizard.

If what you are after is to directly publish more than one configuration that is a whole other story and that is how I stumbled onto this post.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!