【问题标题】:Rename or deploy additional files to Azure Web App将其他文件重命名或部署到 Azure Web App
【发布时间】:2019-04-08 10:21:11
【问题描述】:

作为我在 Azure DevOps 中发布管道的一部分,我希望在我的“部署 Azure 应用服务”任务完成后将一些 .config 文件重命名为 .config.disabled。

我尝试添加一个额外的“部署 Azure 应用服务”任务,但这似乎覆盖了之前的任务,只在 wwwroot 中留下了 .config.disabled 文件。

是否有任何其他方式(FTP 上传任务除外)可用于在 Azure Web 应用程序中重命名/部署文件子集?

【问题讨论】:

  • 以前可以修改,为什么要修改后?
  • 我需要运行一个要求站点在线的脚本,并且当脚本运行后,我需要禁用该脚本再次运行的机会。该脚本执行一些数据库同步并避免脚本意外运行,我想在运行后立即禁用它(通过禁用 .config 文件)

标签: azure-devops azure-pipelines azure-web-app-service azure-pipelines-release-pipeline


【解决方案1】:

如果您不想使用 FTP,尝试使用支持 Azure Web 应用程序的 Kudu service 可能会取得一些成功。

要查看 Kudu 服务,请导航到 https://your-web-app-name>。scm.azurewebsites.net

通过身份验证后,您会发现有一种方法可以浏览文件、查看正在运行的进程和一个交互式控制台,允许您对文件系统运行基本的 DOS 或 PowerShell 命令。

交互式控制台很棒,但要使其自动化,您可以使用 REST API 对文件系统发出命令。

issuing commands to the server 有几个例子。在 PowerShell 中:

# authenticate with Azure
Login-AzureRmAccount
$resoureGroupName = "your-web-app-name"
$websiteName = "your-resource-group-name"

$env = @{
     command= 'Set COMPUTERNAME'
     dir= 'site'
}
$json = $env | ConvertTo-Json

$env2 = @{
     command= 'copy filename.config file.config.notused'
     dir= 'site\wwwroot'
}
$json2 = $env2 | ConvertTo-Json

$env3 = @{
     command= 'delete filename.config'
     dir= 'site\wwwroot'
}
$json3 = $env3 | ConvertTo-Json

# setup auth header
$website = Get-AzureWebsite -Name $websiteName
$username = $website.PublishingUsername
$password = $website.PublishingPassword
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))

$apiBaseUrl = "https://$($website.Name).scm.azurewebsites.net/api"
[System.Uri]$Uri = $apiBaseUrl

# get all the vms in the web-app
$instances = Get-AzureRmResource -ResourceGroupName $resoureGroupName `
                                -ResourceType Microsoft.Web/sites/instances `
                                -ResourceName $websiteName `
                                -ApiVersion 2018-02-01

#loop through instances
foreach($instance in $instances)
{
    $instanceName = $instance.Name
    Write-Host "`tVM Instance ID `t`t: " $instanceName

    #Now execute 'SET COMPUTER' cmd
    $cookie= New-Object System.Net.Cookie
    $cookie.Name = "ARRAffinity"
    $cookie.Value = $instanceName
    $Cookie.Domain = $uri.DnsSafeHost
    $session=New-Object Microsoft.Powershell.Commands.WebRequestSession
    $session.Cookies.add($cookie)
     $response = Invoke-RestMethod -Uri "$apiBaseUrl/command" `
                                -Headers @{Authorization=("Basic {0}" `
                                -f $base64AuthInfo)} `
                                -Method Post -Body $json `
                                -ContentType 'application/json' `
                                -WebSession $session
    Write-Host "`tVM Instance Name `t: " $response

    # perform copy file
    $response = Invoke-RestMethod -Uri "$apiBaseUrl/command" `
                               -Headers @{Authorization=("Basic {0}" `
                               -f $base64AuthInfo)} `
                               -Method Post -Body $json2 `
                               -ContentType 'application/json' `
                               -WebSession $session
    Write-Host "`tCopy file result `t: " $response

    # perform delete file
    $response = Invoke-RestMethod -Uri "$apiBaseUrl/command" `
                               -Headers @{Authorization=("Basic {0}" `
                               -f $base64AuthInfo)} `
                               -Method Post -Body $json3 `
                               -ContentType 'application/json' `
                               -WebSession $session
    Write-Host "`tCopy file result `t: " $response
 }    

文件系统支持copy和delete等基本命令,因此您可以轻松地重命名文件。

【讨论】:

  • 谢谢!我注意到顶部的“Login-AzureRmAccount”——它在 Azure DevOps 中如何工作?我可以通过一些凭据吗?
  • 更好。使用 Azure PowerShell 任务建立与 Azure 订阅的服务连接。 PowerShell 任务将在该上下文中执行。
  • 而且,既然你是新人:如果这回答了你的问题,你介意接受答案(并投票赞成)吗?
猜你喜欢
  • 2020-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多