4

I have an azure app service which I need to deploy through as part of a release definition in VSTS. To provide some context, it is a ASP.NET MVC app and uses Angular 2. It also has a package.json file. I have a VSTS build definition which includes a 'npm install' task to install all dependencies from package.json, so that I don't need to check in all the node modules. At the end of the build the files are dropped to a share without the node_modules folder.

I have a corresponding release definition to web deploy that build to azure. However, I am not sure how to get the node_modules folder on the azure machine. Can someone help or provide suggestions for this scenario? I was hoping the packages can be npm installed on the prod machine in some way.

AgentHunt
  • 669
  • 3
  • 11
  • 28
  • An entire build package should include all the dependency, why do you publish the build package to share without the node_modules folder at the end of the build? – Eddie Chen - MSFT May 15 '17 at 01:53

1 Answers1

2

You can do it by using Kudu API with PowerShell. For example (package.json is in wwwroot folder)

  1. 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

Related article: Interacting with Azure Web Apps Virtual File System using PowerShell and the Kudu API

You also can just deploy node_module folder and files to azure web app by using Azure App Service Deploy (Select 3.* version of step/task, do not check Publish using Web Deploy option. Package or foder: [node_module folder path])

starian chen-MSFT
  • 33,174
  • 2
  • 29
  • 53
  • In the example you indicated directory as "site\wwwroot". How do I know to which directory are the files deployed to as I don't have access to the azure machine? Is there some environment variable for that? – AgentHunt May 11 '17 at 05:46
  • 1
    I am also thinking, if all I want to do is npm install, I should be able to run a powershell script with 'npm install'. Then why would I need to use Kudu api? – AgentHunt May 11 '17 at 06:40
  • @AgentHunt You can try to access https://[webapp].scm.azurewebsites.net/ >Debug Console>CMD, then check files. By default, all files are deployed to wwwroot folder (site\wwwroot), so if package.json is in the root folder, you can use site\wwwroot. – starian chen-MSFT May 11 '17 at 07:14
  • @AgentHunt I assume run powershell script with npm install means run script file during the build/release, then it just install the packages on build agent instead of azure app service. This Kudu api is used to azure app service. Anyway, you can deploy the node_module folder with files to azure app service directly by using Azure App Service Deploy step/task or include the node_module folder to project or package file. For this way, you don't need to use Kudu API to run command. – starian chen-MSFT May 11 '17 at 07:18
  • I ended up adding a npm install followed by a 'Copy Files' task for node_modules to copy the files directly to a drop share along with other files. Then I included all the files in the Azure deployment. – AgentHunt May 15 '17 at 18:23