【问题标题】:How to run Python task with build variables in Azure Devops如何在 Azure Devops 中使用构建变量运行 Python 任务
【发布时间】:2021-07-16 16:34:18
【问题描述】:

我正在使用以下在本地测试的脚本,但我不知道如何获取构建变量并将其插入到 Azure Devops yml 中 Python 代码中的请求中。

我定义了以下构建变量: DATABRICKS_HOST、DATABRICKS_TOKEN、集群

- task: PythonScript@0
  displayName: 'Start Databricks Cluster'
  inputs:
    scriptSource: inline
    script: |
      import os
      import requests
      DOMAIN = os.environ.get('DATABRICKS_HOST')
      TOKEN = os.environ.get('DATABRICKS_TOKEN')
      CLUSTER_ID = os.environ.get('CLUSTER')
      response = requests.post('https://%s/api/2.0/clusters/start' % (DOMAIN),headers={'Authorization': 'Bearer %s' % TOKEN},json={ "cluster_id": CLUSTER_ID  }  )
      if response.status_code != 200:
        print("Error launching cluster")

【问题讨论】:

    标签: python-3.x azure-devops yaml


    【解决方案1】:

    如果你use pipeline variable,试试下面的yaml。

    variables:
      DATABRICKS_HOST: "test"
    
    pool:
      vmImage: ubuntu-latest
    
    steps:
    - task: PythonScript@0
      inputs:
        scriptSource: 'inline'
        script: |
          host = "$(DATABRICKS_HOST)"
          print (host)
    

    如果你set variables in scripts,请检查以下yaml。

    steps:
    - bash: |
        echo "##vso[task.setvariable variable=DATABRICKS_TOKEN]crushed tomatoes"
    - task: PythonScript@0
      inputs:
        scriptSource: 'inline'
        script: |
          token = "$(DATABRICKS_TOKEN)"
          print (token)
    

    请注意Microsoft-hosted Ubuntu latest agents使用python3语法。

    【讨论】:

    • 嗨,ibexy,我的回答有效吗?请检查并请告诉我结果。
    猜你喜欢
    • 2020-06-21
    • 1970-01-01
    • 1970-01-01
    • 2019-04-14
    • 2021-11-04
    • 1970-01-01
    • 2020-02-05
    • 2021-11-04
    • 1970-01-01
    相关资源
    最近更新 更多