> ## Documentation Index
> Fetch the complete documentation index at: https://docs.trunk.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Upgrading to Analytics CLI 1.0

> Move Flaky Tests uploads from the Trunk Analytics CLI 0.17.x and the organization token to 1.0, an API key and a test collection, on every branch and CI job that uploads.

The Trunk Analytics CLI 1.0 uploads only to [test collections](https://docs.trunk.io/flaky-tests/test-collections.md) and accepts only an API key. Upgrading means three changes for each upload: an API key in place of the organization token, a test collection ID, and no removed flags.

0.17.x keeps working with the organization token until **December 31, 2027**. It gets important fixes, but no new features.

> [!WARNING]
>
> **The credential and the CLI version change together.** 0.17.x refuses an API key, and 1.0 refuses the organization token. Changing only one of them stops your uploads.

## Before you start

### Find every place that uploads

Check every branch that runs CI, not only your default branch. That includes release branches, maintenance branches and long-lived feature branches. A branch keeps the CI files it was created with, so an old release branch can still run an old uploader with the organization token after `main` has moved.

Search each branch for:

* `trunk-io/analytics-uploader`
* `trunk flakytests` or `trunk ft`
* `trunk-analytics-cli`
* `rspec_trunk_flaky_tests`
* The secret that holds your organization token, such as `TRUNK_API_TOKEN` or `TRUNK_TOKEN`

For example:

```bash theme={"system"}
git fetch origin
for b in $(git branch -r --format='%(refname:short)' | grep -v HEAD); do
  git grep -l -E 'analytics-uploader|flakytests|trunk-analytics-cli|rspec_trunk_flaky_tests' "$b" -- \
    .github .circleci .buildkite .gitlab-ci.yml Jenkinsfile Gemfile Gemfile.lock 2>/dev/null
done
```

Also check CI configuration that isn't in the repository, such as shared GitHub Actions workflows, reusable CircleCI orbs, Buildkite pipeline settings, and Jenkins shared libraries.

### Create an API key

1. Open [API Keys](https://app.trunk.io/settings/developer/api-tokens) in the Trunk app and click **Create Key**.
2. Name the key after what uses it, such as `flaky-tests-ci`.
3. Select the **Upload test results** permission.
4. Copy the key. It starts with `trunk_v2_` and is shown only once.

### Store it under a new secret name

Add the API key to your CI provider as a **new** secret, such as `TRUNK_API_KEY`. Don't overwrite the secret that holds your organization token.

Every branch that hasn't moved yet still runs 0.17.x or earlier with the old secret. If you overwrite that secret with the API key, those branches get a key they refuse, and their uploads stop. With two secrets, each branch keeps working until you upgrade it.

### Find your test collection ID

Each upload names the test collection it goes to. If your repository uploaded before test collections, Trunk already created a collection for it, and its test history is there.

Open **Flaky Tests** → **Collections** in the [Trunk app](https://app.trunk.io), open the collection, and copy its **Collection ID** from the page header. The ID isn't a secret, so you can write it in your CI configuration.

You can also list your collections with the API. Any API key can call it, including the one you just created:

```bash theme={"system"}
curl -fsS https://api.trunk.io/v2/test-collections \
  -H "Authorization: Bearer $TRUNK_API_KEY"
```

Each collection's `id` is its Collection ID, a short value such as `aB3xY9kQ`. Pass it as the test collection ID. See [List test collections](https://docs.trunk.io/api/testcollections/list-test-collections.md). To create a collection, see [Getting Started](https://docs.trunk.io/flaky-tests/get-started#step-1-create-a-test-collection).

## Upgrade your uploads

Follow the section for each setup you found. Change the credential and the version in the same commit.

### GitHub Actions

`trunk-io/analytics-uploader@v2` stays on 0.17.x. Move every step to `@v3`, which runs 1.0:

```yaml theme={"system"}
- name: Upload test results to Trunk
  if: ${{ !cancelled() }}
  uses: trunk-io/analytics-uploader@v3
  with:
    junit-paths: "**/junit.xml"
    org-slug: <TRUNK_ORG_URL_SLUG>
    test-collection-id: <TRUNK_TEST_COLLECTION_ID>
    token: ${{ secrets.TRUNK_API_KEY }}
```

* Set `token` to the API key. `@v3` fails the step unless the value starts with `trunk_v2_`.
* Set `test-collection-id`. It's required. If you set `TRUNK_TEST_COLLECTION_ID` in the environment instead, move it to this input.
* Remove `hide-banner`, `public-repo-id` and `gh-action-ref`. `@v3` doesn't have them. For fork pull requests, see [Fork pull requests](#fork-pull-requests).
* Remove `cli-version` if it pins a 0.x release. `@v3` runs only 1.x.
* `@v3` doesn't read `TRUNK_API_TOKEN` from the environment. Pass the key as `token`.

Every other input is unchanged. See [GitHub Actions](https://docs.trunk.io/flaky-tests/get-started/ci-providers/github-actions.md).

### Other CI providers

Upload with the [`trunk` CLI](https://docs.trunk.io/flaky-tests/get-started/trunk-cli). Install it fresh in each job, so the job gets `trunk` CLI 0.0.14 or later, which runs Analytics CLI 1.0:

```bash theme={"system"}
curl -fsSL https://install.trunk.io | sh
TRUNK_ORG_API_KEY="$TRUNK_API_KEY" trunk auth api-key set

<run your tests>

trunk flakytests upload --junit-paths "**/junit.xml" \
  --org-url-slug <TRUNK_ORG_URL_SLUG> \
  --test-collection-id <TRUNK_TEST_COLLECTION_ID>
```

* The `trunk` CLI doesn't update itself in CI. A build image or self-hosted runner with an older `trunk` CLI installed keeps that version. Run the installer in the job, or run `trunk update` when you build the image.
* If you ran `trunk flakytests` through the older `trunk` launcher, the one you installed from `get.trunk.io` for Code Quality, install the new `trunk` CLI instead. See [Install the trunk CLI](https://docs.trunk.io/flaky-tests/get-started/trunk-cli.md#install-the-trunk-cli).
* Remove `TRUNK_ANALYTICS_CLI_VERSION` if it pins a 0.x release.
* If you set `TRUNK_API_TOKEN` or `--token`, the `trunk` CLI passes it instead of the stored key. Set it to the API key, or remove it.

### RSpec plugin

`rspec_trunk_flaky_tests` 1.0 is already the latest version on RubyGems, so an unpinned gem, or a Dependabot or Renovate update, installs it now.

The 1.0 gem requires these environment variables when you run `rspec`. If one is missing, or `TRUNK_API_TOKEN` holds the organization token, the run fails before any test runs.

| Variable | Value |
| - | - |
| `TRUNK_ORG_URL_SLUG` | Your organization slug |
| `TRUNK_API_TOKEN` | The API key |
| `TRUNK_TEST_COLLECTION_ID` | The test collection ID |

To run without Trunk, set `DISABLE_RSPEC_TRUNK_FLAKY_TESTS=true`. An empty `TRUNK_API_TOKEN`, such as on a fork pull request with no secrets, turns the gem off without failing.

If you aren't ready to move, pin 0.17.x:

```ruby theme={"system"}
gem 'rspec_trunk_flaky_tests', '~> 0.17.0'
```

## What changes in 1.0

### Removed flags and variables

| Removed | Instead |
| - | - |
| `--use-quarantining` | Nothing for `true`. `--disable-quarantining` for `false`. |
| `--team`, `--print-files`, `--hide-banner` | Remove them. They did nothing. |
| `--public-repo-id`, `TRUNK_PUBLIC_REPO_ID` | `--allow-forked-pr-uploads` with `--test-collection-id`. |
| `--hide-test-collection-links`, `TRUNK_HIDE_TEST_COLLECTION_LINKS` | Remove it. Every link is a test collection link. |

`upload` and `validate` exit with code `2` on a removed flag. `trunk flakytests test` reads an unknown flag before `--` as part of your test command, so the run fails and your tests don't run.

### Fork pull requests

`--allow-forked-pr-uploads` (`allow-forked-pr-uploads: true` on `@v3`) uploads with no API key, but only in a GitHub Actions run of a pull request from a fork, with `GITHUB_TOKEN` in the job's environment. Anywhere else it uploads nothing and says why. See [Fork PR uploads](https://docs.trunk.io/flaky-tests/get-started/fork-pr-uploads.md).

### Output

* The summary goes to stdout. Logs, notices and progress go to stderr. A script that reads only stderr loses the summary.
* `--output-format json`, or `TRUNK_OUTPUT_FORMAT=json`, prints the summary as JSON, in the same shape that `--summary-output-file` writes.
* Test links point at the test in its collection.
* In the JSON summary, `schemaVersion` is `2`, and a failed test's `id` is its `testId` in the [API](https://docs.trunk.io/api.md). It's `null` when Trunk couldn't identify the run's collection.

## Verify the upgrade

1. Push the change and let CI run on each branch you changed.
2. In the job log, check that the CLI version is 1.0 or later and the upload succeeded.
3. Open the collection's **Uploads** tab in the [Trunk app](https://app.trunk.io) and check that uploads arrive from each branch and CI job.

## Retire the organization token

Keep the old secret until no branch uses it. When every branch you still run CI on has moved:

1. Search every branch again for the old secret's name.
2. Delete the old secret from your CI provider.

Branches you never build again can keep the old configuration. If you build one later, its upload fails.

The organization token is still used by other Trunk features, such as the legacy API and Merge Queue. Don't rotate or revoke it unless nothing else uses it.

## Troubleshooting

| Message or symptom | Cause | Fix |
| - | - | - |
| `This Trunk token is not a v2 API key.` | 1.0 got the organization token. | Pass the API key. |
| `token must be a v2 API key (trunk_v2_...)` | `@v3` got the organization token. | Set `token` to the API key. |
| `403` naming **Upload test results** | The API key lacks the permission. | Create a key with **Upload test results**. |
| `This run names no test collection.` | `--test-collection-id` or `TRUNK_TEST_COLLECTION_ID` is missing or blank. | Set the collection ID. |
| Exit code `2`, "unexpected argument" | A removed flag. | Remove it. See [Removed flags and variables](#removed-flags-and-variables). |
| `RSpec::Trunk::ConfigurationError` | The 1.0 gem is missing the API key or collection ID. | See [RSpec plugin](#rspec-plugin). |
| Upload refused on 0.17.x after a secret change | 0.17.x got the API key. | Upgrade that job to 1.0, or point it back at the old secret. |

## Staying on 0.17.x for now

To keep the organization token until you move, pin 0.17.x everywhere you upload:

| Setup | Pin |
| - | - |
| GitHub Actions | `trunk-io/analytics-uploader@v2` |
| `trunk` CLI | `TRUNK_ANALYTICS_CLI_VERSION=0.17.0`, with the token in `TRUNK_API_TOKEN` |
| RSpec plugin | `gem 'rspec_trunk_flaky_tests', '~> 0.17.0'` |

The `trunk` CLI passes a stored API key only to 1.0 and later, so a pinned 0.17.x needs the organization token in `TRUNK_API_TOKEN`.
