Azure DevOps YAML 管道 - 计划触发器参数

问题描述 投票:0回答:1

在 Azure DevOps YAML 管道中,您可以创建计划触发器。但是,每个计划是否可以有自己的一组参数值?这样的事情可能看起来像这样:

parameters:
- name: myParam
  displayName: My Parameter
  type: string
  default: option-a
  values:
  - option-a
  - option-b

schedules:
- cron: "0 5 * * *"
  displayName: "Midnight CT Daily"
  branches:
    include:
    - master
  parameters:
  - myParam: option-b
  always: true
powershell azure-devops yaml
1个回答
0
投票

不幸的是,

schedules.cron
对象的YAML架构没有任何允许输入特定参数的属性。

https://learn.microsoft.com/en-us/azure/devops/pipelines/yaml-schema/schedules?view=azure-pipelines

https://learn.microsoft.com/en-us/azure/devops/pipelines/yaml-schema/schedules-cron?view=azure-pipelines

但是,我可以通过利用时间表的显示名称来解决这个问题。从那里,您可以利用 PowerShell 或 Bash 读取

$(Build.CronSchedule.DisplayName)
变量并将其用作输入。您甚至可以分隔该值以解析出多个值。

parameters:
- name: food
  type: string
  default: roast-beef
  values:
  - apple
  - banana
  - roast-beef
  - yogurt

schedules:
- cron: "0 5 * * *"
  displayName: "apple"
  branches:
    include:
    - master
  always: true
- cron: "0 7 * * *"
  displayName: "banana;yogurt"
  branches:
    include:
    - master
  always: true

stages:
- stage: mealtime
  jobs:
  - deployment: snack
    environment: kitchen
    strategy:
      runOnce:
        deploy:
          steps:
          - pwsh: |
              $targetFoods = @("${{ parameters.food }}")
              
              if ("$(Build.Reason)" -eq "Schedule") {
                $targetFoods = "$(Build.CronSchedule.DisplayName)".Split(";")
              }
              
              Write-Host "Foods to eat: $targetFoods"
              Write-Host "##vso[task.setvariable variable=foods;]$targetFoods"
            displayName: Set Variables

          - pwsh: |
              $(foods) | ForEach-Object { Write-Host "Eating: $_"; $_ | Eat-PsFood }
            displayName: Eat Food
© www.soinside.com 2019 - 2024. All rights reserved.