我正在寻找一种在我的azure-pipelines.yml文件中定义变量的方法,我可以在其中对'Build.SourceVersion'-> 仅使用前 7 个字符进行子串。
文档中似乎没有可以执行此类字符串操作的内置函数。有什么我想念的吗?
我的另一种方法是使用 bash 任务并覆盖那里的变量,但找到可以做到这一点的内置方法将是更好的解决方案。
我使用 Variables.yml 文件作为模板来存储不同的变量,我很好奇是否能够引用 yml 文件本身中的变量来本质上嵌套它们。
例如:
#variables.yml file
variables:
food1: 'pineapple'
food2: 'pizza'
favoriteFood: '$(food1) $(food2)'
Run Code Online (Sandbox Code Playgroud)
这样,当我最终调用这个变量“favoriteFood”时,我可以只使用 ${{ Variables.favoriteFood }} 并且该值应该是“pineapple Pizza”示例:
#mainPipeline.yml file
variables:
- template: 'variables.yml'
steps:
- script: echo My favorite food is ${{ variables.favoriteFood }}.
Run Code Online (Sandbox Code Playgroud)
我走在正确的轨道上吗?我似乎无法通过谷歌搜索任何关于这是否可能的示例。
可以为单独的脚本设置工作目录:
- script: foo
workingDirectory: bar
Run Code Online (Sandbox Code Playgroud)
但是,如果所有步骤都打算在特定目录中运行,则为每个步骤定义它就会变得重复。
使用cd不影响其他步骤:
- script: cd foo
- script: pwd # returns default working dir instead of foo
Run Code Online (Sandbox Code Playgroud)
这个问题很重要的两个具体例子是:
我正在尝试根据 yaml 管道中的参数值设置变量。我似乎读过许多其他帖子,其中显示了作者所说的有效示例,但在尝试执行如下操作时,我无法解决过去的问题。
我也尝试了这个例子的许多变体,这里无法列出。有时它会将“值”显示为重复键。在其他情况下,我已经能够尝试启动运行并获得环境选择的提示,但打开阶段对话框会引发解析错误。
文件顶部的变量声明与阶段或作业中的变量声明之间是否存在某种差异?这似乎是我在阅读其他示例时注意到的差异。
最终我想做的是根据环境参数的值设置ServiceConnection变量值。
parameters:
- name: environment
displayName: Environment
type: string
values:
- DEV
- TEST
pr: none
trigger: none
pool: PrivateAgentPool
variables:
- name: 'isMain'
value: $[eq(variables['Build.SourceBranch'], 'refs/heads/main')]
- name: 'buildConfiguration'
value: 'Release'
- name: 'environment'
value: ${{ parameters.environment }}
- name: 'ServiceConnection'
${{ if eq(variables['environment'], 'DEV') }}:
value: 'svcConnectionDev'
${{ if eq(variables['environment'], 'TEST') }}:
value: 'svcConnectionTest'
Run Code Online (Sandbox Code Playgroud) 在 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: ${{ …Run Code Online (Sandbox Code Playgroud) 我使用 Azure Pipelines 作为 Azure DevOps 的一部分。我试图在我的模板文件中定义变量,因为我需要多次使用相同的值。
这是我的stage-template.yml:
parameters:
- name: param1
type: string
- name: param2
type: string
variables:
var1: path/${{ parameters.param2 }}/to-my-file.yml
stages:
- stage: Deploy${{ parameters.param2 }}
displayName: Deploy ${{ parameters.param1 }}
jobs:
...
Run Code Online (Sandbox Code Playgroud)
尝试使用此管道时,我收到一条错误消息:
/stage-template.yml(第 7 行,第 1 列):意外值“变量”
为什么这不起作用?我做错了什么?
我正在尝试根据条件在特定池上执行 Azure DevOps 作业。目标是在自托管代理和微软代理之间切换。这是配置:
parameters:
custom_agent: true
jobs:
- job: Test
displayName: Test job
- ${{ if eq(parameters.custom_agent, true) }}:
- pool:
name: mypool
demands:
- agent.os -equals Linux
- ${{ if eq(parameters.custom_agent, false) }}:
- pool:
vmImage: 'ubuntu-latest'
steps:
- task: npmAuthenticate@0
Run Code Online (Sandbox Code Playgroud)
有任何想法吗 ?
是否可以在模板 YAML 中包含参数及其值,并在准备运行管道时让主 YAML 读取列表并将它们显示在下拉列表中?
与“变量重用”的 MS 示例非常相似,如下所示。
# File: vars.yml
variables:
favoriteVeggie: 'brussels sprouts'
Run Code Online (Sandbox Code Playgroud)
# File: azure-pipelines.yml
variables:
- template: vars.yml # Template reference
steps:
- script: echo My favorite vegetable is ${{ variables.favoriteVeggie }}.
Run Code Online (Sandbox Code Playgroud)
我有一个大约 50 个值的列表,这些值每周都在增长。当运维人员运行我的管道时,我希望他们从下拉列表中选择一个值。
这可以简单地通过在管道 YAML 文件中指定参数块来实现azure-pipeline.yml:
parameters:
- name: subscription
displayName: Subscription Name
type: string
default: sub A
values:
- sub A
- sub B
- sub C
- sub D
Run Code Online (Sandbox Code Playgroud)
这给了我 UI 中的下拉菜单:
在上面的示例中,只有 4 个值,但是,我们有超过 50 …
我有一个天蓝色的管道并使用参数运行,其中有多个选项,如下所示:
选择测试执行
如果我选择Product,则执行product.js文件,如果选择,Product with Cost则执行"productCost.js",依此类推。我编写了一个天蓝色管道脚本来执行此操作。我有另一个条件"Generate Test Data"复选框,它返回布尔值true,或者false如果该值是true,那么我必须选择一个文件productWithTestData.js(如果Product选择)-我不知道如何在Azure管道代码中编写if else条件。请找到我的伪代码
有人可以帮助我如何为我的用例编写 if else 条件 - 提前感谢您的帮助!谢谢!
variables:
- name: fileName
${{ if eq(parameters.selectTestRun, 'Product') }}:
value: 'product.js'
else ${{ if eq(parameters.selectTestRun, 'Product') } && { if eq(parameters.testData, True) }}:
value: 'productWithTestData.js'
Run Code Online (Sandbox Code Playgroud)
我的管道代码:
trigger:
none
parameters:
- name: selectTestRun
displayName: Select test execution
type: string
default: Product
values: …Run Code Online (Sandbox Code Playgroud) 我正在尝试循环访问 Azure DevOps YAML 管道中的用户定义变量。
变量已通过 UI 创建:
在我正在使用的 YAML 管道代码下方:
trigger:
- dev
- main
pr:
- dev
pool:
vmImage: ubuntu-latest
stages:
- stage:
jobs:
- job: TestVars
steps:
- ${{ each var in variables }}:
- script: |
echo ${{ var.key }}
echo ${{ var.value }}
displayName: ${{ var.key }}
Run Code Online (Sandbox Code Playgroud)
当运行上述管道时,仅列出系统和构建变量(例如system、system.hostType、build.queuedBy等)。
任何循环用户定义变量的帮助将不胜感激。