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

# Azure Pipelines

> Set up Azure Pipelines workflow for coverage reporting.

## Installation

Before using CodeAnt Coverage Upload in your Azure Pipelines, you need to install the CodeAnt extension from the Azure DevOps Marketplace:

1. Go to the [Azure DevOps Marketplace](https://marketplace.visualstudio.com/) and search for "CodeAnt AI" or directly download from [here](https://marketplace.visualstudio.com/items?itemName=codeantai.codeant-azure-devops-extension)
2. Click **Get it free**
3. Select your Azure DevOps organization
4. Click **Install**

Once installed, the `CodeAntCoverageUpload@1` task will be available in all pipelines across your organization.

***

## Video Tutorial

Watch this video to learn how to integrate CodeAnt AI into your CI/CD pipelines:

<iframe src="https://www.tella.tv/video/integrate-code-ai-into-ci-cd-pipelines-1rev/embed" width="100%" height="400" frameBorder="0" allowFullScreen />

***

## Repository Scenarios

CodeAnt Coverage Upload supports multiple repository configurations in Azure DevOps:

| Scenario       | Repository Location                               | Pipeline Location         | Configuration                                                                                                        |
| -------------- | ------------------------------------------------- | ------------------------- | -------------------------------------------------------------------------------------------------------------------- |
| **Scenario 1** | Azure Repos                                       | Azure DevOps              | Auto-detected (default)                                                                                              |
| **Scenario 2** | Azure Repos (TFS / Azure DevOps Server – on-prem) | Azure DevOps Server (TFS) | Requires explicit `platform`, `repo`, `commitId`, and `baseUrl` inputs                                               |
| **Scenario 3** | GitHub                                            | Azure DevOps              | Requires `platform`, `baseUrl`, and `repo` inputs. `commitId` auto-detected from `BUILD_SOURCEVERSION` if available. |
| **Scenario 4** | GitLab                                            | Azure DevOps              | Requires `platform`, `baseUrl`, and `repo` inputs. `commitId` auto-detected from `BUILD_SOURCEVERSION` if available. |
| **Scenario 5** | Bitbucket                                         | Azure DevOps              | Requires `platform`, `baseUrl`, and `repo` inputs. `commitId` auto-detected from `BUILD_SOURCEVERSION` if available. |

***

## Scenario 1: Azure Repos + Azure DevOps Pipeline

When your repository is hosted in Azure Repos:

```yaml theme={null}
- task: CodeAntCoverageUpload@1
  inputs:
    accessToken: $(AZURE_DEVOPS_PERSONAL_ACCESS_TOKEN)
    coverageFile: 'coverage.xml'
  displayName: 'Upload Coverage to CodeAnt'
```

> **Note:** No additional configuration needed. The task automatically extracts information from these Azure DevOps built-in variables:
>
> * `SYSTEM_TEAMPROJECT` - Project name
> * `BUILD_REPOSITORY_NAME` - Repository name
> * `BUILD_SOURCEVERSION` - Commit SHA

***

## Scenario 2: Azure Repos (TFS / Azure DevOps Server – on-prem) + Azure DevOps Server (TFS) Pipeline

When your repository is hosted in an on-premise Azure DevOps Server (formerly known as Team Foundation Server / TFS), you need to explicitly pass all required parameters including the base URL.

```yaml theme={null}
- task: CodeAntCoverageUpload@1
  inputs:
    accessToken: $(AZURE_DEVOPS_PERSONAL_ACCESS_TOKEN)
    platform: 'azuredevops'
    baseUrl: 'https://tfs.example.com:8443/tfs'
    repo: 'MyCollection/MyProject/MyRepository'
    commitId: '$(Build.SourceVersion)'
    coverageFile: 'coverage.xml'
  displayName: 'Upload Coverage to CodeAnt'
```

### Parameters for Azure DevOps Server (TFS)

| Parameter  | Value                              | Description                                                    |
| ---------- | ---------------------------------- | -------------------------------------------------------------- |
| `platform` | `azuredevops`                      | Specifies Azure DevOps as the repository provider              |
| `baseUrl`  | `https://tfs.example.com:8443/tfs` | Base URL of your on-premise Azure DevOps Server / TFS instance |
| `repo`     | `collection/project/repo`          | Repository in `collection/project/repository-name` format      |
| `commitId` | `$(Build.SourceVersion)`           | Commit SHA to analyze                                          |

> **Note:** For on-premise Azure DevOps Server / TFS deployments, all parameters must be explicitly provided as environment variables may differ from Azure DevOps Cloud.

***

## Scenario 3: GitHub Repository + Azure DevOps Pipeline

```yaml theme={null}
- task: CodeAntCoverageUpload@1
  inputs:
    accessToken: $(ACCESS_TOKEN_GITHUB)
    platform: 'github'
    baseUrl: 'https://github.com'
    repo: 'myorg/my-repo'
    commitId: '$(Build.SourceVersion)'
    coverageFile: 'coverage/cobertura-coverage.xml'
  displayName: 'Upload Coverage to CodeAnt'
```

### Parameters for GitHub

| Parameter  | Value                    | Description                                                                                       |
| ---------- | ------------------------ | ------------------------------------------------------------------------------------------------- |
| `platform` | `github`                 | Specifies GitHub as the repository provider                                                       |
| `baseUrl`  | `https://github.com`     | Base URL of the GitHub platform                                                                   |
| `repo`     | `owner/repo`             | Repository in `owner/repository-name` format                                                      |
| `commitId` | `$(Build.SourceVersion)` | Commit SHA to analyze. Auto-detected from `BUILD_SOURCEVERSION` if available, otherwise required. |

***

## Scenario 4: GitLab Repository + Azure DevOps Pipeline

```yaml theme={null}
- task: CodeAntCoverageUpload@1
  inputs:
    accessToken: $(ACCESS_TOKEN_GITLAB)
    platform: 'gitlab'
    baseUrl: 'https://gitlab.com'
    repo: 'mygroup/my-project'
    commitId: '$(Build.SourceVersion)'
    coverageFile: 'coverage.xml'
  displayName: 'Upload Coverage to CodeAnt'
```

### Parameters for GitLab

| Parameter  | Value                    | Description                                                                                       |
| ---------- | ------------------------ | ------------------------------------------------------------------------------------------------- |
| `platform` | `gitlab`                 | Specifies GitLab as the repository provider                                                       |
| `baseUrl`  | `https://gitlab.com`     | Base URL of the GitLab platform                                                                   |
| `repo`     | `group/project`          | Repository in `group/project-name` or `user/project-name` format                                  |
| `commitId` | `$(Build.SourceVersion)` | Commit SHA to analyze. Auto-detected from `BUILD_SOURCEVERSION` if available, otherwise required. |

***

## Scenario 5: Bitbucket Repository + Azure DevOps Pipeline

```yaml theme={null}
- task: CodeAntCoverageUpload@1
  inputs:
    accessToken: $(BITBUCKET_ACCESS_TOKEN)
    platform: 'bitbucket'
    baseUrl: 'https://bitbucket.org'
    repo: 'myworkspace/my-repo'
    commitId: '$(Build.SourceVersion)'
    coverageFile: 'coverage.xml'
  displayName: 'Upload Coverage to CodeAnt'
```

### Parameters for Bitbucket

| Parameter  | Value                    | Description                                                                                       |
| ---------- | ------------------------ | ------------------------------------------------------------------------------------------------- |
| `platform` | `bitbucket`              | Specifies Bitbucket as the repository provider                                                    |
| `baseUrl`  | `https://bitbucket.org`  | Base URL of the Bitbucket platform                                                                |
| `repo`     | `workspace/repo`         | Repository in `workspace/repository-name` format                                                  |
| `commitId` | `$(Build.SourceVersion)` | Commit SHA to analyze. Auto-detected from `BUILD_SOURCEVERSION` if available, otherwise required. |

***

## Task Parameters Reference

### Required Parameters

| Parameter      | Description                                                                                                                                                                       |
| -------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `accessToken`  | Azure DevOps Personal Access Token (PAT): `AZURE_DEVOPS_PERSONAL_ACCESS_TOKEN`, GitHub: `ACCESS_TOKEN_GITHUB`, GitLab: `ACCESS_TOKEN_GITLAB`, Bitbucket: `BITBUCKET_ACCESS_TOKEN` |
| `coverageFile` | Path to the coverage file (e.g., `coverage.xml`, `coverage/cobertura-coverage.xml`)                                                                                               |

### Optional Parameters

| Parameter    | Default                  | Description                                                   |
| ------------ | ------------------------ | ------------------------------------------------------------- |
| `platform`   | `azuredevops`            | Platform type: `github`, `gitlab`, `bitbucket`, `azuredevops` |
| `baseUrl`    | *(auto-detected)*        | Base URL for the platform (e.g., `https://github.com`)        |
| `repo`       | *(auto-detected)*        | Repository name in `owner/repo` format                        |
| `commitId`   | *(auto-detected)*        | Commit SHA to analyze                                         |
| `module`     | *(empty)*                | Name of the module in a monorepo                              |
| `modulePath` | *(empty)*                | Relative path to the module from repository root              |
| `apiBase`    | `https://api.codeant.ai` | Base URL for CodeAnt API (for on-premise deployments)         |

***

## Azure DevOps Pipelines Workflow

This pipeline triggers on pushes to the specified branch and performs the following steps:

1. Runs tests and generates a coverage report
2. Uploads the coverage XML to CodeAnt AI

```yaml theme={null}
trigger:
  branches:
    include:
      - main

pool:
  vmImage: 'ubuntu-latest'

steps:
  - checkout: self

  # Run tests and generate coverage
  - script: |
      python -m venv .venv
      source .venv/bin/activate
      pip install --upgrade pip
      pip install coverage pytest

      coverage run -m pytest tests/
      coverage xml -o coverage.xml
    displayName: 'Run tests & generate coverage'

  # Upload coverage to CodeAnt
  - task: CodeAntCoverageUpload@1
    inputs:
      accessToken: $(AZURE_DEVOPS_PERSONAL_ACCESS_TOKEN)
      coverageFile: 'coverage.xml'
    displayName: 'Upload Coverage to CodeAnt'
```

### With Optional Parameters

You can customize the coverage upload with additional parameters:

```yaml theme={null}
steps:
  - checkout: self

  # Run tests and generate coverage
  - script: |
      npm install
      npm test -- --coverage
    displayName: 'Run tests'

  # Upload coverage to CodeAnt
  - task: CodeAntCoverageUpload@1
    inputs:
      accessToken: $(AZURE_DEVOPS_PERSONAL_ACCESS_TOKEN)
      coverageFile: 'coverage/coverage.xml'
    displayName: 'Upload Coverage to CodeAnt'
```

### Monorepo Support

For monorepo projects, you can specify the `module` and `modulePath` parameters to track coverage for individual modules:

```yaml theme={null}
steps:
  - checkout: self

  # Run tests and generate coverage for a specific module
  - script: |
      cd packages/my-module
      npm install
      npm test -- --coverage
    displayName: 'Run tests for my-module'

  # Upload coverage to CodeAnt with module information
  - task: CodeAntCoverageUpload@1
    inputs:
      accessToken: $(AZURE_DEVOPS_PERSONAL_ACCESS_TOKEN)
      coverageFile: 'packages/my-module/coverage.xml'
      module: 'my-module-name'
      modulePath: 'packages/my-module'
    displayName: 'Upload Coverage to CodeAnt'
```

#### Monorepo Parameters

| Parameter    | Description                                      | Example              |
| ------------ | ------------------------------------------------ | -------------------- |
| `module`     | Name of the module in the monorepo               | `my-module-name`     |
| `modulePath` | Relative path to the module from repository root | `packages/my-module` |

#### Multiple Modules Example

For monorepos with multiple modules, you can upload coverage for each module separately:

```yaml theme={null}
steps:
  - checkout: self

  # Module 1: Backend
  - script: |
      cd packages/backend
      npm install
      npm test -- --coverage
    displayName: 'Run backend tests'

  - task: CodeAntCoverageUpload@1
    inputs:
      accessToken: $(AZURE_DEVOPS_PERSONAL_ACCESS_TOKEN)
      coverageFile: 'packages/backend/coverage.xml'
      module: 'backend'
      modulePath: 'packages/backend'
    displayName: 'Upload Backend Coverage'

  # Module 2: Frontend
  - script: |
      cd packages/frontend
      npm install
      npm test -- --coverage
    displayName: 'Run frontend tests'

  - task: CodeAntCoverageUpload@1
    inputs:
      accessToken: $(AZURE_DEVOPS_PERSONAL_ACCESS_TOKEN)
      coverageFile: 'packages/frontend/coverage.xml'
      module: 'frontend'
      modulePath: 'packages/frontend'
    displayName: 'Upload Frontend Coverage'
```

> **Important:**
>
> * In **Project → Pipelines → Library**, add a **secret variable** named `AZURE_DEVOPS_PERSONAL_ACCESS_TOKEN` with your Azure DevOps Personal Access Token (PAT).

***

## How it works

1. **Setup environment**
   The task automatically extracts organization, project, and repository information from Azure DevOps built-in variables.

2. **Download script**
   The task fetches the coverage upload script (`upload_coverage.sh`) from the CodeAnt API endpoint.

3. **Upload coverage**
   The script uploads your coverage report to CodeAnt AI for analysis and tracking.

4. **Pipeline feedback**
   * **Success**: Coverage uploaded successfully
   * **Failure**: Upload fails due to authentication or file issues

***

## Coverage Formats Supported

> **Important:** Only XML format is supported for coverage reports.

* **Cobertura XML** (most common, generated by `coverage xml`)
* **JaCoCo XML** (Java projects)

***

## Coverage Config File

You have to create a `.coveragerc` file in the project's root folder to include all the source files in the test coverage calculation.

### Python Example

```ini theme={null}
[run]
# include every Python file under the repo root
source = .

# exclude tests, virtualenvs, build artifacts, etc.
omit =
    */tests/*
    */.venv/*
    */build/*
    */dist/*
```

When you assign source to "." , It checks for every python file in the root folder and its sub directories. You can omit some directories by placing them in the omit section of the file.

## How it works

With the above configuration:

1. coverage run -m pytest tests/ will count every .py under the workspace as "valid" lines except for those in the omitted directories.
2. Lines actually executed by your tests are marked "covered."
3. coverage xml -o coverage.xml produces a Cobertura-style report reflecting true coverage over the entire codebase.
4. Using this coverage xml, we calculate the coverage percentage and the status check will be done on every new push to the branch.

***

## Windows Runner Configuration

If you're using Windows runner (`windows-latest`) instead of Linux runner, make the following change:

### Change Required

**Update pool configuration:**

```yaml theme={null}
pool:
  vmImage: 'windows-latest'  # Change from 'ubuntu-latest'
```

### Key Difference

| Configuration | Linux Runner               | Windows Runner              |
| ------------- | -------------------------- | --------------------------- |
| Pool Image    | `vmImage: 'ubuntu-latest'` | `vmImage: 'windows-latest'` |

***

## On-Premise Deployment

If you are using a self-hosted CodeAnt instance, you can specify a custom API endpoint using the `apiBase` parameter:

```yaml theme={null}
- task: CodeAntCoverageUpload@1
  inputs:
    accessToken: $(AZURE_DEVOPS_PERSONAL_ACCESS_TOKEN)
    apiBase: 'https://your-codeant-instance.example.com'
    coverageFile: 'coverage.xml'
  displayName: 'Upload Coverage to CodeAnt (On-Premise)'
```

> **Note:** The `apiBase` parameter is only required for on-premise deployments. Cloud users do not need to configure this.
