Trigger another build exist in project in Azure Devops

人走茶凉 提交于 2020-12-15 06:07:16

问题


I have a repo name called A with its build pipeline as azure-pipelines.yml Then I have another repo called B with its build pipeline as azure-pipelines.yml

Both A and B are under same project ProjectA

This is the flow for

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

So, what I want to achieve is as soon as the release finished from repo A it should trigger build repo B. My pipeline A looks like this :

name: SomethingFancy

trigger:
  - none

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

variables:
  major: 2
  minor: 0

So I made pipeline B looks like this:

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

So far I'm not able to run the pipeline as it complains "Pipeline Resource SomethingFancy Input Must be Valid." as per the documentation it is something # identifier for the resource (used in pipeline resource variables).

I'm referring to [this][1] for the collection of resources.

I'm also intended to use [api][2] call to queue the build of the B, but not able to find what should be the body of the post message e.g. how to add the branch of pipeline B, or how to pass the parameters to the pipeline of B

EDIT

see attached my pipeline name [![enter image description here][3]][3] and build source pipeline also called azurepipelines.yml and release pipeline has one stage called Dev

Now my pipeline B looks like this:

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

still I don't see any auto kick off of build pipeline of 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


回答1:


I noticed the the flow for repo A is build => release (stages ops and dev). I wonder if the build is the build pipeline as azure-pipelines.yml, and release (stages ops and dev) is the classic release pipeline in azure devops Releases hub? You should know that pipeline resources triggers doesnot work for classic release pipeline.

build => release (stages ops and dev) for repo A should be in the same pipeline (ie. azure-pipelines.yml). So the pipeline resources trigger you defined in pipeline B only works when the pipeline A looks like below:

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:
    ...

The source in Pipeline B is the name of the pipeline A as julie-ng mentioned. See below example:

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

Name of the pipeline A:

Resource trigger in Pipeline B:

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

If the release pipeline for repo A is the classic release pipeline. You can add this external task Trigger Build in stage dev to trigger pipeline B in stage dev:

- 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)'

If you want to pass some variables from Pipeline A to pipeline B. you can use the buildParameters field.

In pipelien B, Click the Variables button to define a Variable to the hold the variable value. (Note: Check this option Let users override this value when running this pipeline, so that it can be overrode from A pipeline )

You can always use the Rest api to trigger the pipeline. Please see below threads for more inforamtion

this thread

send multiple parameter to Azure-Devops pipeline job via Powershell

Can you pass a file to an azure pipeline?

Update:

You can use Builds - Queue rest api To trigger a pipeline.

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

See below example:

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"
    }'



回答2:


As I read from the same documentation I think you should set the source property to the name of your pipeline from repo A. source: SomethingFancy




回答3:


The error message looks like it's telling you it cannot find a pipeline with the name you specified, probably because, name means refers to the build numbering format in pipeline YAML e.g.

name: $(BuildID)

As @Roderick noted, the name of the pipeline should be what you see in the UI. From the main "Azure Pipelines" screen in your project. First click the "three dots" to get a submenu and then click "Rename/move". Example screenshot:

So now you should have the project name and pipeline names you need to update your YAML in pipeline B and it should work.




回答4:


After a lot of struggle and the help of wise people here I finally manage to tame the issue. I'm posting here so that anyone can take a referebce. This is working now refer: ListBuild and QueueTheBuild

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


来源:https://stackoverflow.com/questions/64824802/trigger-another-build-exist-in-project-in-azure-devops

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!