英文:
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)"
有关更多信息,您可以参考以下文档:
英文:
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)"
For more information, you can reference the following documents:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论