'Capturing build date in deployment pipleine

I'm trying to capture the build date of an artifact to be logged elsewhere. Join me on my convoluted journey to solve this.

I know we have these handy variables

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

EDIT: These are not as handy as I thought. These are just the identifers for the deploy pipeline, not the original build pipeline that generated the artefact. So I can repeatedly deploy the same build / artefact, and these numbers will continue to increment, having no relevance to what I built - I'm not interested in that.

But there is no build date. I know it can be derived from the BuildNumber but it seems over the top to call a REST API to get that info.

So in my build pipeline I am writing Get-Date to a file then publishing that as an artefact

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

Then I pick that up in the deploy pipeline and save to a variable using the kludgy Write-Host method

  - 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"  

This is done in stage DownloadDBArtifacts

Now I need to use it in a later stage, that is also in a child YAML template

I beleive this is the syntax for extracting the variable:

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

I'm having difficulty getting this recognised in later stages. Here is a subsequent stage that tries to capture the value based on examples from here:

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)"

however the value is not being passed through as the final powershell step just produces this output:

var:



Solution 1:[1]

Capturing build date in deployment pipleine

I could reproduce this issue with your YAML sample.

To resolve this issue, please update your DownloadDBArtifacts by following code:

  - 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

The test result:

enter image description here

Update:

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

You have a slight letter error in your code that is causing the issue.

One is name: GetBuiltDt and another is outputs['GetBuildDt.BuildDt'] ]. The Built should be Build?

enter image description here

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1