How can I include additional files in an Azure cloud services package when using VSTS? - azure

I have a solution with a web project in it and a cloud project for deploying that web project as a cspkg file to a cloud service. This all works fine. However, I have a file that i don't want in the web project but I do want deployed with the cspkg file into the cloud service.
We use VSTS to build and deploy things, and I haven't figured out how to include extra files in the package within this system. I tried a Copy Files step but that doesn't get the file into the package, it does get it into the artifacts though. I tried other things I found online, like the PipelineCollectFilesPhaseDependsOn injection technique, but nothing seems to work.
Is this possible, and if so, how can it be done?

The Azure deployment package file (CSPKG) file is zipped and encrypted, there isn’t the better way to add additional files (More details: Include/Exclude files when creating Azure package). But you can include the files in BeforeBuild target. For this way, you don’t need to include files to the web project.
For example:
Edit web project file (e.g. WebRole1.csproj)
Add BeforeBuild target before tag.
Code:
<Target Name="BeforeBuild">
<ItemGroup>
<Content Include="Files\TT.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Target>

Related

ASP.NET Core, Could not find file. How Can I publish it?

I have found many questions like this, but I'm still having problems to get a "static" file.
The task should be simple. Just get a cshtml file content. A file that it's already in the project. But it's taking forever to figure it out.
This is the error that I'm getting (only when in production)
Could not find file 'D:\home\site\wwwroot\AppData\template.cshtml'.
Looking into the 'D:\home\site\wwwroot' folder, indeed the 'AppData' doesn't exist. But then how can I publish the AppData and its files?
This is how I'm getting the file
string contentRootPath = webHostEnvironment.ContentRootPath;
return $"{contentRootPath}/AppData/{filename}";
This is inside .csproj
<Folder Include="AppData\" />
To publish the project I'm using DevOps Azure. Is there any configuration that I'm missing?
I don't know if this is the right approach, but it's working (for now).
In DevOps/Pipelines/MyMypeline/Tasks, I add Copy files Task.
Source Folder: MyProject/AppData
Target Folder: $(Agent.TempDirectory)\WebAppContent\AppData
The next task (Archive files) it was already set. So in resume, the pipeline will get the files from AppData and add in the same place where the solution was build. And the other task will zip all. Than another task (Azure Web App) will sent the zip to my AppService.
I still have to test when do the swipe from dev to prod, but for now that's it.

How to add extra files to the package that is generated by msbuild called by an Azure Resource Group Project in Visual Studio

So I have a solution made up of a web project and an Azure Resource Group project much like what is described here: https://blogs.technet.microsoft.com/georgewallace/2015/05/10/deploying-a-website-with-content-through-visual-studio-with-resource-groups/
The issue I am having is that the web project needs to include some EXTRA files in its bin directory in order to function. Now through the normal package process on the web project itself I accomplish this by adding a custom target and hook into CopyAllFilesToSingleFolderforPackageDependsOn, like:
<PropertyGroup>
<CopyAllFilesToSingleFolderForPackageDependsOn>
CustomCollectFiles;
$(CopyAllFilesToSingleFolderForPackageDependsOn);
</CopyAllFilesToSingleFolderForPackageDependsOn>
</PropertyGroup>
<Target Name="CustomCollectFiles">
<ItemGroup>
<_CustomFiles Include="bin\SlimKIA.WebApi.xml" />
<FilesForPackagingFromProject Include="%(_CustomFiles.Identity)">
<DestinationRelativePath>bin\SlimKIA.WebApi.xml</DestinationRelativePath>
</FilesForPackagingFromProject>
</ItemGroup>
</Target>
And when I right-click on the web project and say "publish" to, say, a file system, my file gets included. All is well and good.
The problem is that this does not happen when I deploy from the deployment project. I have an MsDeploy resource that does its own thing. I need to somehow do my CustomCollectFiles from within this project.
Anyone have any ideas as to how to accomplish this? Without this I am unable to automate the deployment of my solution to Azure.
Thanks!
On a whim I ended up doing this:
<PropertyGroup>
<CopyAllFilesToSingleFolderForMsdeployDependsOn>
CustomCollectFiles;
$(CopyAllFilesToSingleFolderForMsdeployDependsOn);
</CopyAllFilesToSingleFolderForMsdeployDependsOn>
</PropertyGroup>
The key being the CopyAllFilesToSingleFolderForMsdeployDependsOn instead of CopyAllFilesToSingleFolderForPackageDependsOn. Just changing this did the trick, which I guess triggered msdeploy to include the custom target.

Azure publish package not including all files

I have couple of config, dll and an exe file located in the bin folder and included in project. The files are set as Content and Copy Always.
When I debug the project locally, all works fine, the problem is when I publish the cloud service, the files are missing. The files are being omitted by the Azure publish process and are missing from the deployment package file.
How do I force the files to be included in the package?
This might be a duplicate of Windows Azure not generating aspx files
Here is a quote from that post that may offer a solution:
I've had a similar issue with deploying Spark ViewEngine files in an MVC3 app. Solution for me was to make sure the files were part of the project (see smarx answer), and to explicitly set the Build Action to Content for each file. – kenxl Mar 3 '11 at 9:09

AjaxMin not working on Azure site

I am using Microsoft's AjaxMin to minify javascript on my website, which is hosted by Azure. I am using a BuildTask to automatically minify javascript at run time. This build task is specified in the .csproj file.
The process is working on my local environment, however, it does not work when I deploy to my Azure site. The azure site throws 404: file not found errors, when i try to reference the minified version of .js files.
Is it possible to use build tasks on an Azure site? Is there anything I am missing? I have made sure not to include the .min.js files in source control as this (http://www.asp.net/ajaxlibrary/AjaxMinQuickStart.ashx) tutorial suggests, but I am wondering if there is anything specific to Azure that I need to set up.
Thanks!
I've got this working properly in my projects. I'll tell you how I did it, though this may not be the simplest or most straightforward way.
Before we get started, it's helpful to be able to check if your minified files are included in the Azure deployment package without actually deploying. It's pretty easy to do. The .cspkg file is actually a zip-formatted file, so you can open it with any zip archiver. (I like to use 7Zip for this because the right-click -> Open Archive command doesn't require you to rename the file, but you could use Windows Explorer, WinRAR, etc.) Inside the .cspkg you'll see another large file with a .cssx extension. That's a zip file too. Inside of the .cssx you'll find a sitesroot folder with a subdirectory for each website you're deploying, which will contain all your actual website files. So you can poke around in there and see what files are being deployed to Azure.
First, try editing the project file for your web project (the one that contains all the Javascript/CSS files). You can use Notepad, or in Visual Studio right-click the project, select "Unload Project", then right-click again and select "Edit ". Inside the project file, insert a section like this:
<ItemGroup>
<!-- Copy over all the minified CSS & JS to the output directory-->
<Content Include="**\*.min.css" />
<Content Include="**\*.min.js" />
</ItemGroup>
Then reload the project, repackage it, and see if your files are included in the .cspkg file. If they are, then you're done.
If not, there are a couple other things to check. Your minification might not be running at the right build stage. My minification target looks like this:
<Target Name="PrepWebApp" Condition="$(Configuration)=='Release'" AfterTargets="AfterBuild">
If that's still not working and your Web Role has multiple Sites and/or Virtual Applications in it, it's possible that the packaging steps are not running for all of the sites. So when you go to package your project for deployment to Azure, it may still not be running the minification step (along with the web.config transformations, and some other things). If that's the case, see this blog post for a way to fix it.
Just in case that blog post goes away, I'll copy the most relevant bit here. You would put this in the .ccproj file for your web role (with appropriate bits changed to match your project structure):
<PropertyGroup>
<!-- Inject the publication of "secondary" sites into the Windows Azure build/project packaging process. -->
<CoreBuildDependsOn>
CleanSecondarySites;
PublishSecondarySites;
$(CoreBuildDependsOn)
</CoreBuildDependsOn>
<!-- This is the directory within the web application project directory to which the project will be "published" for later packaging by the Azure project. -->
<SecondarySitePublishDir>azure.publish\</SecondarySitePublishDir>
</PropertyGroup>
<!-- These SecondarySite items represent the collection of sites (other than the web application associated with the role) that need special packaging. -->
<ItemGroup>
<SecondarySite Include="..\WebApplication1\WebApplication1.csproj" />
<SecondarySite Include="..\WebApplication2\WebApplication2.csproj" />
</ItemGroup>
<Target Name="CleanSecondarySites">
<RemoveDir Directories="%(SecondarySite.RootDir)%(Directory)$(SecondarySitePublishDir)" />
</Target>
<Target Name="PublishSecondarySites" Condition="'$(PackageForComputeEmulator)' == 'true'
Or '$(IsExecutingPublishTarget)' == 'true' ">
<!--
Execute the Build (and more importantly the _WPPCopyWebApplication) target to "publish" each secondary web application project.
Note the setting of the WebProjectOutputDir property; this is where the project will be published to be later picked up by CSPack.
-->
<MSBuild Projects="%(SecondarySite.Identity)" Targets="Build;_WPPCopyWebApplication" Properties="Configuration=$(Configuration);Platform=$(Platform);WebProjectOutputDir=$(SecondarySitePublishDir)" />
The build task will run in Visual Studio when you build the project. You need to make sure that the minified files are also being deployed to Azure.
I'm guessing that perhaps because the item is being generated at build-time that it's not part of the project itself, and is this ignored by the deployment step.
Please check that the deployment system being used will include all the script files and not just the ones that are in the project itself.

configure common.logging in Azure

I'm developing an app. using Common.Logging (http://netcommon.sourceforge.net/index.html).
A simplified logging configuration (which is done in web.config) is as follows:
<configuration>
...
<arg key="configType" value="FILE" />
<arg key="configFile" value="NLog.config" />
...
</configuration>
As you can see here, the configuration points another configuration file (here, NLog.config) of a backend logging framework.
My question is: when deploying in Azure, what path should I specify here (on dev, NLog.config is copied when building the solution, and placed in the bin directory). In other words, what would be the SAFEST way to get the physical place where the app is deployed in Azure?
Thanks in advance!
In code you can find the current path to the application using Server.MapPath("/"). Now, you can simply make sure that the NLog.config file gets deployed to the application folder:
Add NLog.config to your project (in the root of your web application)
Change the Build Action to Content
In order to test this you can right click on your Azure project an choose Package. In the bin\Release|Debug\app.publish folder of your Azure project you'll find a *.cspkg file. Add a .zip extension to this file and open the file with WinRAR/ZIP/7zip/... Then you'll see a file like this one: SomeWebRole_1a91f39a-49b7-4ece-873f-862172a2fa06.cssx. Here again, add the .zip extension to this file and open it.
If you navigate to the sitesroot\0 folder you'll see the files of the web application in IIS, including the NLog.config file. This way, you' can simply reference the NLog.config file in the Common.Logging settings:

Resources