Trigger another build exist in project in Azure Devops

后端 未结 4 2026
暖寄归人
暖寄归人 2021-01-28 13:52

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

相关标签:
4条回答
  • 2021-01-28 14:22

    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
    
    0 讨论(0)
  • 2021-01-28 14:25

    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"
        }'
    
    0 讨论(0)
  • 2021-01-28 14:32

    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.

    0 讨论(0)
  • 2021-01-28 14:41

    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

    0 讨论(0)
提交回复
热议问题