Exclude/Skip files in VSTS Build and release - azure-devops

We are in process of creating architecture for VSTS CI/CD to deploy our web app to our Azure App Services.
We want to exclude the web.config while deploying it to the Azure server as we are directly modifying the web.config on the different environment.
CI Tasks looks like this:
CI Taks
CD Task:
Deploy Azure App Service
I am aware of other ways of updating the web.config https://learn.microsoft.com/en-us/vsts/build-release/tasks/transforms-variable-substitution, but in our case we want to skip the web.config file.
I couldn’t find the option to skip file in during release in VSTS as mentioned in this thread
How do I exclude the .cs files within an artifact from a vs-team-services CI build?
Is there a way to exclude certain files while building and deploying the release?

Added -skip:objectName=filePath,absolutePath=web\.config in additional arguments. This skips updating the web.config file during deployment.

You can exclude the web.config before publishing artifacts in your build definition: copy the web packages files to a directory (such as $(build.binariesdirectory)), then copy the files exclude web.config to another folder (such as $(Build.ArtifactStagingDirectory)/package), and zip the files under $(Build.ArtifactStagingDirectory)/package. And finally publish the zip file as build artifacts.
Details changes in the build definition as below:
Change the MSbuild arguments as /p:OutDir="$(build.binariesdirectory)\\" in Visual Studio Build task.
Add a Copy Files task after Visual Studio Build task. Settings for this task as below:
Add Archive Files task after Copy Files task. And settings as below:
Change the Publish Artifacts task as below:
Now the build artifacts are exclude web.config file.

Additional arguments
-skip:objectName=filePath,absolutePath=\\Configuration\\AppSettings\\Base.config

you can add
-skip:objectName=filePath,absolutePath='.*\PackageTmp\Web.config$'
in Additional Arguments in "Deploy IIS WebSite/App" deployment VSTS task, this will not deploy your root web.config file.

Related

Azure devops build pipeline

I have created a sample Website project with a single page having HellowWord.aspx and HellowWord.aspx.cs. I am trying to create an Azure DevOps build pipeline for this project. The following are the tasks I have added to my build package.
But the publish artifacts always contains the aspx and aspx.cs file. Not sure which tasks I am supposed to add to make the proper publish package. Which will create proper dlls for .aspx file instead of aspx.cs.
Publish build artifacts task has an argument Path to publish, which defines the folder or file path to publish. This can be a fully-qualified path or a path relative to the root of the repository. Wildcards are not supported. Variables are supported. Example: $(Build.ArtifactStagingDirectory). By default, this argument uses variable $(Build.ArtifactStagingDirectory).
So you need to check your Copy files task, to see what you have copied to $(Build.ArtifactStagingDirectory), and copy the correct files in this task.
https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/copy-files?view=azure-devops&tabs=yaml

How to include my config transformation files in the web deploy zip?

Im setting up a new build and deploy pipeline in Azure Devops. It is an older Web application with some transformation files for the web.config. In the old days we would build the same code x times depending on how many environments. This this is no longer necesary as I read from here https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/transforms-variable-substitution?view=vsts#xmltransform
it looks like the deploy pipeline can pick up the changes from my transform file.
But the problem is that my other transform files does not get included in the package so I get these warning message:
[warning]Unable to apply transformation for the given package. Verify the following.
[warning]1. Whether the Transformation is already applied for the MSBuild generated package during build. If yes, remove the <DependentUpon> tag for each config in the csproj file and rebuild.
[warning]2. Ensure that the config file and transformation files are present in the same folder inside the package.
And yes when i download the artifact the Web.[stage].config files are not there as suggested.
Is there some setting somewhere that let me include these files? Or stop them from being transformed?
For Web applications
MSBuild knows how to transform the web.config files based on the following settings/properties/parameters (in order)
Build Configuration dotnet publish --configuration Release
Publish profile dotnet publish --configuration Release /p:PublishProfile=FolderProfile
Environment dotnet publish --configuration Release /p:EnvironmentName=Production
Custom file transform dotnet publish --configuration Release /p:CustomTransformFileName=custom.transform
I think it's typical for developers to make this happen based on build configuration only, and I believe MSBuild (and dotnet) know how to do this based on the <DependentUpon>Web.config</DependentUpon> element in the Web.[configuration].config item in the project or build script file.
Azure DevOps Release Pipelines is a little different.
The pipeline wants to transform your web.config after the project has been built/published and doesn't know how to do that if MSBuild (or dotnet) has already made an attempt at it. Thus:
[warning]Unable to apply transformation for the given package. Verify the following.
[warning]1. Whether the Transformation is already applied for the MSBuild generated package during build. If yes, remove the tag for each config in the csproj file and rebuild.
[warning]2. Ensure that the config file and transformation files are present in the same folder inside the package.
The warning text states:
Remove the <DependentUpon> tag for each config in the csproj
Thus: you need to remove the tag from the csproj to prevent MSBuild from transforming the files
Or: you need to use the /p:TransformWebConfigEnabled=False argument to MSBuild. (note: I believe it is correct that this can be used w/o removing the dependent upon tag, but I could be wrong)
Make sure the transform source and target files are in the same folder inside the package.
There may be several ways to do this. I've chosen to mark the transform source config files as content to force MSBuild to include them in the published package.
Now you need to organize your release pipeline in accordance with the File Transforms and Value Substitutions documentation.
[section]Starting: IIS Web App Deploy
====================================
Task : IIS Web App Deploy
Description : Deploy a website or web application using Web Deploy
Version : 0.0.51
Author : Microsoft Corporation
Help : More information
====================================
...[command]C:...\ctt\ctt.exe s:C:...\Web.config t:C:...\Web.Release.config d:C:...\Web.config pw i
[command]C:...\ctt\ctt.exe s:C:...\Web.config t:C:...\Web.Development.config d:C:...\Web.config pw i
XML Transformations applied successfully
...
For Non-Web Applications Needing .config Transformation
Getting your .config files to the release pipeline can happen several ways. Here are two.
Your release should have "access" to the repository as a part of the artifact, which will ensure that the deploy agent downloads the source (not desirable IMHO).
You will need to include the web.[stage].config files as part of your build artifact with a copy task, or a minimatch that picks them up.
Once you have the .config files available to the release pipeline
You can use the File Transform Task or XDT Transform Task to perform the transformation operations.
Option 2 is the route I've gone.
Here is an image of what that task looks like for me.
That task puts the config in the artifact from the build that I can then use in the release pipeline without rebuilding xx times.
Cleanup
If you're in a position where you care to not have the transform files persisting on the agent after the release is complete, then you'll need to add that to your pipeline.

VSTS performs web deploy successful, but website does not work

I am trying to get Visual Studio Team Services (VSTS) to perform a web deploy of my app. According to the VSTS UI the web deploy is successful, but when I browse the deployed web site it shows the default IIS screen ("Welcome - IIS").
This is my publish step in the build pipeline:
This is my release definition:
This is the resulting deploy folder:
Build.log (with replaced values) - https://drive.google.com/file/d/1y6q2Cjr1gxBVMcHeh6n_r7qu-JpJFSyC/view
Do I need to add an additional step to the release pipeline to get the .zip files extracted?
You do not need to add an additional step to extract your zip, but you need to tell the IIS Web App Deploy task to deploy your zip file and not a folder. You have specified a folder for the Package or Folder input, so the task is doing exactly as you told it to do, deploy the specified folder.
If you only have one zip file in your build artifacts, the default value for this field, $(System.DefaultWorkingDirectory)\**\*.zip, will pickup your zip file and correctly deploy it. If you have multiple zip files in your build artifacts, you will need to specify the full path to the zip file in order for it to deploy.

Setup publish to folder using VSTS

I was using publish to folder option through Visual Studio by right-clicking on the project -> publish -> publish to folder. Result was always ready-to-copy project with applied transformations.
I wanted to automate this process using VSTS and have setup build on VSTS.
I used next steps:
- NuGet restore
- Build solution
- Publish Build Artifacts to $(build.artifactstagingdirectory)
- Windows machine file copy from $(build.artifactstagingdirectory) to remote machine using admin login and password
And finally I'm getting zip package on remote machine with complicated folder structure without applied transformations inside at all.
What is wrong? How I can setup same "publish to folder" as in Visual Studio but using VSTS?
Add below Target to your .csproj to enable transforming config files
<Target Name="TransformConfigFiles" AfterTargets="AfterBuild" Condition="'$(TransformConfigFiles)'=='true'">
<ItemGroup>
<DeleteAfterBuild Include="$(WebProjectOutputDir)\Web.*.config" />
</ItemGroup>
<TransformXml Source="Web.config" Transform="$(ProjectConfigTransformFileName)" Destination="$(WebProjectOutputDir)\Web.config" />
<Delete Files="#(DeleteAfterBuild)" /></Target>
In your build solution step add the following build arguments "/p:TransformConfigFiles=true" will make the config transformation using the above added target to .csproj
/p:TransformConfigFiles=true /p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:OutDir="$(build.stagingDirectory)"
Then you can use a publish step to publish your $(build.stagingDirectory) contents. You can use $(build.stagingDirectory)_PublishedWebsites as path to publish if you only need the website output.
This will allow you to get the ms deploy package as well as xcopy deploy published website files.
You can use copy files task before the publish task to copy any additional files if you have any to $(build.stagingDirectory) and get them published as build artifacts.
Use VSTS release management with deployment groups to deploy your application to target server. You can use IIS deploy task to deploy to IIS using ms deploy package. If you are using web deploy package you can use a parameters.xml in your web app to get the web config parameters assigned to .setparameters.xml so that you can change values in the deployment time using IIS deployment task.
You are publishing web application through File System method, it is based on the specified configuration (e.g. Debug, Release) to transform web.config. So you need to check which configuration you specified in build solution task (e.g. Visual Studio Build task)
Simple tasks:
NuGet Tool Installer task
NuGet restore task
Visual Studio Build task (MSBuild Arguments: /p:SkipInvalidConfigurations=true /p:DeployOnBuild=true /p:WebPublishMethod=FileSystem /p:publishUrl="$(build.artifactstagingdirectory)\\" /p:DeployDefaultTarget=WebPublish; Platform: $(BuildPlatform); Configuration: $(BuildConfiguration)) Note: BuildPlatform and BuildConfiguration are build variables. It will publish web app to artifacts directory ([agent working folder]/1/a)
Publish Build Artifacts (Path to publish: $(build.artifactstagingdirectory))

Unique Need - Perform Team Services releases using artifacts created in an external build

I have a unique need where I need to perform releases from Team Services using a Release Pipeline and artifacts that have been created in a previous external build. I have the artifacts that were created, dacpacs and websites ect.
I would like to deploy these items using the features in release Pipelines but artifact sources only come from a build or some other version control.
My approach (hack) was to use a build to copy the external files and publish them into the artifact container for the build. I could then use the release pipelines to do my releases. But .. Build copy tasks only seem to work with paths into a repo.
My fall back will be to use the release pipeline and powershell to do the releases with these externally created artifacts. I would sure like to avoid this since there is nice capability in the release pipeline tasks.
This is a compliance requirement my firm has which results in the rather crazy post.
Any help would really be appreciated.
You can use Copy Files task and Publish Build Artifacts task for your build definition.
Copy Files task
Source Folder: you can specify the folder which has your external build artifacts. Such as C:\project\a.
Contents: you can use wildcards to specify which files to copy. Such as **\*.dll, this will copy all *.dll files in C:\project\a and it’s subfilder.
Target Folder: where you want to copy these files. Usually it’s $(build.artifactstagingdirectory).
Publish Build Artifacts task
Path to Publish: set as the same with Target folder in Copy files task. Such as $(build.artifactstagingdirectory).
Note: Copy files task will find the source folder in the machine where the private agent is located.