How to access secret variable within the python script

1.3k Views Asked by At

I have a simple script to read the secret variable set in the azure-pipeline. I am able to print from "script" step directly, but not able to resolve in the python script. When I print inline, it works. This seems like a very basic thing, but I have checked quite a few blogs and options. This blog seemed to talk about the issue I am facing: https://brandonsnotepad.wordpress.com/2021/04/23/azure-devops-secret-variables-python/ But didn't help :( I haven't got it working so far.

Working:

steps:
- script: echo $TOKEN
  displayName: 'Run a one-line script'
  env:
    TOKEN: $(TOKEN)

Output: xxx

Above output is understandable.

I want to have this token within my script for further processing

- task: PythonScript@0
  inputs:
    scriptSource: 'filePath'
    scriptPath: './test.py'
    failOnStderr: true
  env:
   TOKEN: $(TOKEN)
  displayName: 'Get Token'

test.py I tried below options: 1.

token = os.getenv("TOKEN")

In this case, I got this error. I am using 3.7.10 python version

NameError: name 'os' is not defined

So I tried to do pip install os, but ended with this error. My understanding is os is a default package that should come with python. But still tried based on the above error.

ERROR: Could not find a version that satisfies the requirement os (from versions: none)
ERROR: No matching distribution found for os
  1. Then I tried to refer the way I did in one-line script.
token = $(TOKEN)

Error: SyntaxError: invalid syntax

  1. Then assuming, since it is a variable and passed from the script, I tried this as well
token = TOKEN

Error: NameError: name 'TOKEN' is not defined

0

There are 0 best solutions below