Trigger a Azure DevOps pipeline from a Test Plan and report the results back to it

Azure DevOps configuration

We need to create a service connection, using the "incoming webhook" template, so that we can use Azure DevOps API later on.


Create a Personal Access Token (PAT), so you can use it as the password in API requests, along with the "organization name" as username.

  



Then, in your Azure DevOps repository containing the project's code and tests, create a pipeline /azure-pipelines.yml; this pipeline will be triggered using Azure DevOps API.


In the following example, the pipeline will receive the Test Plan issue key as an input parameter. It will then run the build, including the automated tests, and in the end it will report the results back to Xray using "curl" utility.

We need to define a resources section, that contains a reference to the webhook configured earlier.


/azure-pipelines.yml
parameters:
- name: "testplan"
  type: string
  default: ""

trigger:
- main

resources:
  webhooks:
    - webhook: "MyWebhookTrigger"             ### Webhook alias
      connection: "MyWebhookConnection"       ### Incoming webhook service connection

pool:
  vmImage: ubuntu-latest

steps:

- script: dotnet restore
  displayName: 'install build dependencies'

- script: |
    dotnet test -s nunit.runsettings
  displayName: 'Run tests'
- bash: |
    set -x
    export token=$(curl -H "Content-Type: application/json" -X POST --data "{ \"client_id\": \"$CLIENT_ID\",\"client_secret\": \"$CLIENT_SECRET\" }" "$(xray_endpoint)/api/v2/authenticate"| tr -d '"')  
    curl -o - -H "Content-Type: text/xml" -H "Authorization: Bearer $token" --data @./bin/Debug/net5.0/TestResults/nunit_webdriver_tests.xml  "$(xray_endpoint)/api/v2/import/execution/nunit?projectKey=CALC&testPlanKey=${TESTPLAN}"
  displayName: 'Import results to Xray cloud'


Xray endpoint's base URL and the API key credentials (i.e. client id + client secret) are defined in Azure DevOps as variables. These may be marked as secret.

  



Automation configuration

  1. create a new rule and define the "When" (i.e. when it to should be triggered ), to be "Manually triggered"


2. define the condition so that this rule can only be executed from Test Plan issue; you can also do it directly on the previous step

3. define an action (i.e. the "Then") as "Send web request" and configure it as follows


  • the web request URL provided above is from Azure DevOps API, for queueing builds, and follows this syntax:
    • https://dev.azure.com/<organization_name>/<project>/_apis/build/builds?ignoreWarnings=true&api-version=6.0
  • authentication is done using the organization name plus the personal access token, created earlier in Azure DevOps, as the login:password pair used to calculate the Base64 content of the Authorization header
  • the "Content-Type" header should be "application/json"
  • the HTTP POST body content, defined in the "Custom data" field, will be used to identify the build definition and also the original Test Plan issue key; 

    custom data (i.e. HTTP body content)
    {
        "parameters": "{ \"testplan\": \"{{issue.key}}\" }", 
        "definition":  {
                           "id": 3
                       }
    }

    Note: to find the definition id, you can click on the pipeline in Azure DevOps and its id is shown as part of the URL


After publishing the rule, you can go to the screen of an issue and trigger a pipeline run in Azure DevOps.



In this case, since the pipeline was configured to report results back to Xray, a new Test Execution would be created and linked back to the source Test Plan where the automation was triggered from.

References