Search code examples
azure-devopsazure-pipelinesazure-triggers

Azure DevOps Pipeline Trigger Tags


I have created a pipeline with a trigger:

enter image description here

I want this pipeline to run every time I pr with "v3.*" as the tag. Now I create a new pr:

enter image description here

However, the pipeline doesn't run. Am I doing something wrong here?

Thanks very much!


Solution

  • The root cause is the trigger you use is not PR trigger, it is CI trigger.

    https://learn.microsoft.com/en-us/azure/devops/pipelines/repos/azure-repos-git?view=azure-devops&tabs=yaml#tags

    enter image description here

    The PR trigger of Azure Git repo is this:

    https://learn.microsoft.com/en-us/azure/devops/pipelines/repos/azure-repos-git?view=azure-devops&tabs=yaml#pr-triggers

    You will find no built-in feature for the requirement you want for now:

    https://learn.microsoft.com/en-us/azure/devops/repos/git/branch-policies?view=azure-devops&tabs=browser#build-validation

    https://learn.microsoft.com/en-us/azure/devops/repos/git/branch-policies?view=azure-devops&tabs=browser

    enter image description here

    You have two ways to achieve your requirements.

    1, Automated method. The first way is to send a web hook request when a PR was created, and and push the request to other hosted service, such as azure function(Of course you can use other service to achieve your requirements.).

    enter image description here

    enter image description here

    enter image description here

    below is the sample code of python azure function to achieve the requirements:

    import logging
    import requests
    import json
    import azure.functions as func
    
    
    def main(req: func.HttpRequest) -> func.HttpResponse:
        logging.info('Python HTTP trigger function processed a request.')
        req_body = req.get_body().decode('utf-8')
        #convert string to json
        req_body_json = json.loads(req_body)
        pull_request_id = req_body_json['resource']['pullRequestId']
        print(pull_request_id)
        #===========================================================
        #Here is the core logic.
        org_name = "<Your organization name>"
        project_name = "Your project name"
        pipeline_definition_id = "<Your pipeline definition id>"
        PAT = "<Your personal Access Token>"
    
    
        gettags_url = "https://dev.azure.com/"+org_name+"/_apis/Contribution/HierarchyQuery/project/"+project_name+"?api-version=5.0-preview.1"
    
        gettags_payload = json.dumps({
        "contributionIds": [
            "ms.vss-code-web.pr-detail-data-provider"
        ],
        "dataProviderContext": {
            "properties": {
            "baseIterationId": 0,
            "iterationId": 1,
            "pullRequestId": pull_request_id,
            "repositoryId": "xxx",
            "types": 1019,
            "sourcePage": {
                "url": "https://dev.azure.com/xxx/xxx/_git/xxx/pullrequest/xxx",
                "routeId": "ms.vss-code-web.pull-request-details-route",
                "routeValues": {
                "project": "xxx",
                "GitRepositoryName": "xxx",
                "parameters": "66",
                "vctype": "git",
                "controller": "ContributedPage",
                "action": "Execute",
                "serviceHost": "xxx"
                }
            }
            }
        }
        })
        gettags_headers = {
        'Authorization': 'Basic '+PAT,
        'Content-Type': 'application/json',
        }
    
        gettags_response = requests.request("POST", gettags_url, headers=gettags_headers, data=gettags_payload)
    
        json_gettags_response = gettags_response.json()
    
        tags = json_gettags_response['dataProviders']['ms.vss-code-web.pr-detail-data-provider']['labels']
        for tag in tags:
            print(tag['name'])
            if tag['name'] == 'testtag':
                print('==========================')
                print('found testtag')
    
                runpipeline_url = "https://dev.azure.com/"+org_name+"/"+project_name+"/_apis/pipelines/"+pipeline_definition_id+"/runs?api-version=6.0-preview.1"
    
                runpipeline_payload = json.dumps({
                "runParameters": ""
                })
                runpipeline_headers = {
                'Authorization': 'Basic '+PAT,
                'Content-Type': 'application/json',
                }
    
                runpipeline_response = requests.request("POST", runpipeline_url, headers=runpipeline_headers, data=runpipeline_payload)
    
                print(runpipeline_response.text)
    
                print('==========================')
            else:
                pass
    
        #===========================================================
        return func.HttpResponse(f"This HTTP triggered function executed successfully.")
    

    The idea of how to get the 'gettags_payload(request body)' please check of this answer that I posted:

    Auto abandon Pull Request after 2 weeks with no updates

    requirements.txt

    azure-functions
    requests
    

    I have checked this method on my side and this works fine.

    2, Manually check the tags.

    The automated method on the above is very complicated, if you don't want to design so complicated method, you can add someone to manually review the PR, and check the tags via their eyes.

    enter image description here

    enter image description here

    enter image description here