简体   繁体   English

如何从 azure 管道获取存储在 python 脚本中的变量的值

[英]how to get the value of a variable stored in a python script from azure pipeline

Currently, I am faced with the need to run a pipeline on an azure VM, I am using a python script to run a process in which I store the ID in a variable, and I need to use that variable value in the code yaml from the pipeline to use as a parameter, does anyone know how I can proceed?目前,我需要在 azure VM 上运行管道,我正在使用 python 脚本来运行一个进程,其中我将 ID 存储在一个变量中,我需要在代码 Z6EEDC03A678A69933C2FZD76 中使用该变量值用作参数的管道,有人知道我该怎么做吗?

In your Python script, you can set an output variable with the value you want to pass so that it can be access in the subsequent steps in the pipeline.在 Python 脚本中,您可以使用要传递的值设置 output 变量,以便可以在管道中的后续步骤中访问它。

  1. In the Python script, use the logging command ' SetVariable ' to set an output variable with the value you want.在 Python 脚本中,使用日志记录命令“ SetVariable ”将 output 变量设置为所需的值。 And use the Python Script task to run the script.并使用Python 脚本任务运行脚本。

  2. In the subsequent steps of the same pipeline job, you can use the expression ' $({taskName}.{variableName}) ' to access the output variable.在同一管道作业的后续步骤中,您可以使用表达式“ $({taskName}.{variableName}) ”来访问 output 变量。

Below is an example YAML pipeline as reference.下面是一个示例 YAML 管道作为参考。

jobs:
- job: job1
  displayName: 'Job 1'
  pool:
    vmImage: ubuntu-latest
  steps:
  - task: PythonScript@0
    name: pythScr
    displayName: 'Set output variable'
    inputs:
      scriptSource: inline
      script: |
        import subprocess
        myID = 123
        subprocess.Popen(["echo", "##vso[task.setvariable variable=MY_ID;isoutput=true]{0}".format(myID)])
  
  - task: Bash@3
    displayName: 'Print output variable'
    inputs:
      targetType: inline
      script: echo "MY_ID = $(pythScr.MY_ID)"

Result.结果。 在此处输入图像描述

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

相关问题 如何从python脚本中的ini文件读取和获取存储值? - How to read and get the stored value from ini file in python script? 从python脚本中获取变量的值 - get value of a variable from a python script Azure Devops Pipeline 中的 Python 脚本不能使用库中的值,如果它是一个秘密 - Python script in Azure Devops Pipeline cannot use a value from library if it's a secret 如何获取存储在变量中的对象 ID 的值 - 在 python 中 - How to get the value of an object ID stored in a variable - in python 如何从 python 脚本在 azure devops 管道中生成合理的错误消息 - How to generate sensible error messages in azure devops pipeline from a python script 使用来自 Azure 管道的服务连接对 Python 脚本任务进行身份验证 - Authenticate Python script task using service connection from Azure pipeline 如何将变量值从 Python 脚本传输到 Bash 脚本 - How to transfer variable value from Python script to a Bash script 如何从bash脚本获取python脚本中带有目录路径的变量? - How to get variable with directory path in python script from bash script? 如何从python脚本更改python文件中的变量值 - how to change a variable value in a python file from a python script 从 Python 脚本到管道的 Gitlab-CI 环境变量 - Gitlab-CI environment variable from Python script to pipeline
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM