Azure DevOps YAML 管道计划运行

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

我有一个管道需要按计划运行。管道当前具有每个环境的变量,如下所示:

variables:
- ${{ if eq(parameters.envName, 'Test') }}:
  - name: subscription
    value: testsub
  - name: storage_account
    value: testsa
  - name: other_resource_branch
    value: test
- ${{ if eq(parameters.envName, 'Staging') }}:
  - name: subscription
    value: stagingsub
  - name: storage_account
    value: stagingsa
  - name: other_resource_branch
    value: staging
- ${{ if eq(parameters.envName, 'Prod') }}:
  - name: subscription
    value: prodsub
  - name: storage_account
    value: prodsa
  - name: other_resource_branch
    value: prod

我想为每个环境制定时间表,类似这样,这将使管道在每个环境的周一、周二和周三按计划运行:

schedules:
- cron: '0 0 * * 1'
  displayName: Test schedule
  branches:
    include:
    - master
- cron: '0 0 * * 2'
  displayName: Staging schedule
  branches:
    include:
    - master
- cron: '0 0 * * 3'
  displayName: Prod schedule
  branches:
    include:
    - master

有没有办法将每个计划与相应的变量相关联? 据我在ADO文档中看到的,没有官方的方法,但是有可能有解决方法吗?

azure azure-devops cron yaml
1个回答
0
投票

恐怕没有现成的变量可以区分三个调度触发器。

据我在ADO文档中看到的,没有官方的方法,但是有可能有解决方法吗?

是的。您可以参考以下解决方法:

1.您可以根据计划触发日期将Pipeline拆分为3个独立的Pipeline。

此时,您可以为每个管道设置相应的变量。

当 Pipelines 被相应的调度触发器触发时,变量将按预期设置。

2.如果您需要将环境保持在同一个管道中,您可以添加额外的阶段来检查计划触发日期。

然后我们可以设置 Pipeline 变量并将其传递到下一个阶段,以确认应该运行哪个环境。

这是一个例子:

schedules:
- cron: '0 0 * * 1'
  displayName: Test schedule
  branches:
    include:
    - master
- cron: '0 0 * * 2'
  displayName: Staging schedule
  branches:
    include:
    - master
- cron: '0 0 * * 3'
  displayName: Prod schedule
  branches:
    include:
    - master

stages:
- stage: CheckDate
  jobs:
  - job: CheckDate
    pool: 
      vmImage: windows-latest
    steps:
    - powershell: |
        $Time = Get-Date
        $Dayofweek = $Time.ToUniversalTime().DayOfWeek
        echo $Dayofweek
        #noted the time here is UTC time, please change the time value accordingly 
        if($Dayofweek -eq "Wednesday"){
          echo "##vso[task.setvariable variable=envName;isOutput=true]Prod"
        }
        if($Dayofweek -eq "Monday"){
          echo "##vso[task.setvariable variable=envName;isOutput=true]Test"
        }
        if($Dayofweek -eq "Tuesday"){
          echo "##vso[task.setvariable variable=envName;isOutput=true]Staging"
        }      
      name: CheckDate
   
    - script:  echo $(CheckDate.envName)

- stage: RunPipeline
  variables:
    - name: 'envName'
      value: $[stageDependencies.CheckDate.CheckDate.outputs['CheckDate.envName']]
    
  jobs:
  - job: Prod
    condition: eq(variables['envName'], 'Prod')
    variables:
    - name: subscription
      value: testsub
    - name: storage_account
      value: testsa
    - name: other_resource_branch
      value: test
    steps:
      - script: echo $(envName)

  - job: Test
    condition: eq(variables['envName'], 'Test')
    variables:
    - name: subscription
      value: testsub
    - name: storage_account
      value: testsa
    - name: other_resource_branch
      value: test
    steps:
      - script: echo $(envName)

  - job: Staging
    condition: eq(variables['envName'], 'Staging')
    variables:
    - name: subscription
      value: testsub
    - name: storage_account
      value: testsa
    - name: other_resource_branch
      value: test
    steps:
      - script: echo $(envName)

结果:今天是星期三。

有关更详细的信息,您可以参考有关跨阶段变量

的文档
© www.soinside.com 2019 - 2024. All rights reserved.