# GitLab CI/CD

> Upload JUnit test results from a GitLab pipeline to an Arcus test run so test case statuses update automatically.

Integrating GitLab with Arcus by Testsigma uploads your JUnit test results to a test run at the end of a pipeline, so test case statuses update automatically.

## Prerequisites

- A GitLab account with a connected project repository.
- A project in Arcus by Testsigma with at least one [test run](https://testsigma.com/docs/arcus/v2/run-tests/test-runs/).
- An Arcus by Testsigma [API token](https://testsigma.com/docs/arcus/v2/settings/api-keys/).
- Test results generated in JUnit XML format, for example at `reports/junit.xml`.

## Store the Testsigma values in GitLab

The GitLab pipeline reads the project and run IDs from variables rather than from the script itself.

1. Open your project in GitLab.

2. Go to **Settings > CI/CD > Variables** and click **Expand**.

3. Add these 3 variables:
   - `TESTSIGMA_API_TOKEN`: your Arcus by Testsigma API token.
   - `TESTSIGMA_PROJECT_ID`: your project ID, which appears in your project URL: `https://test-management.testsigma.com/ui/projects//`.
   - `TESTSIGMA_RUN_ID`: the run ID, which appears in the URL when you view a test run: `https://test-management.testsigma.com/ui/test_runs//`.

4. Click **Save changes**.

Keep `TESTSIGMA_API_TOKEN` out of the job log. Store it as a masked variable.

## Configure the pipeline

In your repository, create or update `.gitlab-ci.yml` in the root directory:

```yaml
stages:
  - upload

upload_testsigma:
  stage: upload
  image: curlimages/curl:8.10.1
  only:
    - main
  script:
    - 'set -eu'
    - 'echo "Looking for reports/junit.xml ..."'
    - 'test -f reports/junit.xml || { echo "File not found: reports/junit.xml"; exit 1; }'
    - 'echo "Uploading JUnit XML to Testsigma..."'
    - 'curl --fail --show-error --location --header "Authorization: Bearer ${TESTSIGMA_API_TOKEN}" --form "junit_xml=@reports/junit.xml" "https://arcus.testsigma.com/api/v1/projects/${TESTSIGMA_PROJECT_ID}/junit-import/test-run/${TESTSIGMA_RUN_ID}"'
    - 'echo "Upload complete."'
```

The pipeline does 3 things:

- Runs when you push to `main`.
- Checks that the JUnit XML file exists, and fails the job if it doesn't.
- Uploads the results to the project and test run named in your variables.

Change `reports/junit.xml` everywhere it appears if your build writes its report somewhere else.

## Trigger a build

Commit and push to the repository connected to GitLab. GitLab runs the pipeline, and when it finishes the test run in Arcus by Testsigma updates with the execution results.
