3

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

0

4 Answers 4

7

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 :)

Sign up to request clarification or add additional context in comments.

Comments

1
  1. You don't need isOutput=true - that's only needed for referencing variables between different jobs or stages.
  2. "You cannot use the variable in the step that it is defined." - split that script into two steps: one that runs your .py file, second one that uses the newly defined variable.

2 Comments

I've updated my python code to print(f'##vso[task.setvariable variable=AMLPipelineId]{pipelineId}') and i moved into a separate step: $MLPipelineId = $AMLPipelineId and its still empty.
Try using $(AmlPipeline) (syntax for referencing pipeline variables) or $env:AmlPipeline (pipeline variables are also automatically mapped to env variables).
0

I used print('##vso[task.setvariable variable=<Variable-in-Pipeline]+<output-variable>')

Variable-in-Pipeline // the given name should be used in Azure Devops pipeline and should be added to pipeline variables as an empty string

Comments

0

I was trying to pass variables between tasks on Azure on a release pipeline's agent job and the following worked for me:

print('##vso[task.setvariable variable=<PipelineVariable>;isOutput=true;]'+<OutputVariable>)

Then when you want to access the variable in subsequent steps use:

$(<PipelineVariable>.<OutputVariable>)

And specify your <PipelineVariable> on your Python task as follows: enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.