azure-devopsazure-pipelines

How to find the associated pipeline in Azure DevOps from a given yaml file?


I have many pipelines in Azure DevOps. My code is stored in an Azure DevOps git repository. I know the path to a yaml file which is the definition of one of my pipelines. Unfortunately, the naming convention of the pipelines has not always been ideal, so I cannot easily find which pipeline it is associated with. Are there any quick ways to find it? Checking each pipeline individually is not desirable.


Solution

  • No built-in feature, I write a sample python code to achieve your requirement:

    import requests
    
    orgname = "<Your Organization Name>"
    projectname = "<Your Project Name>"
    yamlname = "<Your YAML file name>"
    reponame = "<Your Repo Name>"
    
    PAT = "<Your Personal Access Token>"
    
    url_repo = "https://dev.azure.com/"+orgname+"/"+projectname+"/_apis/git/repositories/"+reponame+"?api-version=4.1"
    
    payload_repo={}
    headers_repo = {
      'Authorization': 'Basic '+PAT
    }
    
    response_repo = requests.request("GET", url_repo, headers=headers_repo, data=payload_repo)
    data_repo = response_repo.json()
    repoid = data_repo.get('id')
    
    url_pipelines = "https://dev.azure.com/"+orgname+"/"+projectname+"/_apis/pipelines?api-version=6.0-preview.1"
    
    payload_pipelines={}
    headers_pipelines = {
      'Authorization': 'Basic '+PAT  
    }
    
    response_pipelines = requests.request("GET", url_pipelines, headers=headers_pipelines, data=payload_pipelines)
    data_pipelines = response_pipelines.json()
    counter_pipelines = str(data_pipelines.get('count'))
    
    
    counter = 0
    
    for f in data_pipelines.get('value'):
        counter = counter + 1
        url_pipeline = f.get('url')
        id_pipeline = f.get('id')
        pipeline_name_current = f.get('name')
        url_pipeline = "https://dev.azure.com/"+orgname+"/"+projectname+"/_apis/pipelines/"+str(id_pipeline)+"?revision=1"
    
        payload_pe={}
        headers_pe = {
        'Authorization': 'Basic '+PAT
        }
    
        response_pe = requests.request("GET", url_pipeline, headers=headers_pe, data=payload_pe)
        data_pe = response_pe.json()
        if (data_pe.get('configuration').get('type') == 'yaml') and (data_pe.get('configuration').get('repository') != None):
            #
            current_yamlname = data_pe.get('configuration').get('path')
            current_pipelinename = data_pe.get('name')
            current_repoid = data_pe.get('configuration').get('repository').get('id')
            if (current_yamlname == yamlname) and (current_repoid==repoid):
                print(data_pe.get('name'))
        elif data_pe.get('configuration').get('repository'):
            print("This is not a DevOps Repository")
        else:
            #No YAML
            pass
    print(counter)
    

    I can get the pipeline name by providing the YAML file and repo name:

    enter image description here