如何 set/pass 运行时变量到 azure pipeline yaml 中的 powershell 任务
How to set/pass runtime variable to powershell task in azure pipeline yaml
我的 azure yaml 管道中有以下 powershell 任务,但是变量没有在 $releaseCommitIDApiUrl.Path 中的 getLRId 中被替换。它只是在脚本执行时按原样打印变量,但因错误而失败。
在 Path 中使用原始变量 $getLatestReleaseId 同样的问题,它具有在运行时分配的值。
Write-Host "`$getLatestReleaseId = $getLatestReleaseId"
Write-Host "##vso[task.setvariable variable=getLRId;]$($getLatestReleaseId)"
# Get release Commit ID API endpoint URL
$releaseCommitIDApiUrl = New-Object System.UriBuilder -ArgumentList 'https://vsrm.dev.azure.com'
$releaseCommitIDApiUrl.Path = '${{ parameters.organizationName }}/${{ parameters.projectName }}/_apis/release/releases/$(getLRId)'
$releaseCommitIDApiUrl.Query = 'api-version=6.0'
$getLatestCommitIdResponse = Invoke-RestMethod -Uri $releaseCommitIDApiUrl.Uri.AbsoluteUri -Headers $header -Method GET
下面是执行流水线得到的结果。 558 是分配给变量的值,但未在 URL.
中替换
$getLatestReleaseId = 558
##[debug]Processed: ##vso[task.setvariable variable=getLRId;]558
$releaseCommitIDApiUrl = https://vsrm.dev.azure.com:443/<<<maskedvalue>>>/_apis/release/releases/$(getLRId)?api-version=6.0
Invoke-RestMethod: <agent path>/598c95cb-d092-4272-ae79-20d595fc229e.ps1:63
Line |
63 | … dResponse = Invoke-RestMethod -Uri $releaseCommitIDApiUrl.Uri.Absolut …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| {"$id":"1","innerException":null,"message":"VS402853: The
| parameter with name releaseId should be of type Int32, but the
| specified value is not convertible to
| Int32.","typeName":"Microsoft.VisualStudio.Services.ReleaseManagement.Data.Exceptions.InvalidRequestException, Microsoft.VisualStudio.Services.ReleaseManagement2.Data","typeKey":"InvalidRequestException","errorCode":0,"eventId":3000}
getLRId 变量在字符串内部,这就是它无法被计算的原因。
试试这个:
$releaseCommitIDApiUrl.Path = '${{ parameters.organizationName }}/${{ parameters.projectName }}/_apis/release/releases/'+$getLRId
我的 azure yaml 管道中有以下 powershell 任务,但是变量没有在 $releaseCommitIDApiUrl.Path 中的 getLRId 中被替换。它只是在脚本执行时按原样打印变量,但因错误而失败。 在 Path 中使用原始变量 $getLatestReleaseId 同样的问题,它具有在运行时分配的值。
Write-Host "`$getLatestReleaseId = $getLatestReleaseId"
Write-Host "##vso[task.setvariable variable=getLRId;]$($getLatestReleaseId)"
# Get release Commit ID API endpoint URL
$releaseCommitIDApiUrl = New-Object System.UriBuilder -ArgumentList 'https://vsrm.dev.azure.com'
$releaseCommitIDApiUrl.Path = '${{ parameters.organizationName }}/${{ parameters.projectName }}/_apis/release/releases/$(getLRId)'
$releaseCommitIDApiUrl.Query = 'api-version=6.0'
$getLatestCommitIdResponse = Invoke-RestMethod -Uri $releaseCommitIDApiUrl.Uri.AbsoluteUri -Headers $header -Method GET
下面是执行流水线得到的结果。 558 是分配给变量的值,但未在 URL.
中替换
$getLatestReleaseId = 558
##[debug]Processed: ##vso[task.setvariable variable=getLRId;]558
$releaseCommitIDApiUrl = https://vsrm.dev.azure.com:443/<<<maskedvalue>>>/_apis/release/releases/$(getLRId)?api-version=6.0
Invoke-RestMethod: <agent path>/598c95cb-d092-4272-ae79-20d595fc229e.ps1:63
Line |
63 | … dResponse = Invoke-RestMethod -Uri $releaseCommitIDApiUrl.Uri.Absolut …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| {"$id":"1","innerException":null,"message":"VS402853: The
| parameter with name releaseId should be of type Int32, but the
| specified value is not convertible to
| Int32.","typeName":"Microsoft.VisualStudio.Services.ReleaseManagement.Data.Exceptions.InvalidRequestException, Microsoft.VisualStudio.Services.ReleaseManagement2.Data","typeKey":"InvalidRequestException","errorCode":0,"eventId":3000}
getLRId 变量在字符串内部,这就是它无法被计算的原因。 试试这个:
$releaseCommitIDApiUrl.Path = '${{ parameters.organizationName }}/${{ parameters.projectName }}/_apis/release/releases/'+$getLRId