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:-
- Create a new build definition by clicking on “+ New definition”
- Select an ASP.NET template for predefined tasks
- 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.
- Click on “Triggers” tab, Click on enable for Gated Check-in and select checkbox on “Run continuous integration…”.
- Click on “Options”, enable Multi-configuration and enter “BuildConfiguration” under Multipliers field.
- Click on “Tasks” tab and select “Get sources” task, Enter local path to map to server path.
- 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"
- (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 - Click on “Save” link to save your build definition or “Save & queue” to save and run build.
- Once the build is succeeded, click on “Artifacts” link and then “Explore” link to explore artifacts.
- If artifacts looks good, continue to create a new Release Definition: TFS Build and Release Management Definition Automation Tutorial (Part 2)
Troubleshoot:
- 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.
- This is sample build error that can be fixed by including the missing assembly reference (dll).
Reference: