简体   繁体   English

Azure Devops Pipeline 中的 Python 脚本不能使用库中的值,如果它是一个秘密

[英]Python script in Azure Devops Pipeline cannot use a value from library if it's a secret

I have an Azure Devops Pipeline setup.我有一个 Azure Devops Pipeline 设置。 It gets some secrets via the yaml它通过 yaml 获得了一些秘密

variables
 - group: GROUP_WITH_SECRET

Then in the later part of the pipeline I run a python script that gets that particular secret via然后在管道的后半部分,我运行了一个 python 脚本,该脚本通过

my_pat = os.environ["my_secret"]

That is then used in a library provided by Microsoft ( msrest ) as so:然后将其用于 Microsoft ( msrest ) 提供的库中,如下所示:

BasicAuthentication("", my_pat)

If the variable in question, in the ADO Library is set to plain, the script works correctly.如果有问题的变量在 ADO 库中设置为普通,则脚本可以正常工作。 If I change it to a secret, connection fails.如果我将其更改为机密,则连接失败。 If I set it back to plain text, it again works.如果我将它设置回纯文本,它会再次起作用。

Question is, how can I make it work with a secret?问题是,我怎样才能让它与秘密一起工作? I've tried printing the value out but since it's a secret it doesn't show me the actual value other than the The user 'aaaaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaa' is not authorized to access this resource我已经尝试将值打印出来,但由于它是一个秘密,它不会向我显示除The user 'aaaaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaa' is not authorized to access this resource之外的实际值

To use the secret variable in Azure Pipeline, you need to explicitly map secret variables in Agent Job.要在 Azure 管道中使用秘密变量,您需要在代理作业中显式地指定 map 秘密变量。

Based on my test, the Python script task has no environment field to map the secret variables.根据我的测试,Python 脚本任务没有 map 秘密变量的环境字段。

So you can add environment variable in PowerShell task to map secret variables.因此,您可以将 PowerShell 任务中的环境变量添加到 map 秘密变量中。 And you can set it as pipeline variable for the next tasks.您可以将其设置为下一个任务的管道变量。

Here is an example:这是一个例子:

- powershell: |
   echo "##vso[task.setvariable variable=myPass]$env:myPass"
   
  displayName: 'PowerShell Script'
  env:
    myPass: $(myPass)

Then you can use the variable in the next tasks.然后您可以在接下来的任务中使用该变量。

For more detailed info, you can refer to this doc: Secret Variable有关更多详细信息,您可以参考此文档: Secret Variable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Azure Devops Pipeline 中的 Python 脚本执行 - Python script execution in Azure Devops Pipeline 如何从 python 脚本在 azure devops 管道中生成合理的错误消息 - How to generate sensible error messages in azure devops pipeline from a python script 如何从 azure 管道获取存储在 python 脚本中的变量的值 - how to get the value of a variable stored in a python script from azure pipeline 使用 python 从 azure function 触发 Azure Devops 管道 - Trigger an Azure Devops pipeline from an azure function using python Python 将 csv 转换为 yml 以在 azure devops 中使用的脚本 - Python Script to convert a csv to yml to use in azure devops 在 Azure Devops 发布管道中启动 Python 看门狗 - Launching a Python watchdog in an Azure Devops Release pipeline 找不到Azure Devops发布管道Python模块 - Azure Devops Release Pipeline Python Module Not Found 从 Jupyter Notebooks 使用 Azure Devops python 库 - Consuming Azure Devops python library from Jupyter Notebooks 集成了Azure Devops和Python,以便通过自动化脚本传递测试结果并在Azure Devops中进行更新? - Integration of Azure Devops and Python for passing test results from automation script and update in Azure Devops? 在 jenkins 管道上使用来自 github 的 python 脚本 - Use python script from github on jenkins pipeline
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM