简体   繁体   中英

Upload/Publish WebApp Files to Azure via PowerShell

Hi I am currently having a problem updating an old PowerShell script used for Azure. It was originally written to deploy a website after asking a few questions of the user. This was simple enough as you could create a new website via New-AzureWebsite and then Publish-AzureWebsite to upload the files.

I am now using New-AzureRmWebApp but can not figure out how to upload the files, there is no Publish command for this and the Set-AzureRmWebApp command does not have a parameter to cover this.

Does anybody know if this can still be done?

Thanks,

It doesn't seem as straightforward with Resource Manager, but you can use the Publish-AzureWebsite.ps1 generated by VS. See details here: https://azure.microsoft.com/en-us/documentation/articles/vs-azure-tools-publishing-using-powershell-scripts/

More in-depth article on automating provisioning and deployment: http://www.asp.net/aspnet/overview/developing-apps-with-windows-azure/building-real-world-cloud-apps-with-windows-azure/automate-everything#resources

I know this is an old question but wanted to share my experience with this.

It definitely isn't as straightforward in Resource Manager - the normal approaches, like uploading a zip file, have been usurped by automation and integration with other services such as GitHub and even synchronisation with Dropbox - you can also integrate with a local version of Git where you would technically do a git push from your computer to the "local" (that is, local to the site/app service) Git repo. That Git repo then deploys the site from your push.

You can also use FTP solutions such as curl and wget if you want simplicity.

Other solutions like Web Deploy (msdeploy.exe) require software, specifically Visual Studio, to be installed.

Information on all of the above methods of deploying are listed here: https://docs.microsoft.com/en-us/azure/app-service-web/web-sites-deploy

The solution I ended up using was a halfway house - the deployment engine for Azure App Service is something called Kudu. It is this service that implements the local Git repo push in order to deploy your site from your Git push but it can do more.

Kudu has an API and I happened upon a PowerShell script that allows uploading of a directory, directly from your computer for deployment to the site using Kudu. The script was originally written for use with ASM so I made some minor changes to allow it to integrate with Resource Manager Web apps.

To ensure the original author is credited, the gist is here: https://gist.github.com/lwsrbrts/a2c9bfe1949ea0ebe34b6c6d5c0b11b6

The code for those that hate people posting links:

Param(
[Parameter(Mandatory = $true)]
[string]$websiteName,
[Parameter(Mandatory = $true)]
[string]$resourceGroupName,
[Parameter(Mandatory = $true)]
[string]$sourceDir,
[string]$destinationPath = "/site/wwwroot"
)

# Usage: .\kuduSiteUpload.ps1 -websiteName mySite -sourceDir C:\Temp\mydir -resourceGroupName myResourceGroup

Function d3-KuduUploadDirectory
{
param( 
    [string]$siteName = $( throw "Missing required parameter siteName"),
    [string]$sourcePath = $( throw "Missing required parameter sourcePath"),
    [string]$destinationPath = $( throw "Missing required parameter destinationPath"),
    [string]$resourceGroupName = $( throw "Missing required parameter resourceGroupName")
)

$zipFile = [System.IO.Path]::GetTempFileName() + ".zip"

d3-ZipFiles -zipfilename $zipFile -sourcedir $sourcePath

d3-KuduUploadZip -siteName $siteName -sourceZipFile $zipFile -destinationPath $destinationPath -resourceGroupName $resourceGroupName
}

Function d3-KuduUploadZip
{
param( 
    [string]$siteName = $( throw "Missing required parameter siteName"),
    [string]$sourceZipFile = $( throw "Missing required parameter sourceZipFile"),
    [string]$destinationPath = $( throw "Missing required parameter destinationPath"),
    [string]$resourceGroupName = $( throw "Missing required parameter resourceGroupName")

)

[xml]$publishSettings = Get-AzureRmWebAppPublishingProfile -Format WebDeploy -OutputFile .\Temp.publishsettings -ResourceGroupName $resourceGroupName -Name $siteName
$website = $publishSettings.SelectSingleNode("//publishData/publishProfile[@publishMethod='MSDeploy']")

$timeOutSec = 900

$username = $webSite.userName
$password = $webSite.userPWD
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))

$baseUrl = "https://" + $siteName + ".scm.azurewebsites.net"
$apiUrl = d3-JoinParts ($baseUrl, "api/zip", $destinationPath) '/'

Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method PUT -InFile $sourceZipFile -ContentType "multipart/form-data" -TimeoutSec $timeOutSec
}

Function d3-JoinParts {
param ([string[]] $Parts, [string] $Separator = '/')

# example:
#  d3-JoinParts ('http://mysite','sub/subsub','/one/two/three') '/'

$search = '(?<!:)' + [regex]::Escape($Separator) + '+'  #Replace multiples except in front of a colon for URLs.
$replace = $Separator
($Parts | ? {$_ -and $_.Trim().Length}) -join $Separator -replace $search, $replace
}

Function d3-ZipFiles
{
Param(
    [Parameter(Mandatory = $true)]
    [String]$zipfilename,
    [Parameter(Mandatory = $true)]
    [String]$sourcedir
)

Add-Type -Assembly System.IO.Compression.FileSystem
$compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
[System.IO.Compression.ZipFile]::CreateFromDirectory($sourcedir, $zipfilename, $compressionLevel, $false)
}

$startTime = Get-Date
d3-KuduUploadDirectory -siteName $websiteName -sourcePath $sourceDir -destinationPath $destinationPath -resourceGroupName $resourceGroupName
$finishTime = Get-Date
Write-Host (" Total time used (minutes): {0}" -f ($finishTime -$startTime).TotalMinutes)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM