4

我在 Azure Devops 上有一个管道,我正在尝试使用 REST api 以编程方式/无头运行:https ://docs.microsoft.com/en-us/rest/api/azure/devops/pipelines/runs/run% 20pipeline?view=azure-devops-rest-6.0

到目前为止一切顺利,我可以进行身份​​验证并开始运行。我想将数据传递到文档建议可以variables在请求正文中使用的此管道。我的请求正文:

{
    "variables": {
        "HELLO_WORLD": {
            "isSecret": false,
            "value": "HelloWorldValue"
        }
    }
}

我的管道 YAML 如下所示:

trigger: none

pr: none

pool:
  vmImage: 'ubuntu-latest'

steps:
- task: Bash@3
  inputs:
    targetType: 'inline'
    script: |
      KEY=$(HELLO_WORLD)
      echo "Hello world key: " $KEY

然而,这给了我一个“HELLO_WORLD:找不到命令”的错误。

我尝试将“HELLO_WORLD”变量添加到管道并启用“让用户在运行此管道时覆盖此值”设置。这导致HELLO_WORLD变量不再是未知的,而是在我使用 REST api 触发运行时卡在其初始值上并且未设置

如何使用 REST api 将变量传递给管道?仅针对特定运行/构建设置变量值很重要

我找到了另一个 API 来运行构建,但似乎你不能使用个人访问令牌身份验证,就像你可以使用管道 api - 只有 OAuth2 - https://docs.microsoft.com/en-us/rest/ api/azure/devops/build/builds/queue?view=azure-devops-rest-6.0

4

2 回答 2

9

You can do it with both the Runs API and Build Queue API, both work with Personal Access Tokens. For which one is the better/preferred, see this question: Difference between Azure Devops Builds - Queue vs run pipeline REST APIs, but in short the Runs API will be the more future proof option

Option 1: Runs API

POST https://dev.azure.com/{{organization}}/{{project}}/_apis/pipelines/{{PipelineId}}/runs?api-version=6.0-preview.1

Your body will be of type application/json (HTTP header Content-Type is set to application/json) and similar to the below, just replace resources.repositories.self.refName with the appropriate value

{
    "resources": {
        "repositories": {
            "self": {
                "refName": "refs/heads/main"
            }
        }
    },
    "variables": {
        "HELLO_WORLD": {
            "isSecret": false,
            "value": "HelloWorldValue"
        }
    }
}

Option 2: Build API

POST https://dev.azure.com/{{organization}}/{{project}}/_apis/build/builds?api-version=6.0

Your body will be of type application/json (HTTP header Content-Type is set to application/json), something similar to below, just replace definition.id and sourcebranch with appropriate values. Please also note the "stringified" content of the parameter section (it should be a string representation of a json map)

{
    "parameters": "{\"HELLO_WORLD\":\"HelloWorldValue\"}",
    "definition": {
        "id": 1
    },
    "sourceBranch": "refs/heads/main"
}
于 2021-02-13T16:21:45.510 回答
2

这是我解决它的方法......

REST 调用:

POST https://dev.azure.com/<myOrg>/<myProject>/_apis/pipelines/17/runs?api-version=6.0-preview.1

  请求正文:

{
    "resources": {
        "repositories": {
            "self": {
                "refName": "refs/heads/main"
            }
        }
    },
    "templateParameters": {
        "A_Parameter": "And now for something completely different."
    }
}

注意:我添加了一个带有基本身份验证的授权标头,其中包含用户名(任何名称都可以)和密码(您的 PAT 令牌值)。还添加了 Content-Type application/json 标头。


  这是我使用的整个 yaml 管道:  

parameters:
- name: A_Parameter
  displayName: A parameter
  default: noValue
  type: string
 
trigger:
- none
 
pool:
  vmImage: ubuntu-latest
 
steps:
 
- script: |
    echo '1 - using dollar sign parens, p dot A_Parameter is now: ' $(parameters.A_Parameter)
    echo '2 - using dollar sign double curly braces, p dot A_Parameter is now::' ${{ parameters.A_Parameter }} '::'
    echo '3 - using dollar sign and only the var name: ' $(A_Parameter)
  displayName: 'Run a multi-line script'

    这是管道日志的输出。请注意,只有第二种方式正确显示了该值。    

1 - using dollar sign parens, p dot A_Parameter is now: 
2 - using dollar sign double curly braces, p dot A_Parameter is now:: And now for something completely different. :: 
3 - using dollar sign and only the var name:
于 2021-06-23T20:06:17.467 回答