【问题标题】:Trigger another build exist in project in Azure Devops触发 Azure Devops 中项目中存在的另一个构建
【发布时间】:2020-11-13 16:49:23
【问题描述】:

我有一个名为 A 的仓库名称,其构建管道为 azure-pipelines.yml 然后我有另一个名为B 的仓库,其构建管道为azure-pipelines.yml

AB都在同一个项目ProjectA

这是流程

  1. 回购A,build => release (stages ops and dev)
  2. 回购B,Build create the Artifact and store the Artifact

所以,我想要实现的是,一旦从 repo A 发布完成,它应该触发 build repo B。 我的管道A 看起来像这样:

name: SomethingFancy

trigger:
  - none

resources:
 containers:
    - container: docker
      image: docker:1.6
    - container: python3
      image: python:3

variables:
  major: 2
  minor: 0

所以我让管道B 看起来像这样:

name: 

trigger:
  - none

resources:
 pipelines:
   - pipeline: SomethingFancy
     source: azure-pipelines
     branch: DATA-1234
     project: ProjectA
     trigger:
      branches:
      - DATA-1234
     stages:
    - dev
    - ops
 containers:
    - container: docker
      image: docker:1.6

到目前为止,我无法运行管道,因为它抱怨“管道资源SomethingFancy Input Must be Valid”。根据文档,它是# identifier for the resource (used in pipeline resource variables)

我指的是 [this][1] 收集资源。

我还打算使用 [api][2] 调用来排队 B 的构建,但无法找到应该是帖子消息的正文,例如如何添加管道B的分支,或者如何将参数传递给B的管道

编辑

见附件我的管道名称 [![在此处输入图像描述][3]][3] 并构建源管道也称为azurepipelines.yml,发布管道有一个阶段称为Dev

现在我的管道 B 如下所示:

resources:
  pipelines:
  - pipeline: azurepipelines
    source: azurepipelines
    branch: DATA-1234
    project: ProjectA
    trigger:
      branches:
      - DATA-1234
      stages:
        - Dev

我仍然没有看到B 的构建管道有任何自动启动。 [1]:https://docs.microsoft.com/en-us/azure/devops/pipelines/process/resources?view=azure-devops&tabs=example#resources-pipelines [2]:https://docs.microsoft.com/en-us/rest/api/azure/devops/build/builds/queue?view=azure-devops-rest-5.1 [3]:https://i.stack.imgur.com/2Uk7A.png

【问题讨论】:

    标签: azure azure-devops


    【解决方案1】:

    经过一番挣扎和这里有识之士的帮助,我终于设法解决了这个问题。我在这里发帖,以便任何人都可以参考。现在正在工作,请参阅: ListBuildQueueTheBuild

    name="ProjectA"
        curl --silent -X GET -H "Authorization:Bearer $(System.AccessToken)" -H "Content-Type:application/json"  $(System.TeamFoundationCollectionUri)/$(System.TeamProject)/_apis/build/definitions?api-version=6.0 --output /tmp/response.json
        #Now get the build-id of your project you are interested in
        #please be aware that api-version > 6 has different json output and below command 
        #may not help you to give the right id
        id=$(cat /tmp/response.json | jq -r --arg key ${name} '.value[] | select(.name==$key)| .id'  --raw-output)
        #create your body to post
        generate_post_data()
        {
          cat <<EOF
        {
          "sourceBranch":"refs/heads/DATA-1234", 
          "definition":{"id": $id}
        }
        EOF
        }
    
    #Now queue your build to run
    #have to still verify if this command works for API_VERSION 6
    
    curl -X POST \
                    --silent \
                    -H "Authorization:Bearer $(System.AccessToken)"  \
                    -H "Content-Type:application/json" \
    $(System.TeamFoundationCollectionUri)$(System.TeamProject)/_apis/build/builds?api-version=6.1-preview.6  \
    --output /tmp/response1.json \
    -d "$(generate_post_data)"
    #check the outcome
    cat /tmp/response1.json
    

    【讨论】:

      【解决方案2】:

      正如我从同一个文档中看到的,我认为您应该将 source 属性设置为来自 repo A 的管道名称。source: SomethingFancy

      【讨论】:

      • 还是不行,A和B到底怎么通信,哪个是repo名字。在两个仓库中,管道具有相同的名称。
      • Aure Web UI 中的管道名称是否也相同?尝试指定您在 UI 中看到的管道的名称。
      【解决方案3】:

      错误消息似乎是在告诉您找不到具有您指定名称的管道,可能是因为,name 表示管道 YAML 中的构建编号格式,例如

      name: $(BuildID)
      

      正如@Roderick 所说,管道的名称应该是您在 UI 中看到的名称。从项目中的主“Azure Pipelines”屏幕。首先单击“三个点”以获取子菜单,然后单击“重命名/移动”。示例截图:

      所以现在您应该拥有更新管道 B 中的 YAML 所需的项目名称和管道名称,并且它应该可以工作。

      【讨论】:

      • 它仍然没有开始......请查看更新后的问题,看看我在你的伟大建议之后做了什么。
      【解决方案4】:

      我注意到回购 A 的流程是 build =&gt; release (stages ops and dev)。我想知道build 是否是构建管道,如azure-pipelines.yml,而release (stages ops and dev) 是 azure devops Releases hub 中的经典发布管道?您应该知道管道资源触发器不适用于经典发布管道。

      build =&gt; release (stages ops and dev) for repo A 应该在同一管道中(即 azure-pipelines.yml)。因此,您在管道 B 中定义的管道资源触发器仅在管道 A 如下所示时有效:

      name: ..
      trigger:
        - none
      resources:
       containers:
          ..
      variables:
        ..
      
      stages:
      - stage: build  # build the project in build stage
        jobs:
        - job 
          ..
      
      - stage: ops    #stage ops
        jobs:
        - job:
          ...
      
      - stage: dev    #stage dev
        jobs:
        - job:
          ...
      

      管道 B 中的 source 是 julie-ng 提到的管道 A 的名称。见下例:

      resources:
        pipelines:
        - pipeline: {Can be Any String} #identifier for the resource (used in pipeline resource variables)
          source: {Name of the pipeline A what you see in the UI}  #name of the pipeline that produces an artifact
      

      管道 A 的名称

      管道 B 中的资源触发器:

      resources:
       pipelines:
         - pipeline: AnyString
           source: pipelineA
           branch: DATA-1234
      

      如果 repo A 的发布管道是经典发布管道。您可以在stage dev中添加这个外部任务Trigger Build来触发stage dev中的管道B:

      - task: benjhuser.tfs-extensions-build-tasks.trigger-build-task.TriggerBuild@3
        displayName: 'Trigger a new build of 48'
        inputs:
          buildDefinition: {ID of pipeline B}
          buildParameters: 'variableName: variableValue'  
          password: '$(System.AccessToken)'
      

      如果您想将一些变量从管道 A 传递到管道 B。您可以使用 buildParameters 字段。

      在 pipelien B 中,单击变量按钮定义一个变量来保存变量值。 (注意:勾选这个选项让用户在运行这个管道的时候覆盖这个值,这样就可以从A管道覆盖)

      您始终可以使用 Rest api 来触发管道。请参阅以下主题以获取更多信息

      this线程

      send multiple parameter to Azure-Devops pipeline job via Powershell

      Can you pass a file to an azure pipeline?

      更新:

      您可以使用Builds - Queuerest api 来触发管道。

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

      见下例:

      curl -X POST --silent \
      -H "Authorization:Bearer $(System.AccessToken)"  \  
      -H "Content-Type:application/json" \ 
              $(System.TeamFoundationCollectionUri)$(System.TeamProject)/_apis/build/builds?api-version=6.1-preview.6 \
      
      -d '{  
           "definition":{ "id": id-of-pipelineB}, 
           "sourceBranch":"refs/heads/DATA-1234"
          }'
      

      【讨论】:

      • 谢谢,我的管道是一个经典的管道,并且在阅读了您的输入之后。我放弃了使用资源触发器。但是,在A 的阶段 Dev 之后,我已经开始使用 API 构建队列来调用 repo B @ DATA-1234 的构建管道,我使用 api 调用获得了构建 ID 和项目 ID,然后调用 POST启动构建管道。现在它向我抛出此错误"message":"The request specifies project ID 0b330b4e-1ecb-47e7-854f-e6e799833627 but the supplied pipeline specifies project ID 00000000-0000-0000-0000-000000000000." 请参阅下面的帖子以检查 API 调用。
      • curl -X POST \ --silent \ -H "Authorization:Bearer $(System.AccessToken)" \ -H "Content-Type:application/json" \ $(System.TeamFoundationCollectionUri)/$(System.TeamProject)/_apis/build/definitions?api-version=6.0 \ --output /tmp/response1.json \ -d'{ "id":"$id", "sourceBranch":"refs/heads/DATA-1234", "project":{"name":"ProjectA", "id":"$project_id"} }' 所以我不知道项目 ID 00000000-0000-0000-0000-000000000000 来自哪里。有什么想法吗?
      • @change198 看起来你使用了错误的 rest api。您使用的上述 api 用于创建管道定义而不是排队构建。您应该使用构建队列休息 api 来触发管道。请参阅上面的更新。
      • @change198 你检查了上面的更新,进展如何?
      • 现在它给了我"message":"TF400898: An Internal Error Occurred. Activity Id: acbae1be-ceb0-4cc1-a1df 453211e2d8c6.", "typeName": "Newtonsoft.Json. JsonReaderException, Newtonsoft.Json","typeKey":"JsonReaderException" 我不知道我做错了什么? curl -X POST --silent -H "Authorization:Bearer $(System.AccessToken)" -H "Content-Type:application/json" $(System.TeamFoundationCollectionUri)$(System.TeamProject)/_apis/build/builds?api-version=6.1-preview.6 --output /tmp/response1.json -d '{ "sourceBranch":"refs/heads/DATA-1234", "definition":{ "id": $id } }'
      猜你喜欢
      • 2019-08-02
      • 2021-05-02
      • 2023-03-26
      • 2019-08-22
      • 2020-12-08
      • 2022-07-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      相关资源
      最近更新 更多