2017-05-10 71 views
3

我有一個Azure應用服務,我需要在VSTS中作爲發佈定義的一部分進行部署。 爲了提供一些上下文,它是一個ASP.NET MVC應用程序並使用Angular 2.它還有一個package.json文件。我有一個VSTS構建定義,它包含一個'npm install'任務來安裝package.json中的所有依賴項,這樣我就不需要檢查所有的節點模塊。在構建結束時,文件將被放到沒有node_modules文件夾的共享中。通過VSTS在Azure部署中包含節點模塊

我有一個相應的發佈定義,以網絡部署構建到天藍。但是,我不確定如何獲取azure計算機上的node_modules文件夾。 有人可以幫助或爲這種情況提供建議嗎?我希望軟件包可以以某種方式安裝在prod機器上。

+0

整個構建包應包含所有依賴項,爲什麼要在構建結束時發佈不包含node_modules文件夾的構建包以便共享? –

回答

2

您可以通過使用Kudu API和PowerShell來實現。例如(的package.json是在wwwroot文件夾)

  1. 添加Azure中的PowerShell步/任務(腳本參數:-resourceGroupName VS-starain2-集團-webAppName tempappstarain -dir 「網站\ wwwroot的」 指令 「故宮安裝」 )

PowerShell腳本:

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 
     } 
    [email protected]($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 

相關文章:Interacting with Azure Web Apps Virtual File System using PowerShell and the Kudu API

你也可以只部署node_modul通過使用Azure App Service Deploy將e文件夾和文件複製到Azure Web應用程序(選擇步驟/任務的3. *版本,請勿使用Web Deploy選項選中「發佈」選項。打包或foder:[node_module文件夾路徑])

+0

在您指定的目錄爲「site \ wwwroot」的示例中。我如何知道部署到哪個目錄的文件,因爲我無法訪問Azure計算機?有沒有一些環境變量? – AgentHunt

+1

我也在想,如果我想要做的是npm install,我應該可以用'npm install'運行powershell腳本。那麼爲什麼我需要使用Kudu API? – AgentHunt

+0

@AgentHunt您可以嘗試訪問https:// [webapp] .scm.azurewebsites.net />調試控制檯> CMD,然後檢查文件。默認情況下,所有文件都部署到wwwroot文件夾(site \ wwwroot),所以如果package.json位於根文件夾中,則可以使用site \ wwwroot。 –