为自动管道动态设置参数值

Setting parameter value dynamically for automatic pipelines

如果我创建一个参数,我可以在 运行 手动设置管道时设置它的值。但是当管道自动 运行s 时,它使用默认值。当管道 运行s 自动(比如响应推送到 repo)时,有没有办法将参数值传递给它?

这是我正在玩弄的 yaml 文件。目标是能够控制管道中的哪些测试 运行。

parameters:
  - name: testFiles
    type: string
    default: cypress/integration/first.spec.js

trigger:
  - azure-test

pool:
  vmImage: ubuntu-latest

steps:
  - task: NodeTool@0
    inputs:
      versionSpec: "10.x"
    displayName: "Install Node.js"

  - script: npm install
    displayName: "npm install"

  - script: npx cypress run --record --key [record key removed] --spec ${{ parameters.testFiles }}
    displayName: "run cypress"

When a pipeline runs automatically (say in response to pushing to a repo) is there any way to pass it the value of a parameter?

当运行启用自动管道时,它只会使用默认参数值。

所以我们可以通过改变参数的默认值来实现这个需求

根据我的测试,我找到了一个方便的方法:您可以使用If expression来检查触发方式(手动或CI)。然后你可以设置参数的值。

注意:定义参数时不能使用if表达式,需要使用变量传值

你可以参考this ticket

这是我的例子:

trigger:
- master


variables:
   ${{ if eq( variables['Build.Reason'], 'IndividualCI' ) }}: 
    buildVersion: $(BUILD.SOURCEVERSIONMESSAGE)
   ${{ if ne( variables['Build.Reason'], 'IndividualCI' ) }}: 
    buildVersion: cypress/integration/first.spec.js

parameters:
  - name: testFiles
    type: string  
    default: $(buildVersion)


pool:
  vmImage: ubuntu-latest

steps:
- script: echo ${{ parameters.testFiles }}
  displayName: 'Run a one-line script'

变量$(Build.Reason)用于确定触发方式

变量 $(BUILD.SOURCEVERSIONMESSAGE) 包含提交的消息。

步骤如下:

  1. 当你推送repo 中的更改时,你需要添加评论。注释为测试文件路径。

  1. CI 触发的管道将获取此注释并将其设置为参数默认值。

在这种情况下,类似于手动运行设置管道以设置参数值。

当您手动 运行 管道时,它将使用定义的默认值。您也可以在手动 运行 管道时修改该值。