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

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:

For Build definition instruction, please refer to TFS Build and Release Management Definition Automation Tutorial (Part 1). Here are step-by-step instructions on how to setup release definition that will publish code and database automatically. This release definition uses artifact created by build definition previously with different build configurations to publish to different environments.

Step-by-step instructions:-

  1. Create a new Release definition by clicking on “Releases” tab and “+ New definition” link.
    new release definition
    Or by clicking on “Releases” tab, then click on “+” dropdown and select “Create release definition” link.
  2. Choose “Empty” template and click “Next”.
  3. Select the correct build definition from “Source (Build definition)” dropdown that created previously on part 1. Click on checkbox for “Continuous deployment …”. Then click on “Create” button to create a new release definition.
  4. Rename your Release definition and Environment name
  5. Click on “Artifacts” tab and verify that the source is correct from the build you created on part 1.
  6. Click on “Triggers” tab and verify that “Set trigger on artifact source” dropdown is correctly selected.
  7. Click on “Environments” tab and “+ Add tasks” link to add a task. Do not click on dropdown and select “Add an agent phase” unless you are doing something else.
  8. Click on “Utility” tab, scroll down to “PowerShell” task and click on “Add” button twice to add two PowerShell tasks.

  9. Rename to “App Deployment Script” and enter script path and arguments as shown below. Make sure AppPackageV1.0.0.0.ps1 Custom PowerShell script is accessible by build server.

    Script Path: \\put-this-ps-on-your-build-server-share\AppPackageV1.0.0.0.ps1
    Arguments: $(my_Target_Path1) $(my_Encrypt_Cfg) $(my_Cleanup_Target) $(my_Source_Path)
  10. Rename to “Database Deployment Script” and enter script path and arguments as shown below. Make sure SqlPackageV1.0.0.0.ps1 Custom PowerShell script is accessible by build server.

    Script Path: \\put-this-ps-on-your-build-server-share\SqlPackageV1.0.0.0.ps1
    Arguments: $(my_Source_DACPAC_File) $(my_Publish_Profile)
  11. Next is to convert PowerShell task to Task Group so that it can be called easily with variables. To convert to Task Group, right click on “App Deployment Script” and select “Create task group”.
  12. Enter “App Deployment Task” on Name field and click on “Create”.
  13. As you can see below, it’s using “App Deployment Task” task group with variables on the right hand side. By doing this, you can deploy your code to multiple location with new “App Deployment Task” task found under “Add tasks” link.
    Custom PowerShell Script (PS) Variable Description:

    my_Target_Path1 = Target folder (i.e., web server UNC path: \\webserver1\demo)
    
    my_Source_Path = Source folder (i.e., build artifact wwwroot folder: $(System.DefaultWorkingDirectory)\Project_Demo_Build\drop\[Debug]\wwwroot)
    
    my_Encrypt_Cfg = Encrypt web.config sections (0 = disable encrypt; 1 = enable encrypt (default))
    
    my_Cleanup_Target = Delete target folder (0 = disable delete; 1 = enable delete (default))
  14. Do the same for “Database Deployment Task” from step 11 to 13 and you will end up with “Database Deployment Task” task group as shown below.

    Custom PS Variable Description:

    my_Source_DACPAC_File = Source DACPAC database file (i.e., build artifact dbroot folder: $(System.DefaultWorkingDirectory)\Project_Demo_Build\drop\[Debug]\dbroot\Proj.Demo.Database.dacpac)
    
    my_Publish_Profile = Predefined publish profile for database (i.e.,  $(System.DefaultWorkingDirectory)\Project_Demo_Build\drop\[Debug]\dbroot\Proj.Demo.Database.[Debug].publish.xml)
  15. Click on “Run on agent” link to select a deployment queue/pool.
  16. In case you wonder, Task Groups can be found by clicking on “Task Groups” link as shown below.
  17. Click on “Save” link to save your release definition.

 

Troubleshoot:

  1. If you see “The model version ‘2.9. is not support.”, this could be the wrong version of SQLPackage.exe is being used. Make sure to use 2017 SQLPackage.exe.

Reference:

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: