Last week I talked about updating a website to .NET 5. In that same application, I had a need to use a value generated at build time in the release so that my appsettings.json file would be updated appropriately. An example of why you may need this is to set the last modified meta tag based on the build:

1
<meta http-equiv="last-modified" content="YYYY-MM-DD" />

Using Azure DevOps makes this task pretty easy. In my particular case, I created a Variable Group as the variables would then be shared across both the build pipeline and release. However, you can simply create an individual pipeline variable like so:

Creating a custom pipeline variable

The value I had set for this variable is $[format('{0:yyyy}-{0:MM}-{0:dd}', pipeline.startTime)]. This will take the pipeline’s start time and format in the way I’d need it to. Remember, this value is generated on the application build. Because I named a property in the root of my appsettings.json as BuildDateTime and added the appsettings.json file to the transform step in release, the proper value will simply be replaced on release. I can see the complete list of variables in the release logs including my new variable and value:

Viewing the variables available during release

If you’d like to learn more about pipeline expressions, visit docs.microsoft.com/en-us/azure/devops/pipelines/process/expressions.