Azure DevOps 管道模板 - 如何连接参数
Azure DevOps pipeline template - how to concatenate a parameter
整个下午我都在努力思考如何在 ADO 模板中连接参数。该参数是源路径,在模板中需要添加下一个文件夹级别。我想通过“简单”的连接来实现这一点。
简化模板采用参数并使用它来形成 PowerShell 脚本的 inputPath,如下所示:
parameters:
sourcePath: ''
steps:
- task: PowerShell@2
inputs:
filePath: 'PSRepo/Scripts/MyPsScript.ps1'
arguments: '-inputPath ''$(sourcePath)/NextFolder''
我尝试了多种方法来实现这种串联:
- '$(sourcePath)/NextFolder'
- 见上文
- '$(variables.sourcePath)/下一个文件夹'
- 我知道 sourcePath 不是变量,但基于在任务条件中使用参数显然仅在通过变量引用时有效的事实进行了尝试
- '${{ parameters.sourcePath }}/NextFolder'
还有一些其他变体,都无济于事。
我还尝试在模板中引入 variables 部分,但这是不可能的。
我在互联网上搜索了 examples/documentation,但没有直接的答案,其他问题似乎暗示了一些解决方案,但没有奏效。
如果有人能帮助我,我一定会很高兴。
提前致谢
我们可以在我们的临时yaml文件中添加变量,并将sourcePath传递给变量,然后我们就可以使用它了。这是我的演示脚本:
Main.yaml
resources:
repositories:
- repository: templates
type: git
name: Tech-Talk/template
trigger: none
variables:
- name: Test
value: TestGroup
pool:
# vmImage: windows-latest
vmImage: ubuntu-20.04
extends:
template: temp.yaml@templates
parameters:
agent_pool_name: ''
db_resource_path: $(System.DefaultWorkingDirectory)
# variable_group: ${{variables.Test}}
temp.yaml
parameters:
- name: db_resource_path
default: ""
# - name: 'variable_group'
# type: string
# default: 'default_variable_group'
- name: agent_pool_name
default: ""
stages:
- stage:
jobs:
- job: READ
displayName: Reading Parameters
variables:
- name: sourcePath
value: ${{parameters.db_resource_path}}
# - group: ${{parameters.variable_group}}
steps:
- script: |
echo sourcePath: ${{variables.sourcePath}}
- powershell: echo "$(sourcePath)"
在这里,我只是使用工作目录作为测试路径。您也可以使用变量。
附上我的构建结果:
谢谢玉君。同时确实让它工作了。显然一定是有一些拼写错误阻止了脚本的正确执行,因为 se 解决方案看起来像上面提到的选项之一。
parameters:
sourcePath: ''
steps:
- task: PowerShell@2
inputs:
filePath: 'PSRepo/Scripts/MyPsScript.ps1'
arguments: '-inputPath ''$(sourcePath)/NextFolder''
整个下午我都在努力思考如何在 ADO 模板中连接参数。该参数是源路径,在模板中需要添加下一个文件夹级别。我想通过“简单”的连接来实现这一点。 简化模板采用参数并使用它来形成 PowerShell 脚本的 inputPath,如下所示:
parameters:
sourcePath: ''
steps:
- task: PowerShell@2
inputs:
filePath: 'PSRepo/Scripts/MyPsScript.ps1'
arguments: '-inputPath ''$(sourcePath)/NextFolder''
我尝试了多种方法来实现这种串联:
- '$(sourcePath)/NextFolder'
- 见上文
- '$(variables.sourcePath)/下一个文件夹'
- 我知道 sourcePath 不是变量,但基于在任务条件中使用参数显然仅在通过变量引用时有效的事实进行了尝试
- '${{ parameters.sourcePath }}/NextFolder'
还有一些其他变体,都无济于事。 我还尝试在模板中引入 variables 部分,但这是不可能的。
我在互联网上搜索了 examples/documentation,但没有直接的答案,其他问题似乎暗示了一些解决方案,但没有奏效。
如果有人能帮助我,我一定会很高兴。
提前致谢
我们可以在我们的临时yaml文件中添加变量,并将sourcePath传递给变量,然后我们就可以使用它了。这是我的演示脚本:
Main.yaml
resources:
repositories:
- repository: templates
type: git
name: Tech-Talk/template
trigger: none
variables:
- name: Test
value: TestGroup
pool:
# vmImage: windows-latest
vmImage: ubuntu-20.04
extends:
template: temp.yaml@templates
parameters:
agent_pool_name: ''
db_resource_path: $(System.DefaultWorkingDirectory)
# variable_group: ${{variables.Test}}
temp.yaml
parameters:
- name: db_resource_path
default: ""
# - name: 'variable_group'
# type: string
# default: 'default_variable_group'
- name: agent_pool_name
default: ""
stages:
- stage:
jobs:
- job: READ
displayName: Reading Parameters
variables:
- name: sourcePath
value: ${{parameters.db_resource_path}}
# - group: ${{parameters.variable_group}}
steps:
- script: |
echo sourcePath: ${{variables.sourcePath}}
- powershell: echo "$(sourcePath)"
在这里,我只是使用工作目录作为测试路径。您也可以使用变量。
附上我的构建结果:
谢谢玉君。同时确实让它工作了。显然一定是有一些拼写错误阻止了脚本的正确执行,因为 se 解决方案看起来像上面提到的选项之一。
parameters:
sourcePath: ''
steps:
- task: PowerShell@2
inputs:
filePath: 'PSRepo/Scripts/MyPsScript.ps1'
arguments: '-inputPath ''$(sourcePath)/NextFolder''