Jenkins具有带下拉列表等的UI概念,以允许用户在运行时指定变量。在我们的构建中,事实证明这很重要,可以在管道中做出决策(即,要在哪个代理上运行,选择的代码库等)。通过允许参数,我们可以使单个管道/定义对许多客户端/发行版/环境处理相同的任务。
过去一年,我一直看着很多人要求这样做-消除几乎相同的构建定义的数量-是否有最佳实践来解决这一问题?对于一个特定的任务有一个单一的构建定义,并且可以足够聪明地处理参数,将是一个很好的选择。
运行时参数现在可用。现在,您可以使用parameters
在管道YAML的开头设置运行时参数。对于以下示例:
parameters:
- name: image
displayName: Pool Image
default: ubuntu-latest
values:
- windows-latest
- vs2017-win2016
- ubuntu-latest
- ubuntu-16.04
- macOS-latest
- macOS-10.14
- name: test
displayName: Run Tests?
type: boolean
default: false
trigger: none
jobs:
- job: build
displayName: Build and Test
pool:
vmImage: ${{ parameters.image }}
steps:
- script: echo building $(Build.BuildNumber)
- ${{ if eq(parameters.test, true) }}:
- script: echo "Running all the tests"
以上示例来自Microsoft官方文档。单击here了解有关运行时参数的更多信息。
当您在Yaml管道上方运行时,将能够从下拉列表中选择参数的值。请参见下面的屏幕截图。
更新:在运行时动态设置变量。
您可以使用task.setvariable
记录命令在脚本中动态设置变量。
对于下面的示例:$ resultValue是来自rest api调用的值。并将其值分配给变量VariableName
- powershell: |
$resultValue = call from Rest API
echo "##vso[task.setvariable variable=VariableName]$resultValue"
检查文档here以获得更多信息。