Trigger a TravisCI project build from an issue

In this very simple scenario, we'll implement a rule, triggered manually, that will trigger a TravisCI project/job. The action will be available from the "Automation" panel, in all issues of the selected project.

We're assuming that:

  • you just want to trigger a CI job, period; this job may be totally unrelated to the issue from where you triggered it
  • what the CI job will do, including if it will report the results back to Xray or not, is not relevant


TravisCI configuration

In TravisCI, we need to generate an API authentication token for some user, which can be done from the My Account settings page.

We have defined the following configuration in our source code that will be used in TravisCI configuration of the pipeline:

.travis.yml
sudo: false
language: java
jdk:
  - openjdk8
cache:
  directories:
  - "$HOME/.cache"
  
jobs:
  include:
    - stage: test and report to Xray
      script:
        - |
            echo "building repo..."
            mvn clean compile test --file pom.xml
            export token=$(curl -H "Content-Type: application/json" -X POST --data "{ \"client_id\": \"$CLIENT_ID\",\"client_secret\": \"$CLIENT_SECRET\" }" https://xray.cloud.xpand-it.com/api/v2/authenticate| tr -d '"')
            echo $token
            curl -H "Content-Type: text/xml" -H "Authorization: Bearer $token" --data @target/surefire-reports/TEST-com.xpand.java.CalcTest.xml  "https://xray.cloud.xpand-it.com/api/v2/import/execution/junit?projectKey=$PROJECTKEY"
            echo "done"

For the configuration of the YAML file we used the TravisCI tutorial documentation.

As you can see we are pushing results back to Xray with the last curl command:

curl command
curl -H "Content-Type: text/xml" -H "Authorization: Bearer $token" --data @target/surefire-reports/TEST-com.xpand.java.CalcTest.xml  "https://xray.cloud.xpand-it.com/api/v2/import/execution/junit?projectKey=$PROJECTKEY"

On this command we are passing the project key in order to report back to a specific Project in the Xray side, we will further ahead show how it is populated.

  • PROJECTKEY - The key that identifies the project on the Jira side.

Once we have the authentication token we followed the TravisCI API documentation to configure the following steps on the Jira side. 

Automation configuration

On the Jira side we will use the Automation capabilities that it provides out of the box, so within the project you want to use automation go to project settings and: 

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

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


  • the Webhook URL provided above follows this syntax:
    • <TravisCI_API_URL>/repo/{slug|id}/requests (The %2F in the request URL is required so that the owner and repository name in the repository slug are interpreted as a single URL segment.)
  • besides the "Content-Type" header that should be "application/json", define also an "Authorization" header having the value "token <token>", where  you will place the authentication token obtained previously in the TravisCI page and the "Travis-API-Version" header is also mandatory and it will contain the version used.
  • Custom data
    • We included the simplest possible just to trigger the pipeline from the master branch.

After publishing the rule, you can go to the screen of an issue and trigger the TravisCI project/job.


In this case, since TravisCI was configured to report results back to Xray, a new Test Execution would be created in Jira/Xray.


Trigger a TravisCI project build from a Test Plan and report the results back to it

In this simple scenario, we'll implement a rule, triggered manually, that will trigger a TravisCI project/job. The action will be available from the "Automation" panel, for all Test Plan issues of the selected project.

We're assuming that:

  • you just want to trigger a CI job, period; this job may be totally unrelated to the issue from where you triggered it
  • the results will be submitted back to Xray, if the project is configured to do so in TravisCI


TravisCI configuration

In TravisCI, we need to generate an API authentication token for some user, which can be done from the My Account settings page.

Once we have the authentication token we followed the TravisCI API documentation to configure the following steps on the Jira side. 

For the TravisCI the important change we must do is in the YAML file that will configure TravisCI pipeline, we use the following configuration to achieve that:

.travis.yml
sudo: false
language: java
jdk:
  - openjdk8
cache:
  directories:
  - "$HOME/.cache"
  
jobs:
  include:
    - stage: test and report to Xray
      script:
        - |
            echo "building repo..."
            mvn clean compile test --file pom.xml
            export token=$(curl -H "Content-Type: application/json" -X POST --data "{ \"client_id\": \"$CLIENT_ID\",\"client_secret\": \"$CLIENT_SECRET\" }" https://xray.cloud.xpand-it.com/api/v2/authenticate| tr -d '"')
            echo $token
            curl -H "Content-Type: text/xml" -H "Authorization: Bearer $token" --data @target/surefire-reports/TEST-com.xpand.java.CalcTest.xml  "https://xray.cloud.xpand-it.com/api/v2/import/execution/junit?projectKey=$PROJECTKEY&testPlanKey=$TESTPLAN"
            echo "done"

For more details about this configuration please check the TravisCI tutorial documentation.

As you can see we are pushing results back to Xray with the last curl command:

curl command
curl -H "Content-Type: text/xml" -H "Authorization: Bearer $token" --data @target/surefire-reports/TEST-com.xpand.java.CalcTest.xml  "https://xray.cloud.xpand-it.com/api/v2/import/execution/junit?projectKey=$PROJECTKEY&testPlanKey=$TESTPLAN"

On this command we are passing the project key in order to report back to a specific Project in the Xray side, we will further ahead show how it is populated.

  • PROJECTKEY - The key that identifies the project on the Jira side.
  • TESTPLAN - The Test Plan key used to identify the Test Plan to associate the execution with.

Once we have the authentication token we followed the TravisCI API documentation to configure the following steps on the Jira side. 

Automation configuration

On the Jira side we will use the Automation capabilities that it provides out of the box, so within the project you want to use automation go to the automation entry in the project settings and: 

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

2. define a condition, in our case we will define that only Test Plan issue types will be allowed to trigger this pipeline; this could also be defined on the previous step


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


  • the Webhook URL provided above follows this syntax:
    • <TravisCI_API_URL>/repo/{slug|id}/requests (The %2F in the request URL is required so that the owner and repository name in the repository slug are interpreted as a single URL segment.)
  • besides the "Content-Type" header that should be "application/json", define also an "Authorization" header having the value "token <token>", where  you will place the authentication token obtained previously in the TravisCI page and the "Travis-API-Version" header is also mandatory and it will contain the version used.
  • Custom data
    • We included the simplest possible just to trigger the pipeline from the master branch.
    • Added environment configuration variables to be used later in the TravisCI pipeline
      • TESTPLAN - that will be automatically filled with the test plan key from where the pipeline is triggered.
      • PROJECTKEY - that will be automatically filled in with the project key.

After publishing the rule, you can go to the screen of a Test Plan issue and trigger the TravisCI project/job.


In this case, since TravisCI was configured to report results back to Xray, a new Test Execution would be created in Jira/Xray.

Associated with the Test Plan that we have passed along:

References