[英]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.