3

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!

JJS
  • 6,431
  • 1
  • 54
  • 70
Chris Keller
  • 235
  • 3
  • 9
  • To get the **short git commit hash** of `$(Build.SourceVersion)` look to [this post](https://stackoverflow.com/a/62469078/789423). It saves the short hash (as shown in the commit history) to variable `$(short_hash)`, which you can include to your file. – Beauty Jun 11 '21 at 06:44

1 Answers1

3

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.

Bhargavi Annadevara
  • 4,923
  • 2
  • 13
  • 30
  • I fixed that (THANK YOU!) and now I have the file showing up at the end along with the zip of the publishing output. Is there any way that I can get the file into the zip? I had thought that if I added it before the build step it would get copied but that does not seem to be the case. – Chris Keller Feb 10 '21 at 16:10
  • Happy to help! Indeed, you could [copy the file](https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/copy-files?view=azure-devops&tabs=yaml) to the destination and then [publish](https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/publish-build-artifacts?view=azure-devops) it as part of the build artifacts. – Bhargavi Annadevara Feb 10 '21 at 16:50
  • MSBuild is a complicated beast. If you could generate the file as part of the build process, you could then add it to your outputs (either by creating an item ``, or even using ``. Look through Microsoft.Common.CurrentVersion.targets. There's a LOT to be learned! – fourpastmidnight Jan 14 '22 at 17:18