Search code examples
azure-devopspowerbicontinuous-integrationazure-pipelinescicd

How to rename multiple PowerBi reports in Dev, QA, PROD during CICD using Azure DevOps Yaml Pipeline?


In my repository, I have 2 PowerBi reports (.pbix files) in a folder:

  • Report1.pbix
  • Report2.pbix

In my Yaml pipeline, I am creating an artifact named "reports" and coping all the reports there in the CI step.

Then in the CD steps (Dev, Qa, and Prod), I am deploying them using PowerBIActions@5 task. Everything is working as expected. But I want to rename them in each environment.

For Example in DEV:

  • DEV - Report1.pbix
  • DEV - Report2.pbix

For Example in QA:

  • QA - Report1.pbix
  • QA - Report2.pbix

For Example in PROD:

  • PROD - Report1.pbix
  • PROD - Report2.pbix

This should be generic, in the future, I can have more reports. I have used copy files task but it does not have the option to provide the destination filename. There are other options like CmdLine ren, Powershell Rename-Item, and Powershell Copy-Item but they copy or rename a single file at a time. But in my case, there are 2 reports and the number of reports will increase in the future. So I do not want to put multiple Rename-Item tasks for each report. I think a loop or something else is required. Guidance will be appreciated.

Thanks.


Solution

  • The below code is a sample for 'Dev-'. You can capture the current environment and rename all of the '.pbix' files.

    trigger:
    - none
    
    pool:
      vmImage: ubuntu-latest
    
    steps:
    - script: |
        dir
      displayName: 'Run a multi-line script'
    - task: PythonScript@0
      inputs:
        scriptSource: 'inline'
        script: |
          #foreach folder and rename files with specific suffix
          import os
          import re
          
          def rename_files(path,env):
              for file in os.listdir(path):
                  if file.endswith(".pbix"):
                      os.rename(file , env+file)
                      print(file)
                      print(os.path.join(path, file))
                  else:
                      pass
          
          rename_files(".","Dev-")
    - script: |
        dir
      displayName: 'Run a multi-line script'
    

    Successfully on my side:

    enter image description here

    After rename the files, copy them to target place.