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:

Object already exists when trying to use aspnet_regiis to encrypt web.config connectionStrings section

Issue:

When I tried to encrypt my web.config connectionStrings section with aspnet_regiis, I got an error message saying Object already exists. What happen?

Administration utility to install and uninstall ASP.NET on the local machine.
Copyright (C) Microsoft Corporation.  All rights reserved.
Encrypting configuration section...
An error occurred executing the configuration section handler for connectionStrings.


Failed to encrypt the section 'connectionStrings' using provider 'MyIisRsaProv'. Error message from the provider: Object already exists.


Failed!

Environment:

Windows 10, .Net Framework v4.0.30319, PowerShell ISE or Command Prompt

Resolution:

In order to run aspnet_regiis to encrypt configuration section(s), you need to run PowerShell ISE or Command Prompt with Run as administrator

Step-by-step instructions:-

  1. Click on search on your Windows 10 machine and type powershell or command prompt

  2. Right click on Windows PowerShell ISE and select “Run as administrator”
  3. Enter your encryption command and run, ex., C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis -pef connectionStrings c:\temp\ -prov MyIisRsaProv
  4. You should get succeed result as below:

    Microsoft (R) ASP.NET RegIIS version 4.0.30319.0

    Administration utility to install and uninstall ASP.NET on the local machine.

    Copyright (C) Microsoft Corporation. All rights reserved.

    Encrypting configuration section...

    Succeeded!

Troubleshoot:

If you still having problem, make sure you run PowerShell or Command Prompt as Administrator by checking Window Title:
 

Reference:

Sample PowerShell to encrypt existing configuration sections:

        $app_path = "c:\your\Web.Config\folder"
        Write-Host "> connectionStrings"
        $bol_encrypt_config_here = 0
        Get-Content $app_path\web.config | ForEach-Object {if($_ -match "connectionStrings"){ $bol_encrypt_config_here = 1 }}
        if($bol_encrypt_config_here) { C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis -pef connectionStrings $app_path -prov MyIisRsaProv }
       
        Write-Host "> machineKey"
        $bol_encrypt_config_here = 0
        get-content $app_path\web.config | foreach-object {if($_ -match "machineKey"){ $bol_encrypt_config_here = 1 }}
        if($bol_encrypt_config_here) { c:\windows\microsoft.net\framework\v4.0.30319\aspnet_regiis -pef system.web/machineKey $app_path -prov MyIisRsaProv }
       
        Write-Host "> sessionState"
        $bol_encrypt_config_here = 0
        get-content $app_path\web.config | foreach-object {if($_ -match "sessionState"){ $bol_encrypt_config_here = 1 }}
        if($bol_encrypt_config_here) { c:\windows\microsoft.net\framework\v4.0.30319\aspnet_regiis -pef system.web/sessionState $app_path -prov MyIisRsaProv }
       
        Write-Host "> appSettings"
        $bol_encrypt_config_here = 0
        get-content $app_path\web.config | foreach-object {if($_ -match "appSettings"){ $bol_encrypt_config_here = 1 }}
        if($bol_encrypt_config_here) { c:\windows\microsoft.net\framework\v4.0.30319\aspnet_regiis -pef appSettings $app_path -prov MyIisRsaProv }