CircleCI

Configure Flaky Tests using CircleCI

Getting Started

You can use the Flaky Tests CLI within your CircleCI workflows to upload and analyze your test results.

The Trunk Flaky Tests CLI currently only supports x86_64 and arm64 for both Linux and macOS. If you have another use case, please get in touch with support at https://slack.trunk.io. For the best results, you'll need to validate that your test invocation doesn't use cached test results and doesn't automatically retry failing tests.

Create a CircleCI Workflow

Create a CircleCI workflow (or modify an existing one) to run the tests that you want to monitor. The workflow should produce a test report in JUnit XML format. Most testing frameworks support XML output. See Testing Framework Configuration for guides for common testing frameworks. Make sure that your test invocation doesn't use cached test results, and doesn't automatically retry failing tests.

Find Organization Slug and Token

Next you will need your Trunk organization slug and token. Navigate to app.trunk.io. Once logged in navigate to Settings -> Manage -> Organization. Copy your organization slug. You can find your Trunk token by navigating to Settings → Manage Organization → Organization API Token and clicking "View." Copy this token. Make sure you are getting your organization token, not your project/repo token.

Set Project Environment Variables

In your CircleCI project settings under Environment Variables, create new variables for your Trunk org as TRUNK_ORG_SLUG and the api token as TRUNK_API_TOKEN.

Add Uploader to Testing Workflow

Now update your CircleCI workflow to download and run the test uploader binary after you've run your tests. Here is an example of a NodeJS project using JUnit tests.

You can upload test results to Flaky Tests with the trunk-analytics-cli by running it in a stage after your tests are complete. There are four different OS/arch builds of the CLI in the latest release. Pick the one you need for your testing platform and be sure to download the release on every CI run. Do not bake the CLI into a container or VM. This ensures your CI runs are always using the latest build.

upload.yaml
version: 2.1
orbs:
  node: circleci/node@5
  python: circleci/python@2
jobs:
  test-node:
    # Install node dependencies and run tests
    executor: node/default
    steps:
      - checkout
      - node/install-packages:
          cache-path: ~/project/node_modules
          override-ci-command: npm install
      - run:
          name: Run tests with Jest
          command: |
            ./node_modules/.bin/jest --config=javascript/tests/jest/jest.config.json javascript/tests/jest/**/*.js
      - run:
          name: Upload test results to Trunk
          when: always
          command: |
            curl -fsSL --retry 3 "https://github.com/trunk-io/analytics-cli/releases/latest/download/trunk-analytics-cli-x86_64-unknown-linux.tar.gz" | tar -xvz > ./trunk-analytics-cli
            ./trunk-analytics-cli upload --junit-paths "tests/jest/jest_junit_test.xml" --org-url-slug ${TRUNK_ORG_SLUG} --token ${TRUNK_API_TOKEN}

In the config about we have added a second run step fo the test-node job. This step downloads the latest release of the trunk-analytics-cli, makes it executable, then runs it to upload the test output xml file. The TRUNK_ORG_SLUG and TRUNK_API_TOKEN variables are filled in at runtime by the CircleCI environment variables set earlier. Note that the when property is set to always because it should run whether or not the actual tests in the previous run step succeed.


If you're interested in better understanding this binary or want to contribute to it, you can find the open source repo here.

Last updated