在部署管道中捕获构建日期

Capturing build date in deployment pipleine

我正在尝试捕获要在别处记录的工件的构建日期。加入我解决这个问题的复杂旅程。

我知道我们有这些方便的变量

$(Build.SourceVersion)
$(Build.BuildNumber)

编辑:这些并不像我想象的那么方便。这些只是部署管道的标识符,而不是生成工件的原始构建管道。所以我可以重复部署相同的构建/工件,并且这些数字将继续增加,与我构建的内容无关 - 我对此不感兴趣。

但是没有构建日期。我知道它可以从 BuildNumber 派生,但调用 REST API 来获取该信息似乎有点过头了。

所以在我的构建管道中,我将 Get-Date 写入一个文件,然后将其发布为人工制品

- powershell: (Get-Date).ToString("yyyy-MM-dd HH:mm:ss") | Out-File -FilePath $(Build.ArtifactStagingDirectory)\BuildDt.txt

然后我在部署管道中选择它并使用笨拙的 Write-Host 方法保存到一个变量

  - stage: DownloadDBArtifacts
    displayName: Download DB Artifacts
    dependsOn: []
    jobs:
    - job: GetArtefacts
      displayName: Get Artefacts
      steps:    
      - download: DBBuild
      - task: PowerShell@2
        displayName: Get Build timestamp
        name: GetBuildDt
        inputs:
          targetType: inline
          script: |
            $BuildDt = Get-Content -Path $(Pipeline.Workspace)\DBBuild\drop\BuildDt.txt
            Write-Host "##vso[task.setvariable variable=BuildDt;isoutput=true]$BuildDt"
            Write-Host "##[debug]Artifact Creation Date: $BuildDt"  

这是在 DownloadDBArtifacts

阶段完成的

现在我需要在后期使用它,它也在子 YAML 模板中

我相信这是提取变量的语法:

stageDependencies.DownloadDBArtifacts.GetArtefacts.outputs['GetBuildDt.BuildDt']

我很难在后期阶段识别它。这是一个后续阶段,它试图根据此处的示例捕获值:

https://docs.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#use-outputs-in-a-different-stage

https://docs.microsoft.com/en-us/azure/devops/pipelines/process/expressions?view=azure-devops#job-to-job-dependencies-across-stages

  - stage: DeployDBtoTST
    displayName: Deploy DB to TST
    dependsOn: DownloadDBArtifacts
    variables: 
    - group: vgTST
    jobs:
    - deployment: DeployDBtoTST
      displayName: Deploy DB to TST
      environment: TST Environment
      variables:
        BuildDt: $[ stageDependencies.DownloadDBArtifacts.GetArtefacts.outputs['GetBuildDt.BuildDt'] ]
      strategy:
        runOnce:
          deploy:
            steps:
            - powershell: |
                Write-Host "var: $(BuildDt)"

然而,该值未被传递,因为最后的 powershell 步骤仅产生此输出:

var:

Capturing build date in deployment pipleine

我可以用你的 YAML 示例重现这个问题。

要解决此问题,请通过以下代码更新您的 DownloadDBArtifacts

  - stage: DownloadDBArtifacts
    displayName: Download DB Artifacts
    dependsOn: []
    jobs:
    - job: GetArtefacts
      displayName: Get Artefacts
      steps:    
      - download: DBBuild
      - task: InlinePowershell@1
        displayName: 'Get Artefacts'
        inputs:
          Script: |
            $BuildDt = Get-Content -Path $(Pipeline.Workspace)\DBBuild\drop\BuildDt.txt
            Write-Host "##vso[task.setvariable variable=BuildDt;isOutput=true]$BuildDt"
        name: GetBuildDt

测试结果:

更新:

Sorry, I tried changing the DownloadDBArtifacts as you mentioned above andit made no difference.

你的代码中有一个小的字母错误导致了这个问题。

一个是 name: GetBuiltDt,另一个是 outputs['GetBuildDt.BuildDt'] ]Built应该是Build