how to configure webjob to access the web app's reference project files in azure - azure

I have a .net core web app that I deploy to azure using Azure Devops build/release pipelines. This project references Business project and a Models project that are part of the three-tier solution. The Models project consists of Entity Framework 6 code first models (including migrations).
Recently I have had to deploy a triggered webjob in order to accomplish a long running task. This was just created as a normal .Net console app and then published from within Visual Studio 2017 by selecting "Publish as Azure Web Job". This webjob is published to and runs under the .net core web app service mentioned above. It references the same Models and Business project that the .net core web app references.
My issue is that whenever the model is changed by introducing db migrations, the web job also must be updated since the models.dll that is published as part of the webjob project resides separately in a directory app_data\jobs\triggered\webjob under the main web app.
Is there any way to configure my webjob so that the models.dll and business.dll are directly referenced from that of the main web app? Failing that, how can I modify the Azure devops process to copy these files to the directory of the webjob upon successful deploy? Is there a guide for this?

how can I modify the Azure devops process to copy these files to the directory of the webjob upon successful deploy? Is there a guide for this?
AFAIK, we could use powershell scripts and git command to filter whether the file modification comes from models.dll and business.dll files, like:
$editedFiles = git diff HEAD HEAD~ --name-only
$editedFiles | ForEach-Object {
Switch -Wildcard ($_ ) {
'SubFolderA/models.dll*' { Write-Output "##vso[task.setvariable variable=UpdateFile]True" }
# The rest of your path filters
}
}
Code comes from here.
Then add custom conditions in the next task in the build pipeline:
and(succeeded(), eq(variables['UpdateFile'], 'True'))
In the next task, we could use the kudu API to update these files to the directory of the webjob upon successful deploy:
GET /api/zip/{path}/
Zip up and download the specified folder. The zip doesn't include the top folder itself. Make sure you include
the trailing slash!
PUT /api/zip/{path}/
Upload a zip file which gets expanded into the specified folder. Existing files are not deleted
unless they need to be overwritten by files in the zip. The path can be nested (e.g. `folder1/folder2`), and needs to exist.
You could check the similar thread and the document for some details.
Hope this helps.

Related

.net core azure pipeline deployment with editable folders

I have a .net core web app (NopCommerce 4.1) that I am attempting to setup a build and release pipeline for.
However, when I setup the pipeline my deployment is failing because it attempts to create a folder, but write rights do not exist. I have confirmed this with Kudu where I get an error message (409) when attempting to create a folder via the cmd shell.
NopCommerce requires a couple of folders to be editable, but azure pipelines deploys a zip folder and creates a folder structure that is read only.
I want to deploy to a dev, test, prod environment with a folder structure that is editable (as nopcommerce creates folders and writes files to them dynamically).
I followed the following YAML structure:
https://damianbrady.com.au/2018/10/11/what-yaml-do-i-need-for-azure-pipelines/
Is there a way to create a build / deployment that will deploy either:
1. The files without zipping
2. Transfer a zip, unpack into a folder structure with execute/modify/create permissions
1.The files without zipping 2.Transfer a zip, unpack into a folder structure with execute/modify/create permissions
We could use the Azure kudu Zip API to do that.
Note:It is not recommended for deployments Kudu's zip API.
Kudu Rest API is an effective way to move multiple files to your site, but zipdeploy is preferable for deployment For more information please refer to this document.
In your case, you could use a Powershell task with Powershell script to invoke Kudu Zip API to do that. For information about how invoke REST API with Powershell, please refer to this SO thread.

How to deploy Azure WebJob as part of automatic VSTS deployment

Is there a way of having my Azure WebJob automatically deploy without needing to right click and select "Publish as Azure WebJob" every time? i.e. when I check-in my solution it is automatically deploy to the Azure Portal webjob section
While I tried to accomplish this, I found out that there is no tooling support for dotnet core projects as of now. The proposed webjobs.props/ msbuild solutions are all dotnet framework specific.
However I also found out that a webjob can be anything that's executable on the local machine (could be node.js or just a batch command).
The key is to understand how WebJobs are recognized by the host:
A WebJob on a Windows host is (by what I gathered from experimenting with it) just a run.cmd file that contains instructions on how to start the webJob. For dotnet core that would be dotnet MyDll.dll %* (%* to pass arguments for output redirection etc. down from the host).
Now depending on wether the job is continuous or triggered the run.cmd file needs to be located either at app_data/jobs/continuous/[NameOfJob] or app_data/jobs/triggered/[NameOfJob]. For the triggered job you could also add a schedule using a settings.job file like described here.
If there is a run.cmd at the proper location it will be recognized as a WebJob
Now to deploy a webjob using VSTS regardless of the runtime framework follow these steps:
Build/Publish your WebJob if necessary to get the executables
Add a run.cmd file next to your webjob executables that contains the proper startup instructions. You could also add settings.job here if needed.
Create the folder hierarchy app_data/jobs/[triggered/continuous]/[nameOfJob] and copy your executables into the lowest folder. Make sure run.cmd is directly under the [nameOfJob]/ directory
Zip the app_data folder so that the zip-package contains the entire hierarchy
Publish your zip file using the normal Azure App Service Deployment task (just like deploying the web app)
And that's it.
Yes you can.
Brady Gaster has written about this on a blog post (haven't tried it myself).
From what I gather, the TL;DR; summary is the following:
add a file named webjobs.props to the properties folder of the Web Application Project or Console Application you’re publishing
After that, edit the file so the ManagementCertificate, SubscriptionId and PublishSettingsPath are filled with correct data.
Now you should be able to publish the files using the command
msbuild WebJobDemo.Web.csproj /p:DeployOnBuild=true /p:PublishProfile=WebJobDemo /p:VisualStudioVersion=12.0 /p:Password=asdfasdf
(Note, post is written for VS2013)
Afterwards you should see something like this.
You could of course automate this in VSTS (or any other build/deployment tool for that matter) whenever something is checked in to your repository.
A (rather complete) answer on how to do this in VSTS via the command-line can be found in this answer: https://stackoverflow.com/a/45703975/352640
You can do it through Continuous Integration Build (trigger the build once check in).
Regarding deploy WebJob, you can generate a package through Visual Studio Build task with /p:DeployOnBuild=true /p:WebPublishMethod=Package /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.stagingDirectory)" argument.
Then deploy it through Azure App Deployment task.
More information: Deploying and Schedule Azure WebJobs from VSTS to Azure Web App
In Visual Studio in order to enable automatic WebJobs deployment together with a Web project right-click the Web project in Solution Explorer, and then click: Add > Existing Project as Azure WebJob and create your WebJob.
More details can be found in an article by MS - webjobs-dotnet-deploy-vs

How to publish and deploy a .Net Console application in similar way as Web application?

We have several Build definitions setup at TFS online (visualstudio.com) which delivers our Web application to several Azure machines (DEV, TEST, AccTest etc..).
It works and they trigger on build events.
BUT: Now we have added a Console Application (Batch exe file) that don't have a publishingProfile. The Build definition builds entire solution (console project is a part of that), but when it comes to Deployment only Web project is deployed in another folder and zipped.
I have read a lot on Stackoverflow and MSDN sites. But could not really find any spot on answers. This must be a common scenario that you have non-web projects in a solution that also needs to be deployed on same server through VSTS.
You can archive the files you want to deploy as a zip file, and specify the zip file as Package or folder option in Azure App Service Deploy task.
Detail tasks and settings to deploy the .net console app to azure as below:
1. Copy Files task
Source Folder: $(System.DefaultWorkingDirectory)
Contents: ProjectName\** or you can specify the certain file (such as *.exe) to copy
Target Folder: $(Build.ArtifactStagingDirectory)
2. Archive Files
Root folder (or file) to archive: $(Build.ArtifactStagingDirectory)
Archive type: zip
Archive file to create: $(Build.ArtifactStagingDirectory)/deploy.zip
3. Azure App Service Deploy
Specify Azure subscription and App Service name as you set before.
Package or folder: $(Build.ArtifactStagingDirectory)\deploy.zip
Now files you copied from .net console project are deployed to your Azure App service.

How to deploy a static website to Azure from Visual Studio Team Services

I have an existing website that I would like to deploy on Azure, using Visual Studio Team Services. The website is made up of static files, there's no ASP.NET or anything else involved.
Within Visual Studio Team Services, I created a build which executes npm install and a gulp build. This results in a dist folder containing all the files for the website. In Azure, everything is set up correctly (subscription, web app,...).
However, I'm unsure on how to push my code to Azure. Exploring the options in the Release tab in VSTS, an 'artifact' always seems to be required, but I just have a bunch of files. I need to publish the files in the dist folder and make sure index.html is served.
How can I do that?
This question is related to this one, however, the answers all state to start from Azure, and do not mention how to deploy existing code using Visual Studio Team Services.
The trick is to create the artifact yourself, which can be as simple as a zip file containing the static website files. The zip file should be copied as an artifact to the $(build.artifactstagingdirectory) target directory. Next, you can use a simple Web App deployment task to publish the zip file to Azure. If index.html is in the root directory, Azure is smart enough to serve it.
Below is a working build and deploy flow. It assumes gulp is used to build the website and write the build output (i.e. the static files) to a dist folder.
The easiest way is to deploy from a source control, if you take a look under "Settings" for your Website in the Azure portal you will probably see "Continuous deployment".
From there you can deploy from Visual Studio Team Services, Github, etc.
Every check-in will be deployed, also wrong ones, so you may want a introduce a staging environment as a deployment slot as well, where you can swap staging with production whenever you feel your site is ready for production.
Without the need to create an artifact, another solution could be FTP deployment after creating an Service Endpoint in VSTS

How can I use the TFS Online <--> Azure Website integration when there are two web apps in the solution

I am trying to get continuous integration configured using Azure. The process I have followed is exactly as detailed by Continuous delivery to Windows Azure using Visual Studio Online
This process works perfectly! I check-in, it builds, and deploys to the Azure website. However, as soon as I add a second web application to the solution, after the CI build kicks off and then completes, the only thing that gets published to the website is the bin directory of the second web application! (Updates to the first project are successful, though)
Given the scenario, I don't understand why the dll's of the second application are being published to the bin directory when the rest of the application(i.e. content files) are not. There is no reference from app1 to app2 so the dll's shouldn't be brought in by reference.
How can I configure this so that it will also publish the second web application?
You can tell Windows Azure Web Sites which project within a repository to deploy using a deployment file.
Add a file called .deployment to the root directory of your repository, with this content:
[config]
project = src/MyApp.Web
You can specify the folder containing the web application, or the actual .csproj or .vbproj file for the application.
If you have two sites in a single solution/repository and you want to publish them both, you will need to use two separate Web Sites, and use App Settings in the portal to specify the project instead. Create the sites, and for each, add a setting called Project in the Configure page, with the path to the directory or project as before.
More info: https://github.com/projectkudu/kudu/wiki/Customizing-deployments (Kudu is the system that actually handles deployments on Azure Web Sites)

Resources