Search code examples
azureazure-devopsyamldevops

How to read Python file parameters into Azure DevOps


I am building Docker images in my Azure DevOps pipeline. I tag the image with the version of the image. The version is stored in prodInfo.py and is of the following structure:

title = "Product A"
description = "Description"
version = "0.1.0.0"

I was wondering how I can load the version info into Azure DevOps so I can provide it to the tag field within my .yaml pipeline.


Solution

  • In the same job, before the Docker build task, you can add a Bash task to read the prodInfo.py file and extract the version number from the line. And then use the logging command "SetVariable" to set the extracted version number as a pipeline variable for use on the Docker build task.

    Below is a sample as reference.

    stages:
    - stage: A
      displayName: 'Stage A'
      jobs:
      - job: A1
        displayName: 'Job A1'
        steps:
        - task: Bash@3
          displayName: 'Get tag version'
          inputs:
            targetType: inline
            script: |
              lineStr=$(sed -n -e '/^version = "/p' relative/path/to/prodInfo.py)
              echo $lineStr
              tagVer=$(echo $lineStr | cut -d'"' -f 2)
              echo $tagVer
              echo "##vso[task.setvariable variable=tagVersion;]$tagVer"
        
        - task: Bash@3
          displayName: 'Print tagVersion'
          inputs:
            targetType: inline
            script: echo "tagVersion = $(tagVersion)"
    
    # On the subsequent Docker build task, 
    # you can pass the pipeline varibale $(tagVersion) to the Tags field.
    

    Result: enter image description here