Search code examples
node-modulesazure-deploymentazure-pipelines-release-pipeline

Include node modules in azure deployment via VSTS


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.


Solution

  • 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])