Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy and Rename Config File in Azure Pipelines

I have specific config files for specific environments (dev,qa,uat) and another root web.config. The deployed code reads web.config. So I've been trying to either copy the contents or rename the file in Azure Pipelines.

- task: CopyFiles@2
      displayName: 'Copy Specific Config'
      inputs:
        SourceFolder: 'Client.WebApi.Core/Configs/Web.QA.config'
        TargetFolder: '$(build.artifactstagingdirectory)\MainWebApi'

- task: CmdLine@2
      inputs:
        script: rename $(build.artifactstagingdirectory)\MainWebApi\Web.QA.config 
        $(build.artifactstagingdirectory)\MainWebApi\web.config

Another method:

- task: PowerShell@2
      inputs:
        targetType: 'inline'
        script: |
          copy-item -path Client.WebApi.Core/Configs/Web.QA.Config -destination Client.WebApi.Core/web.config
          TargetFolder: '$(build.artifactstagingdirectory)/MainWebApi'

I have tried various combinations like renaming then copying and copying and then renaming it, copying the contents into new web.config files etc What exactly should be used?

like image 485
Joyti Avatar asked Sep 09 '20 08:09

Joyti


2 Answers

Did you try

- task: CmdLine@2
  inputs:
    script: ren $(build.artifactstagingdirectory)\MainWebApi\Web.QA.config 
    $(build.artifactstagingdirectory)\MainWebApi\web.config

You can follow these documents:

  • https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/command-line?view=azure-devops&tabs=yaml#where-can-i-learn-windows-commands
  • https://ss64.com/nt/ren.html
like image 139
trytobeanengineer Avatar answered Sep 18 '22 15:09

trytobeanengineer


What exactly should be used?

You can use the built-in task called File transform to transform web.< environment >.config to web.config. And then, you can use "Copy files" task to copy web.config to $(build.artifactstagingdirectory).

Here are detailed steps:

Step1. You need to add a transform file, which is in the same path as the source file.

If you don't want to make any changes to result file, just create a xml file with one line of scirpt:

<?xml version="1.0"?>

If you want to make some changes, write the changes in the transform file. Here is an example.

Step2. In your pipeline, search and add a "File Transform" task.

enter image description here

You need to tick the "XML transformation" and in "Transformation rules", write foolowing script:

-transform {transform file} -xml {source file} -result Web.config

Step3. Search and add a "Copy Files" task. Copy Web.config file to $(build.artifactstagingdirectory).

Here is the yaml script for step2 and step3.

steps:
- task: FileTransform@1
  displayName: 'File Transform: '
  inputs:
    folderPath: '{folder path}'
    enableXmlTransform: true
    xmlTransformationRules: '-transform {transform file} -xml {source file} -result Web.config'
    fileType: xml
- task: CopyFiles@2
  displayName: 'Copy Files to: $(build.artifactstagingdirectory)'
  inputs:
    SourceFolder: '{folder path}'
    Contents: Web.config
    TargetFolder: '$(build.artifactstagingdirectory)\MainWebApi'
like image 30
Jane Ma-MSFT Avatar answered Sep 18 '22 15:09

Jane Ma-MSFT