Skip to main content

Azure DevOps Yaml FileTransform

Full stack developer, working on C#, DotNet core, Azure, Azure DevOps and CI/CD Pipelines.

azure-devops-yaml-filetransform

Let’s see how we can use File Transform task in Azure DevOps CI Pipeline to replace contents in a JSON file.

Prerequisites –

  1. Basic knowledge of how to create a repository on Azure DevOps.
  2. Basic knowledge of how to create a CI pipeline on Azure DevOps.

YouTube Tutorial link for step-by-step implementation —

https://youtu.be/dpVdrp6Orm4

What is File Transform Task in Azure DevOps Pipelines?

File Transform Documentation — File Transform task — Azure Pipelines | Microsoft Docs

File Transform task can be used in Azure Pipelines to apply file transformations and replace tokens with values in XML or JSON files.

Search for File Transform task in Azure Pipelines, you’ll see below highlighted task –

File Transform Task in Azure DevOps

File Transform Task in Azure DevOps

Replacing a value in appsettings.json –

  1. Below is the Git Repository that I have on Azure DevOps, and the current appsettings.json values are highlighted
Azure Git Repository

Azure Git Repository

2. We will update the value of “EMail” key in the above appsettings.json with another value — testaz@testdomain.com

3. First, create a new Variable Group, click on Pipeline -

Variable Group in Azure

Variable Group in Azure

a. On next screen assign value to Variable Group = JsonVariableGrp, and click on +Add button to add a variable named Email and value = testaz@testdomain.com, and hit Save -


Create Variable

Create Variable

4. Create a new CI pipeline –

Scroll to Continue

a. Go to Azure DevOps portal, and click on Pipeline on left pane, then on right top corner click on New Pipeline button.

b. Replace the content in the yaml file with below content, I will explain the steps later (see point 5 below).

c. Below is the Azure build pipeline with FileTransform task. You can also go to this github link to see the yaml pipeline - https://gist.github.com/jitendra-25/45fc49ba72586b03913740ee3ef4cdcb



# ASP.NET Core # Build and test ASP.NET Core projects targeting .NET Core. # Add steps that run tests, create a NuGet package, deploy, and more: # https://docs.microsoft.com/azure/devops/pipelines/languages/dotnet-core trigger: - main pool: vmImage: windows-2019 variables: - group: JsonVariableGrp - name: buildConfiguration value: 'Release' - name: MailSettings.EMail value: '$(EMail)' steps: - task: PowerShell@2 inputs: targetType: 'inline' script: | echo "Print EMail value" echo "Value: $(EMail)" - task: FileTransform@1 displayName: "Running File Transform Task" inputs: folderPath: 'DemoCoreApp/**/' fileType: 'json' targetFiles: '*.json' - script: dotnet build --configuration $(buildConfiguration) displayName: 'dotnet build $(buildConfiguration)' - task: DotNetCoreCLI@2 displayName: 'Running Publish Artifacts' inputs: command: 'publish' publishWebProjects: true arguments: '--configuration $(BuildConfiguration) --output $(Build.ArtifactStagingDirectory)' zipAfterPublish: true - task: PublishPipelineArtifact@1 displayName: 'Uploading Artifact to Dir' inputs: targetPath: '$(Build.ArtifactStagingDirectory)' artifact: 'artifactDrop' publishLocation: 'pipeline'

5. Pipeline sections explained —

a. Variable block (Line 12–17)
Here, we have added the Library Variable group into Pipeline, we have to specify the group name.

Then, we have added the variable with name “MailSettings.EMail” and value is “$(EMail)”. This will read the value of EMail variable from Library.

Since, the EMail key was defined under MailSettings key in appsettings.json, we have to use dot notation here to point to EMail key.


b. PowerShell Task (Line 20–25) —

This is a PowerShell task just to print the value of the Library Variable EMail.

c. File Transform Task (Line 27-32) - It is the File Transform task.

folderPath - specifies the solution name where the json file is.
fileType - specifies the type of file used for File Transform.
targetFiles – Here, we specify the wildcard *.json, so, the File Transform will look for json files in our solution to do the replacement.

d. DotNet Core Task (Line 37-43) -

This is a DotNetCore Build and Publish task, which will build and publish our solution into a zip file.

e. Publish Artifacts Task (Line 45-50) -

This task will publish the above created artifact i.e. zip file into Pipeline, which we can download on local system and extract to see the appsettings.json file


6. Click Save, it will ask to commit the pipeline. Go ahead and commit the yml file to current Repository.

7. The pipeline will not run yet, as we have to provide permission to the pipeline to read the value from Library Variable Group. Below are the steps to do this —

a. Go to Pipelines -> Library, and open the Variable Group that was created earlier by clicking on its name.

b. Click on Pipeline permissions on top, it will open a pop-up. Then click on “+” button to select the pipeline, like below

Pipeline Permission

Pipeline Permission

8. Now, run the pipeline, after completion you should see be able to see the generated artifact.

9. Click on Job on left side and then on “1 artifact” link to download the artifact.


Artifact generated by Pipeline

Artifact generated by Pipeline

10. After extracting the zip artifact, you can see the appsettings.json file, it should have the value which was provided to the Pipeline Library variable EMail, like below -

Updated value in appsettings.json

Updated value in appsettings.json

Thanks for Reading!!

Connect with me —

LinkedIn — https://www.linkedin.com/in/jitendra-p/
Youtube — https://www.youtube.com/channel/UCwJoL7MMlwQIz0A2UPFyn7w
Instagram — https://www.instagram.com/itsjitendrap/

Please support me —

https://www.buymeacoffee.com/jitpanchal

© 2022 Jitendra

Related Articles