如何在编译时获取昨天的日期?

huangapple go评论69阅读模式
英文:

How to get yesterday in compile time?

问题

$[format('{0:ddMMyyyy}', pipeline.startTime)] 可以在编译时获取今天的日期。
但是在编译时如何获取昨天的日期呢?因为我想在某些任务的输入中使用它。

我尝试了 pipelilne.startTime.AddDays(-1),但似乎 ADO 不支持它。
对于 '##vso[task.setvariable variable=AAA]BBB',它只在运行时生效,而不是在编译时。

英文:

As we know, we could set the variable like
$[format('{0:ddMMyyyy}', pipeline.startTime)] to get the today in compile time.
But how could we get yesterday in compile time, because I want to use it in some task's input.

I've tried pipelilne.startTime.AddDays(-1), but it looks ADO don't support it.
For '##vso[task.setvariable variable=AAA]BBB', it only takes effect in runtime, not compile time.

答案1

得分: 0

目前,在Azure DevOps中没有内置的变量或表达式可以获取昨天的日期。

作为一种解决方法,您可以尝试在您的YAML管道中将作业(或阶段)作为第一个作业,使用相关的日期命令获取昨天的日期,并将日期设置为输出变量,以便在后续作业(或阶段)中使用。

以下是一个示例作为参考:

  • YAML
jobs:
- job: A
  displayName: 'Job A'
  steps:
  - task: Bash@3
    name: get_yesterday
    displayName: 'Get Date of Yesterday'
    inputs:
      targetType: inline
      script: |
        YesterdayDate=$(date -d "yesterday" '+%d%m%Y')
        echo "##vso[task.setvariable variable=Yesterday;isoutput=true]$YesterdayDate"        

- job: B
  displayName: 'Job B'
  dependsOn: A
  variables:
    Yesterday: $[ dependencies.A.outputs['get_yesterday.Yesterday'] ]
  steps:
  - task: Bash@3
    displayName: 'Print Date of Yesterday'
    inputs:
      targetType: inline
      script: |
        echo "Print Date of Yesterday:"
        echo "Yesterday = $(Yesterday)"        
  • Result
    如何在编译时获取昨天的日期?

有关更多信息,您可以参考以下文档:

英文:

Currently, there is not built-in variable or expression on Azure DevOps can get the date of yesterday.

As a workaround, you can try to add a job (or stage) as the very first one in your YAML pipeline to get the date of yesterday using related date command and set the date as an output variable for use in the subsequent jobs (or stages).

Below is an example as reference:

  • YAML
jobs:
- job: A
  displayName: 'Job A'
  steps:
  - task: Bash@3
    name: get_yesterday
    displayName: 'Get Date of Yesterday'
    inputs:
      targetType: inline
      script: |
        YesterdayDate=$(date -d "yesterday" '+%d%m%Y')
        echo "##vso[task.setvariable variable=Yesterday;isoutput=true]$YesterdayDate"        

- job: B
  displayName: 'Job B'
  dependsOn: A
  variables:
    Yesterday: $[ dependencies.A.outputs['get_yesterday.Yesterday'] ]
  steps:
  - task: Bash@3
    displayName: 'Print Date of Yesterday'
    inputs:
      targetType: inline
      script: |
        echo "Print Date of Yesterday:"
        echo "Yesterday = $(Yesterday)"        
  • Result
    如何在编译时获取昨天的日期?

For more information, you can reference the following documents:

huangapple
  • 本文由 发表于 2023年1月9日 14:54:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75053979.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定