Azure app service deployment with Powershell (No FTP) - azure

I have an ASP.NET app service running in Azure as Paas,
what i wanted is simple powershell script that just deploys my ASP.NET build produced by jenkins, so just xcopy may work (No FTP).
Too many resources and options around the internet, specific input would be appreciated.

I'd look into msdeploy. It's a pretty powerful way to do deploys to Azure App Services.

Thanks for the input Mike, your input ended up looking something like this with powershell script.
#param([string]$packageFolderPath,[string]$publishProfilePath)
[string]$packageFolderPath = "C:\Site"
[string]$publishProfilePath = "C:\Publish\app-local1.PublishSettings"
#Get publish-settings from file
[xml]$xml=Get-Content($publishProfilePath)
[string]$azureSite=$xml.publishData.publishProfile.msDeploySite.get(0)
[string]$azureUrl=$xml.publishData.publishProfile.publishUrl.get(0)
[string]$azureUsername=$xml.publishData.publishProfile.userName.get(0)
[string]$azurePassword=$xml.publishData.publishProfile.userPWD.get(0)
[string]$computerName ="`"https://$azureUrl/msdeploy.axd?site=$azureSite`""
msdeploy.exe -verb:sync -source:contentPath=$packageFolderPath -dest:contentPath=$azureSite,ComputerName=$computerName,UserName=$azureUsername,Password=$azurePassword,AuthType='Basic'
Write-Output "Done !"

Related

How to automatically deploy a web job to Azure portal using Power shell

I have been checking for ways to deploy a web job to azure automatically using PowerShell. I saw some blogs that depict the steps and the following summarizes what I have tried
I build my application (ASP.NET Console Application) in release mode and Zipped the contents of bin/Release to a folder.
In PowerShell, I logged in with az login
Then I tried the following commmand
Invoke-WebRequest -Uri https://$applicationName.scm.azurewebsites.net/site/wwwroot/app_data/jobs/triggered/$webJobName ` -InFile $ZipFile -ContentType "application/zip" -Method Put
$ZipFile has the path to the folder I created on step 1.
The output I get is the following
Invoke-WebRequest : The page was not displayed because the request entity is too large
Please let me know if you know what the issue is or If you have any reference that would help.
Thanks in advance!
Thanks for pitching in everyone! Your input was helpful, however I would like to update the answer with the solution I found that was so easy and saved me so much time. I will like to update you on how I could successfully deploy the app service and web job in a single go. Its very easy and since it deploys web app and corresponding web jobs in a single go, this was the perfect solution for my scenario. Thanks to my colleague who helped me with this solution.
The following depicts the steps I had to go through.
Lets suppose that my app service in Azure is "appService1" and I want to create a triggered web job under appService1 that goes by the name "webJob1".
I followed zip-deployment with azure cli.
Publish your web application (For the app service) solution in release mode to get the files you will have to deploy. Let this folder be WebAppBuild.
Build your application (a console application in my case) that would serve as the web job for the app service in release mode.
Inside the published folder for the web application (for app service ie WebAppBuild in our example), add a folder with the following path
app_data\jobs\triggered\webJob1
(If you need more than one web jobs deployed, you can create more than one folders like webJob2, webJob3 etc)
Add the files you have in step 2 to this folder. This is basically the files needed for your web job
Zip the contents in a single folder that acts as your deployment folder for web app and web job
Go to powershell and run az login (works if you have installed azure cli, otherwise you will have to install it as well)
Log into your respective account with the prompt window
Run the following commands that sets run from package property to true for your web app and the second command is the actual deployment command
az webapp config appsettings set --resource-group <<resourceGroupName>> --name <<appServiceName>> --settings WEBSITE_RUN_FROM_PACKAGE="1" ;
az webapp deployment source config-zip --resource-group <<resourceGroupName>> --name <<appServiceName>> --src <<zipFilePath>>
Now login to your azure portal and navigate to your web app. Check under web jobs option and you will see that the web job has been created with the files you deployed.
For more help on starting, stopping, deleting the web job with azure cli, go through the following document.
Check here

Start an Auto Swap in azure using TFS

I'm using VSO to continuously deploy to azure.
I have three slots :
Staging ( for automated tests )
AutoSwap ( if the version passes the automated tests in staging, it's deployed to AutoSwap )
Production ( When AutoSwap is deployed, it will auto swaps with Production ).
The problem is that my deployments are done using FTP ( I can't do it otherwise because it's an Asp.net Core 1.0 app ), so when I deploy to AutoSwap, it's not detected as an actual deployment so no auto swap is done with production.
My question : Is there any powershell command that I can call from the TFS task to start that auto swapping ? ( A command to tell for example that a deployment has been done which I can call when the FTP uploading ends ) ?
EDIT
I have found and tried this but it does simply nothing ( it doesn't fail ) :
Switch-AzureWebsiteSlot -Name "MyApp" -Slot1 "production" -Slot2 "AutoSwap" -Force
Try to use the Move-AzureDeployment, which swaps the deployments in production and staging.
Parameter Set: Default
Move-AzureDeployment [-ServiceName] <String> [ <CommonParameters>]
More details you can refer the link from MSDN: Move-AzureDeployment
Note: This applies only to cloud services, not web apps. The difference between Web APP and cloud service: Web App vs Cloud Service
Update
This may caused by the azure powershell script loads from VSO online does not support swap-slot. Try to remove the Azure powershell module, and import a different one.
See the answer from Ryan P in this MSDN link: https://social.msdn.microsoft.com/Forums/windows/en-US/0f30b76b-7954-4558-a10d-6a2b6635765a/switchazurewebsiteslot-does-not-work-in-vso-online
The problem was that I'm using this utility to upload the files via FTP :
https://marketplace.visualstudio.com/items?itemName=januskamphansen.ftpupload-task
In its code there is this line which should be commented to not to block azure commands in the tasks following it :
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$CurrentSession.ignoreCert}
Now everything works great, It took me one week to find it, I hope that this answer will save time to someone.

Copy and install exe on azure vm via powershell

I'm trying to create an Azure VM and then copy an install file to the VM and then silently installing it. I have created a basic Azure Resource Group project, and can create and deploy the VM, but I can't figure out how to do everything from the powershell script.
It sounds like you could use a custom script extension to do what you want. In your ARM template, you can specify the url for a file and the command to run; Azure will handle getting the file onto your VM and running it based on your command. Here is an example from the Azure Quickstart Templates: https://github.com/Azure/azure-quickstart-templates/tree/master/windows-vm-custom-script
Hope this helps! :)

Azure staging web deploy fails with ERROR_USER_NOT_AUTHORIZED_FOR_CREATEAPP but not for production

I'm trying automated web site deployment in Azure and I'm at the phase were I run web deploy commands. The command for production works fine and updates the content but the one for staging is failing with the error code in the title.
I'm no sure were the problem is and if it is an authorization one I'm not sure where to set more permissions for the staging account.
This is the production command (which works):
"C:\Program Files\IIS\Microsoft Web Deploy V3\msdeploy.exe" -source:package="MyTestingSite.zip" -dest:auto,ComputerName="https://mytestingsite.scm.azurewebsites.net:443/msdeploy.axd?site=MyTestingSite",UserName="$mytestingsite",Password="fromProductionPublishProfile",authtype="Basic" -verb:sync
And this is the one for staging (which fails with ERROR_USER_NOT_AUTHORIZED_FOR_CREATEAPP)
"C:\Program Files\IIS\Microsoft Web Deploy V3\msdeploy.exe" -source:package="MyTestingSite.zip" -dest:auto,ComputerName="https://mytestingsite-staging.scm.azurewebsites.net:443/msdeploy.axd?site=MyTestingSite__staging",UserName="$MyTestingSite__staging",Password="fromStagingPublishProfile",authtype="Basic" -verb:sync -verbose
Help is greatly appreciated!
Thank you!
Azure expecting that site name will be presented twice - in scm.azurewebsites.net:443/msdeploy.axd?site=%SiteNameHere%" and the same value as a parameter, by default value from file SetParameters.xml is used for second.
You may modify this file or pass parameter to msdeploy directly:
-setParam:name="IIS Web Application Name",value="%SiteNameHere%"
I managed to find where the problem was ...
In a settings file created by msdeploy.exe I was using the wrong site name.
Sadly the documentation was popping up an error that had nothing to do with the wrong site name but only saying that it can't create that site because of the rights. Which somehow, in a twisted Microsoft like way, makes sense.

Install service in windows azure

I would like to know that is there any way to install the windows service on Azure environment? Or is there any alternate way of doing the same?
If I understand the question correctly what you need in Azure is a Worker Role, similar question on Stackoverflow: What's the concept of *worker role* in Windows Azure cloud?
Check out the this blog post 'Migrating Windows Service to Azure Worker Role'
http://blogs.msdn.com/b/joseph_fultz/archive/2010/04/02/migrating-windows-service-to-azure-worker-role-image-conversion-example-using-storage.aspx
Also look at the Windows Azure Training Kit for code & usage sample for Worker Roles in Azure
http://www.microsoft.com/download/en/details.aspx?id=8396
HTH
Another potential option is to use StartUp tasks to install a windows service in either a Web or a Worker Role.
Here is an MSDN article - although it seems a little short on the details.
How to Define Startup Tasks for a Role
Basically you will need to be sure that the installer is copied during the deployment and create a cmd file to execute the installer. Then add a Startup task element to the service definition XML file.
Couple of things to add along with the other answers:
You can install Services with Web or Worker Role through the Startup Task
Alternatively you can also run a process with elevated privilege through the Task Scheduler
Here are the steps to install a windows service on VM running Windows Server 2012 R2:
start your VM in Windows Azure Console and connect to it with Remote Desktop Connector
map your storage as a new drive in your VM:
net use z: \mystorage.file.core.windows.net\endoint /u:myusername verylongkeythatendswith==
Storage key can be found in your Azure Management Console -> Storages -> Manage Access Keys
copy all the necessary installation files to the mapped storage (copy&paste)
copy nssm to a local drive (not z: as it uses MAFS file system and that cannot be accessed with low-level windows API commands)
Create a .bat file with the following entries
set username=xxx
set password=yyy
call d:\nssm install "My service"
"%programfiles%\PathToService\myservice.exe" "-p 8677"
d:\nssm set "My service" ObjectName "%username%" "%password%"
sc failure "My service" actions= restart/60000/restart/60000/restart/60000 reset= 240
d:\nssm start "My service"
Username and password should be the ones you used to create the VM.
run the script. The service should be visible in your services list.
Enjoy!
PS : I used NSSM to simplify the service deployment.

Resources