0

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

  • 1
    Your script is missing `import os`? – James Z Jul 10 '21 at 18:06
  • This has more to do with python than pipelines. See [How to access environment variable values?](https://stackoverflow.com/questions/4906977/how-to-access-environment-variable-values). – qbik Jul 11 '21 at 05:17
  • Does this answer your question? [How to set environment variables in Python?](https://stackoverflow.com/questions/5971312/how-to-set-environment-variables-in-python) – qbik Jul 11 '21 at 05:17
  • @swetha-setty were you able to resolve this secrets usage in python script from devops env injection? – polavishnu Sep 27 '22 at 19:14

0 Answers0