TFS Build and Release Management Definition Automation Tutorial (Part 1)

Issue:

I would like to setup a build definition that will run when I check in my application and database projects. Then I want my artifact to automatically release to production environment. Where do I start?

Environment:

Team Foundation Server (TFS) 2017, Visual Studio (VS) 2017, Build Server (BS) 2017, ASP.Net Project, SQL Server 2016

Resolution:

Here are step-by-step instructions on how to setup build definition that will create an artifact for Release Management to publish code automatically. This definition includes creating both application code and database DACPAC file for publish. For Release Management definition instructions, please refer to TFS Build and Release Management Definition Automation Tutorial (Part 2).

Step-by-step instructions:-

  1. Create a new build definition by clicking on “+ New definition”
  2. Select an ASP.NET template for predefined tasks
  3. Click on “Variables” tab, enter “Debug,Test,Qa,Release” next to BuildConfiguration for transforming those configurations to correct environment in web.config. You will need to predefine these configurations in your VS solution. I.e., web.debug.config, web.test.config, etc.
  4. Click on “Triggers” tab, Click on enable for Gated Check-in and select checkbox on “Run continuous integration…”.
  5. Click on “Options”, enable Multi-configuration and enter “BuildConfiguration” under Multipliers field.
  6. Click on “Tasks” tab and select “Get sources” task, Enter local path to map to server path.
  7. Select “Build solution” task, copy and paste below arguments to MSBuild Arguments field. This step is crucial to generate a successful artifact!

    MS Build Arguments*:

    /p:DeployOnBuild=true /p:WebPublishMethod=FileSystem /p:PrecompileBeforePublish=true /p:DeployDefaultTarget=WebPublish /p:publishUrl="$(build.artifactstagingdirectory)\$(BuildConfiguration)\wwwroot"
  8. (Optional) Add a Copy task to copy dacpac file and publish profile xml file to artifact folder. See below for setup. This step is to publish your database changes to target server by Release Management with SQLPackage.

    Source Folder: $(Build.SourcesDirectory)
    Contents: **\bin\$(BuildConfiguration)\**.dacpac**\**$(BuildConfiguration).publish.xml
    Target Folder: $(build.artifactstagingdirectory)\$(BuildConfiguration)\dbroot
  9. Click on “Save” link to save your build definition or “Save & queue” to save and run build.
  10. Once the build is succeeded, click on “Artifacts” link and then “Explore” link to explore artifacts.
  11. If artifacts looks good, continue to create a new Release Definition: TFS Build and Release Management Definition Automation Tutorial (Part 2)

 

Troubleshoot:

  1. If build failed, check build log for error detail by clicking on “Builds” tab. Then click on completed build with date suffix and select “Build (debug)”. The error should shown on your right panel with red text. Release management will not kick in if build failed.
  2. This is sample build error that can be fixed by including the missing assembly reference (dll).

 

Reference:

Leave a Reply