简体   繁体   中英

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. I have a variable set in the pipeline for the deployment folder.

I'm now trying to expand this to deploy to 2 servers. 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. 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. 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

You must restart the Azure Pipelines Agent windows service on each VM before changes to environment variables are picked up!!

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.

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

变量的屏幕截图

And then set the variable from script as suggested by Thomasz and 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!

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

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.

Please refer this answer for more details

In addition, we can update the release pipeline variable via this 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. (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.

$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 "=========================================================="

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