Include node modules in azure deployment via VSTS
I have an azure app that I need to deploy as part of a release definition in VSTS. To provide some context, this is an ASP.NET MVC application and uses Angular 2. It also has a package.json file. I have a VSTS build definition that includes an "npm install" task to install all dependencies from package.json so that I don't have to check all node modules. At the end of the build, the files are dropped to a share without a node_modules folder.
I have a corresponding release definition for web deployments that are azure builds. However, I'm not sure how to get the node_modules folder on the azure machine. Can anyone help or provide suggestions for this scenario? I was hoping that packages could be installed on a machine with npm in some path.
source to share
You can do this using the Kudu API with PowerShell. For example (package.json is located in wwwroot folder)
- Add Azure PowerShell step / task (Script Arguments: -resourceGroupName VS-starain2-Group -webAppName tempappstarain -dir "site \ wwwroot" -command "npm install")
PowerShell script:
param(
[string]$resourceGroupName,
[string]$webAppName,
[string]$slotName="",
[string]$dir,
[string]$command
)
function Get-AzureRmWebAppPublishingCredentials($resourceGroupName, $webAppName, $slotName = $null){
if ([string]::IsNullOrWhiteSpace($slotName)){
$resourceType = "Microsoft.Web/sites/config"
$resourceName = "$webAppName/publishingcredentials"
}
else{
$resourceType = "Microsoft.Web/sites/slots/config"
$resourceName = "$webAppName/$slotName/publishingcredentials"
}
$publishingCredentials = Invoke-AzureRmResourceAction -ResourceGroupName $resourceGroupName -ResourceType $resourceType -ResourceName $resourceName -Action list -ApiVersion 2015-08-01 -Force
Write-Host $publishingCredentials
return $publishingCredentials
}
function Get-KuduApiAuthorisationHeaderValue($resourceGroupName, $webAppName, $slotName = $null){
$publishingCredentials = Get-AzureRmWebAppPublishingCredentials $resourceGroupName $webAppName $slotName
Write-Host $publishingCredentials.Properties.PublishingUserName
Write-Host $publishingCredentials.Properties.PublishingPassword
return ("Basic {0}" -f [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $publishingCredentials.Properties.PublishingUserName, $publishingCredentials.Properties.PublishingPassword))))
}
function RunCommand($dir,$command,$resourceGroupName, $webAppName, $slotName = $null){
$kuduApiAuthorisationToken = Get-KuduApiAuthorisationHeaderValue $resourceGroupName $webAppName $slotName
$kuduApiUrl="https://$webAppName.scm.azurewebsites.net/api/command"
$Body =
@{
"command"=$command;
"dir"=$dir
}
$bodyContent=@($Body) | ConvertTo-Json
Write-Host $bodyContent
Invoke-RestMethod -Uri $kuduApiUrl `
-Headers @{"Authorization"=$kuduApiAuthorisationToken;"If-Match"="*"} `
-Method POST -ContentType "application/json" -Body $bodyContent
}
RunCommand $dir $command $resourceGroupName $webAppName
Further article: Interacting with Azure Web Apps Virtual File System using PowerShell and the Kudu API
You can also just deploy the node_module folder and files in an azure web application using Azure App Service Deploy (select 3. * for step / task, do not check publishing with Web Deploy option. Package or foder: [path of node_module folder])
source to share