简体   繁体   English

在 Azure DevOps 发布管道中,如何将具有不同目标文件夹的部署组部署到多个 VM

[英]In an Azure DevOps release pipeline how do you deploy to multiple VMs a deployment group with different target folders

I started out with a release pipeline setup in azure DevOps to deploy a windows service to a deployment group which had only a single VM with an agent set up.我首先在 azure DevOps 中设置发布管道,将 Windows 服务部署到只有一个 VM 并设置了代理的部署组。 I have a variable set in the pipeline for the deployment folder.我在管道中为部署文件夹设置了一个变量。

I'm now trying to expand this to deploy to 2 servers.我现在正在尝试将其扩展为部署到 2 台服务器。 I've added the second server into the deployment group and the registration has worked.我已将第二台服务器添加到部署组中,并且注册成功。 On this server, the deployment needs to go to a different drive.在此服务器上,部署需要转到不同的驱动器。

There doesn't seem to be a way to specify a different value for the variable by an agent.似乎没有办法为代理指定不同的变量值。

I've tried googling and trawling around in the ui and so far I've found nothing.我试过在 ui 中使用谷歌搜索和拖网搜索,但到目前为止我一无所获。 I'm wondering if variables are even the right thing?我想知道变量是否是正确的?

Im going to answer my own question as the solution is actually a combination of the answers kindly provided by @Martin A @Tomasz Kaniewski and @Vito Liu-MSFT with a fair amount trial and error.我要回答我自己的问题,因为解决方案实际上是@Martin A @Tomasz Kaniewski 和@Vito Liu-MSFT 提供的答案的组合,并进行了大量的反复试验。 I hope this will help others.我希望这会帮助其他人。

Environment Variables are the key to identifying the deployment folder so I set up a system environment variable called AutomationDeploymentRoot on each of my VM's环境变量是识别部署文件夹的关键,因此我在每个 VM 上设置了一个名为 AutomationDeploymentRoot 的系统环境变量

You must restart the Azure Pipelines Agent windows service on each VM before changes to environment variables are picked up!!您必须在每个 VM 上重新启动 Azure Pipelines 代理 Windows 服务,然后才能获取对环境变量的更改!!

I found that the support for environment variables to be quite inconsistent between the different tasks - they seem to work well in script tasks but not so well in others such as CopyFiles.我发现对环境变量的支持在不同任务之间非常不一致——它们似乎在脚本任务中运行良好,但在其他任务(如 CopyFiles)中表现不佳。

The most reliable approach was to copy the environment variable into a pipeline variable (deployment.root) which I set up on the variable tab like so最可靠的方法是将环境变量复制到我在变量选项卡上设置的管道变量 (deployment.root) 中,如下所示

变量的屏幕截图

And then set the variable from script as suggested by Thomasz and Vito然后按照 Thomasz 和 Vito 的建议从脚本中设置变量

 steps: - script: | @echo ##vso[task.setvariable variable=deployment.root]%AutomationDeploymentRoot% displayName: 'Set Deployment Folder'

when this runs the c:\\temp\\deploy is replaced by the correct folder for the target machine and everything goes green!当它运行时, c:\\temp\\deploy 被目标机器的正确文件夹替换,一切都变绿了!

You can set a variable from the script: https://docs.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=classic%2Cpowershell#set-variables-in-scripts您可以从脚本设置变量: https : //docs.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=classic%2Cpowershell# set-variables-in-scripts

Write a script that will be able to determine on which machine it is running and assign a proper value to a variable.编写一个脚本,该脚本将能够确定它在哪台机器上运行并为变量分配适当的值。

Agree with Tomasz.同意托马斯。

We can set the variable via the power script Write-Host " ##vso[task.setvariable variable={variable name}]{variable value} ", then we can call the variable in the another task.我们可以通过power脚本Write-Host“ ##vso[task.setvariable variable={variable name}]{variable value} ”设置##vso[task.setvariable variable={variable name}]{variable value} ,然后我们可以在另一个任务中调用该变量。

Please refer this answer for more details请参阅此答案以获取更多详细信息

In addition, we can update the release pipeline variable via this API Update a release definition .此外,我们可以通过此 API 更新发布管道变量Update a release definition

a .一个 Open the release pipeline and add a new variable test and grant test Build Service (xxx) account the Edit release pipeline permission.打开发布管道并添加一个新变量 test 并授予test Build Service (xxx)帐户编辑发布管道权限。 (open the release pipeline--> ... --> Security --> Edit release pipeline set to Allow). (打开发布管道--> ... --> 安全性--> 编辑发布管道设置为允许)。

b . Open pipeline, enable the feature Allow scripts to access the OAuth token (Click Agent Job Name=>Additional options) add task powershell and enter the script below to update the release variable test value.打开管道,启用允许脚本访问 OAuth 令牌功能(单击代理作业名称=>附加选项)添加任务 powershell 并输入下面的脚本以更新发布变量测试值。

$url = "https://vsrm.dev.azure.com/{org name}/{project name}/_apis/release/definitions/{release definition ID}?api-version=6.0-preview.4"
Write-Host "URL: $url"
$pipeline = Invoke-RestMethod -Uri $url -Headers @{
    Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
}
Write-Host "Pipeline = $($pipeline | ConvertTo-Json -Depth 100)"

# Update an existing variable named test to its new value 
$pipeline.variables.test.value= {new variable value}

####****************** update the modified object **************************
$json = @($pipeline) | ConvertTo-Json -Depth 99


$updatedef = Invoke-RestMethod -Uri $url -Method Put -Body $json -ContentType "application/json" -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}

write-host "==========================================================" 
Write-host "The value of Varialbe 'test' is updated to" $updatedef.variables.test.value
write-host "=========================================================="

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何部署到 Azure Devops 部署组中的单个选定目标 - How to deploy to a single selected target in an Azure Devops Deployment Group Azure Devops Release Pipeline 中针对不同分支的多个调度 - Multiple schedules in Azure Devops Release Pipeline for different branches Azure DevOps 部署组目标离线 - Azure DevOps Deployment Group Target Offline Azure DevOps - 使用部署组或部署作业向环境发布管道? - Azure DevOps - Release Pipeline using Deployment Groups or Deployment Job to an Environment? 如何从 Azure DevOps 管道将多个发布或构建导出到桌面? - How to export multiple Release or Builds from Azure DevOps Pipeline to desktop? Azure Devops Release Pipeline - 将单个文件部署到Azure App Service - Azure Devops Release Pipeline - Deploy single file to Azure App Service Azure DevOps 发布管道 - Azure Devops Release Pipeline Azure 用于本地按需 kube.netes 部署的 Devops 发布管道 - Azure Devops release pipeline for local on-demand kubernetes deployment Azure DevOps 发布管道消除了应用服务部署槽 - Azure DevOps Release Pipeline wipes out App Service Deployment Slot Azure Devops - 如何从发布管道选项内的下拉列表中添加额外的部署类型 - Azure Devops - How to Add an extra Deployment Type from drop down list inside Release pipeline Options
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM