GGi*_*ard 8 yaml azure-devops azure-pipelines azure-pipelines-yaml
在 Azure DevOps 管道模板中,我将参数声明为数组/序列
parameters:
mySubscription: ''
myArray: []
steps:
- AzureCLI@2
inputs:
azureSubscription: ${{ parameters.mySubscription }}
scriptType: pscore
scriptPath: $(Build.SourcesDirectory)/script.ps1
arguments: '-MyYAMLArgument ${{ parameters.myArray }}'
Run Code Online (Sandbox Code Playgroud)
然后从管道定义传递参数的值作为
steps:
- template: myTemplate.yml
parameters:
mySubscription: 'azure-connection'
myArray:
- field1: 'a'
field2: 'b'
- field1: 'aa'
field2: 'bb'
Run Code Online (Sandbox Code Playgroud)
我的问题是我无法在 YAML 语法(某种ToString()
)中按原样传递该数组,以便能够在我的模板中从 PowerShell 使用和处理该数组。尝试运行此管道时,出现以下错误:
/myTemplate.yml (Line: X, Col: X): Unable to convert from Array to String. Value: Array
. 错误消息中引用的行/列对应arguments: '-MyYAMLArgument ${{ parameters.myArray }}'
于我的模板。
我还尝试将参数映射为脚本的环境
- AzureCLI@2
inputs:
azureSubscription: ${{ parameters.mySubscription }}
scriptType: pscore
scriptPath: $(Build.SourcesDirectory)/script.ps1
arguments: '-MyYAMLArgument $Env:MY_ENV_VAR'
env:
MY_ENV_VAR: ${{ parameters.myArray }}
Run Code Online (Sandbox Code Playgroud)
这也不起作用:
/myTemplate.yml (Line: X, Col: Y): A sequence was not expected
。该时间线/列指的是MY_ENV_VAR: ${{ parameters.myArray }}
.
有没有人遇到过类似的要求,将管道定义中定义的复杂类型(这里是对象的数组/序列)传递给 PowerShell 脚本?如果是这样,你是如何实现的?
MaM*_*zav 10
基于@ed-randall的convertToJson想法,结合ConvertFrom-Json Powershell函数,我们可以使用JSON“契约”在yaml和PS脚本之间传递值:
- powershell: |
$myArray = '${{ convertToJson(parameters.myArray) }}' | ConvertFrom-Json
...
Run Code Online (Sandbox Code Playgroud)
小智 6
我面临着类似的问题。我的解决方法是使用不同维度的不同分隔符将数组展平为字符串。
例如,我想设置一些必需的参数,如果未传递这些参数,则构建失败。我不想为每个要检查的参数添加一个任务,而是想在单个任务中执行此操作。
为此,我首先将一个数组作为参数(传递给另一个模板,调用check-required-params.yml
该模板,该模板负责检查参数),其中每个元素都是类型的字符串,name:value
该字符串是以下类型的串联(使用format
表达式)name
以及value
用冒号分隔的必需参数:
# templates/pipeline-template.yml
parameters:
- name: endpoint
type: string
default: ''
- name: rootDirectory
type: string
default: $(Pipeline.Workspace)
- name: remoteDirectory
type: string
default: '/'
- name: archiveName
type: string
default: ''
#other stuff
- template: check-required-params.yml
parameters:
requiredParams:
- ${{ format('endpoint:{0}', parameters.endpont) }}
- ${{ format('archiveName:{0}', parameters.archiveName) }}
Run Code Online (Sandbox Code Playgroud)
然后在 中check-required-params.yml
,我使用表达式连接数组,并用分号分隔元素${{ join(';', parameters.requiredParams) }}
。这将创建该类型的字符串endpoint:value;archiveName:value
并将其作为环境变量传递。
此时,使用一些字符串操作,在脚本中我可以使用分号作为分隔符来分割字符串,这样我将得到一个字符串数组,name:value
我可以进一步分割它,这次使用冒号作为分隔符。我的check-required-params.yml
样子:
# templates/check-required-params.yml
parameters:
- name: requiredParams
type: object
default: []
steps:
- task: PowerShell@2
env:
REQURED_PARAMS: ${{ join(';', parameters.requiredParams) }}
displayName: Check for required parameters
inputs:
targetType: inline
pwsh: true
script: |
$params = $env:REQURED_PARAMS -split ";"
foreach($param in $params) {
if ([string]::IsNullOrEmpty($param.Split(":")[1])) {
Write-Host "##vso[task.logissue type=error;]Missing template parameter $($param.Split(":")[0])"
Write-Host "##vso[task.complete result=Failed;]"
}
}
Run Code Online (Sandbox Code Playgroud)
然后在我的azure-pipelines.yml
我可以做:
#other stuff
- template: templates/pipeline-template.yml
parameters:
endpoint: 'myEndpoint'
rootDirectory: $(Pipeline.Workspace)/mycode
Run Code Online (Sandbox Code Playgroud)
在这个例子中,构建将失败,因为我没有传递参数archiveName
。
您可以通过使用变量定义分隔符来增加一些灵活性,而不是在脚本和表达式中对它们进行硬编码。
您现在可以使用convertToJson
ADO 管道中的函数将这些类型的参数转换为字符串 :
parameters:
- name: myParameter
type: object
default:
name1: value1
name2: value2
...
- task: Bash@3
inputs:
targetType: inline
script: |
echo "${{ convertToJson(parameters.myParameter) }}"
Run Code Online (Sandbox Code Playgroud)
参考:https : //developercommunity.visualstudio.com/t/allow-type-casting-or-expression-function-from-yam/880210
convertToJson:https ://docs.microsoft.com/en-us/azure/devops/pipelines/process/expressions ? view = azure-devops#converttojson
归档时间: |
|
查看次数: |
12518 次 |
最近记录: |