How to create a file in during Azure Pipeline that includes build.* variables

Viewed 3178

I need to create the following json structure and include it with my .NET application using Azure Pipelines and it is not clear to me how to do so.

{
"buildNumber":"$(Build.BuildNumber)",
"buildId":"$(Build.BuildId)",
"branchName":"$(Build.SourceBranchName)",
"commitHash":"$(Build.SourceVersion)"
}

I've tried simply doing a command line to echo the information into a file but I'm not sure of the path necessary to have it picked up by the publish step.

Right now I am experimenting with a powershell task:

    - task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      # Write your PowerShell commands here.

      new-item -itemtype file -path $(Build.SourcesDirectory)/MyFirstExperiment -name "buildinfo.json" -force -value '{
      \"buildNumber\":\"$(Build.BuildNumber)\",
      \"buildId\":\"$(Build.BuildId)\",
      \"branchName\":\"$(Build.SourceBranchName)\",
      \"commitHash\":\"$(Build.SourceVersion)\"
      }'

I see output during the task that the file is created but I must still be missing something obvious because it is not showing up in the build / publish later in the pipeline.

What should I be checking?

I strongly suspect that I am missing something very obvious because there is not a lot of results doing google searches for 'azure pipeline create file'

Thank you in advance!

1 Answers

I see you're using $(Build.SourcesDirectory), which is the location where your source code files are downloaded. Use $(Build.StagingDirectory) instead, this is where any artifacts are copied to before being pushed to their destination.

Reference: Build variables (DevOps Services)

If permitted by your org, you can also consider using an extension from the Visual Studio Marketplace, like File Creator. This is a task that can be added as a step to your pipeline to create a file during a build or release process.

Related