【问题标题】:Azure devops pass build parameters through Rest APIAzure devops 通过 Rest API 传递构建参数
【发布时间】:2021-09-02 15:34:32
【问题描述】:

我正在尝试使用 jira 通过 build rest API 传递参数,但它不会覆盖该参数。

管道:

parameters:
- name: "Testplan"
  type: string
  default: "NoPlanDefined"

stage: Test
  jobs:
  - job: Testing_And_Transfer
    - task: PowerShell@2
        displayName: "Testing API Call"
        inputs:
          targetType: 'filepath'
          filePath: './script/Jira.ps1'
          arguments: 
            -Jira_id ${{ parameters.Testplan }}

Jira.ps1 内容:

Param(
    [string]$Jira_id = "no ID"
)
#-----------------------Jira API--------------------------
echo 'This is a test \n ID: '
echo $Jira_id

我的休息命令是这样设置的:

网址:https://dev.azure.com/{Mycorp}/MyHello/_apis/build/builds?api-version=6.0

主体:

{    
    "definition":  { 
        "id": 1 
        },
    "parameters": "{ \"Testplan\":\"Postman\" }"
}
  • 使用触发器时,ps1 按预期返回 NoPlanDefined
  • 使用手动触发并更改参数时,参数 按预期进行更改。
  • 尝试通过以下方式更改参数时 Rest api,Testplan 是空的,而不是 Postman

我的 REST API 有什么问题吗?

【问题讨论】:

    标签: rest azure-devops azure-pipelines


    【解决方案1】:

    那是因为那些是不是参数,尽管在 REST 调用中使用了名称。它们是运行时变量,它们的行为不同,并且在与参数不同的范围内可用。

    有一个不同的 API 允许您指定 templateParametershttps://docs.microsoft.com/en-us/rest/api/azure/devops/pipelines/runs/run-pipeline?view=azure-devops-rest-6.1

    【讨论】:

      【解决方案2】:

      如果您熟悉 PowerShell,则可以在调用管道时使用 AzurePipelinesPS 模块和以下命令将参数传递给管道。

      $id = '2' # your pipeline id
      $templateParameters = @{
          Testplan = 'myTestPlan' # your parameter and value
      }
      Invoke-APPipeline -Instance https://dev.azure.com -Collection 'yourCollectionName' -Project 'yourProjectName' -ApiVersion '6.0-preview.1' -PipelineId $id -TemplateParameters $templateParameters
      

      该模块支持“会话”以限制所需参数的数量。有关如何创建会话,请参阅模块的 github page

      【讨论】:

        猜你喜欢
        • 2016-03-24
        • 2019-08-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-18
        相关资源
        最近更新 更多