AzureAPI 在 AzureWebApp 中重新部署解决方案

问题描述 投票:0回答:1

这里有人知道 Azure API 在 Azure 管道中重新部署解决方案吗

我尝试搜索 azure 门户,找到一些用于列出管道的 api,但我无法找到触发 azure 中重新部署的 api....

azure-devops tfs azure-web-app-service
1个回答
0
投票

如果您的意思是通过在 Azure DevOps 管道中调用 REST API 来重新部署,那么您可以尝试以下示例。

对于经典发布管道,我们可以调用Releases - 更新发布环境 REST API来触发特定阶段(环境)。

PATCH https://vsrm.dev.azure.com/{organization}/{project}/_apis/Release/releases/{releaseId}/environments/{environmentId}?api-version=7.1-preview.7

以下 PowerShell 供您参考:(例如本示例中重新部署“Test”阶段)

Param(
   [string]$collectionurl = "https://vsrm.dev.azure.com/{organization}", 
   [string]$project = "Your Project Name",
   [string]$releaseid = "1",    #Your release ID
   [string]$stagename = "Test", #The stage you want to redeploy
   [string]$user = "",
   [string]$token = "PAT Here"
)

# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))

#Get Environment ID
$releaseurl = "$collectionurl/$project/_apis/release/releases/$($releaseid)?api-version=5.1"            
$response = Invoke-RestMethod -Uri $releaseurl -Method Get -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}

$environmentId = ($response.environments | where {$_.name -eq $stagename}).id
Write-Host "Environment Id of state $stagename is" $environmentId

$body = @"
{
  "status": "inProgress",
  "scheduledDeploymentTime": null,
  "comment": null,
  "variables": {}
}
"@

$redeployurl  = "$collectionurl/$project/_apis/release/releases/$($releaseid)/environments/$($environmentId)?api-version=5.1-preview.6" 

#Redeploy the specific environment
$redeploy = Invoke-RestMethod -Uri $redeployurl -Method PATCH -Body $body -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}

对于YAML管道,我们可以调用Stages - UpdateREST API来触发/重新部署特定阶段。

PATCH https://dev.azure.com/{organization}/{project}/_apis/build/builds/{buildId}/stages/{stageRefName}?api-version=7.1-preview.1

以下 PowerShell 脚本供您参考:(在本示例中重新部署 Dev 阶段)

Param(
   [string]$collectionurl = "https://dev.azure.com/{organization}", 
   [string]$project = "Your Project Name",
   [string]$buildid = "117",
   [string]$stagename = "Dev",
   [string]$user = "",
   [string]$token = "PAT"
)

# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))

$stageurl = "$collectionurl/$project/_apis/build/builds/$buildid/stages/$($stagename)?api-version=7.1-preview.1"            
cls

$body = @"
{
  "state":1,
  "forceRetryAllJobs":false,
  "retryDependencies":true
}
"@
    
#Redeploy the specific environment
$redeploy = Invoke-RestMethod -Uri $stageurl -Method PATCH -Body $body -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}

© www.soinside.com 2019 - 2024. All rights reserved.