Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Pipelines - Output variable from python script file to pipeline variable

I've tried several articles and threads from Stackoverflow but can't seem to get anywhere. I am trying to take a variable from a .py file which is called in a YAML step and set that variable globally to be used.

In my .py file i have

print(f'##vso[task.setvariable variable=AMLPipelineId;isOutput=true]{pipelineId}')

Then in my YAML pipeline step i have

- task: AzurePowerShell@5
  displayName: 'Run AML Pipeline'
   inputs:
     azureSubscription: '$(azureSubscription)'
     ScriptType: 'InlineScript'
     name: AmlPipeline
     azurePowerShellVersion: 'LatestVersion'
     pwsh: true
     Inline: |
             $username = "$(ARM_CLIENT_ID)"
             $password = "$(ARM_CLIENT_SECRET)"
             $tenantId = "$(ARM_TENANT_ID)"
                  
             python $(Pipeline.Workspace)/AML_Pipeline/build_aml_pipeline.py --wsName $(wsName) --resourceGroup $(ResourceGroupName) --subscriptionId $(subId)

                 
             $MLPipelineId = $AmlPipeline.AMLPipelineId

But it seems like this variable is empty. I know there are other ways of using the "set variable" but this is my latest attempt i.e. something like print('##vso[task.setvariable variable=version;]%s' % (version))

My current approach i followed: https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch

like image 494
ScuffedCoder Avatar asked Mar 10 '26 10:03

ScuffedCoder


1 Answers

A very minimal example for everyone struggling with this. The documentation is kind of lacking on this for my taste. As @qbik said, dont set and use the variable in the same step, make it seperate steps.

set_variable.py

if __name__ == '__main__':
    # set name of the variable
    name = 'COLOR'
    # set value of the variable
    value = 'red'
    # set variable
    print(f'##vso[task.setvariable variable={name};]{value}')

azure-pipelines.yml

trigger:
- main

pool:
  vmImage: ubuntu-latest

steps:
- task: UsePythonVersion@0
  inputs:
    versionSpec: '3.9'
  displayName: 'Use Python 3.9'
# run the script to set the variable
- task: PythonScript@0
  inputs:
    scriptSource: filePath
    scriptPath: set_variable.py
# now you can use the variable in the next step
- bash: echo my favorite color is: $(COLOR)

Now you can technically do all kinds of cool stuff in python, then set, and reference the variables in the following steps. In my case I have to extract specific package version numbers from a JSON/YAML file based on an id that is set earlier in the pipeline and parse the information as an args for a docker build. Hope that helps other people stumbling across this answer looking for a minimal working example :)

like image 80
code4meplz Avatar answered Mar 12 '26 06:03

code4meplz