|
1 | | -# codacy-cli-v2 |
| 1 | +# Codacy CLI v2 |
2 | 2 |
|
3 | | -The Codacy CLI (version 2) is a command-line tool that supports analyzing code using tools like ESLint and uploading the results in SARIF format to Codacy. |
4 | | -The tool is invoked using the `codacy-cli` command, and provides two main commands: analyze and upload. |
| 3 | +[](https://app.codacy.com/gh/codacy/codacy-cli-v2/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade) [](https://app.codacy.com/gh/codacy/codacy-cli-v2/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_coverage) |
5 | 4 |
|
6 | | -### Commands |
7 | 5 |
|
8 | | -- **`analyze` Command**: Runs ESLint analysis on the codebase. |
9 | | - - `--output, -o`: Output file for the results. |
10 | | - - `--tool, -t`: Specifies the tool to run analysis with (e.g., ESLint). |
11 | | - - `--format`: Output format (use 'sarif' for SARIF format to terminal). |
12 | | - - `--fix`: Automatically fixes issues when possible. |
| 6 | +Codacy CLI (version 2) is a command-line tool for running code analysis and integrating with Codacy. If your repository exists in Codacy, you can sync your configuration to ensure consistency with your organization's standards. |
13 | 7 |
|
14 | | -- **`upload` Command With Project Token**: Uploads a SARIF file containing analysis results to Codacy. |
15 | | - - `--sarif-path, -s`: Path to the SARIF report. |
16 | | - - `--commit-uuid, -c`: Commit UUID. |
17 | | - - `--project-token, -t`: Project token for Codacy API. |
| 8 | +You can also use Codacy CLI for local code analysis without a Codacy account, leveraging the linter configuration files found in your project's root or Codacy's suggested defaults. |
18 | 9 |
|
19 | | -- **`upload` Command With API Token**: Uploads a SARIF file containing analysis results to Codacy. |
20 | | - - `--sarif-path, -s`: Path to the SARIF report. |
21 | | - - `--commit-uuid, -c`: Commit UUID. |
22 | | - - `--api-token, -a`: User level token for Codacy API. |
23 | | - - `--provider, -p`: Provider name (e.g., gh, gl, bb). |
24 | | - - `--owner, -o`: Repository owner. |
25 | | - - `--repository, -r`: Repository name. |
| 10 | +The CLI supports uploading analysis results (in [SARIF](https://docs.oasis-open.org/sarif/sarif/v2.1.0/sarif-v2.1.0.html) format) to Codacy as well. |
26 | 11 |
|
27 | | -### Important Concepts |
| 12 | +It is invoked using the `codacy-cli` command and provides several commands for project setup, analysis, and integration. |
28 | 13 |
|
29 | | -- **`.codacy/codacy.yaml`**: Configuration file to specify runtimes and tools versions for the CLI. |
30 | | - ```yaml |
31 | | - runtimes: |
32 | | - |
33 | | - tools: |
34 | | - |
35 | | - |
36 | | -- **`codacy-cli install`**: Command to install the specified node and eslint versions before running analysis. |
| 14 | +--- |
37 | 15 |
|
38 | | -## Download |
| 16 | +## Supported Platforms |
39 | 17 |
|
40 | | -### MacOS (brew) |
| 18 | +- **macOS** |
| 19 | +- **Linux** |
| 20 | +- **Windows (via WSL only)** |
41 | 21 |
|
42 | | -To install `codacy-cli` using Homebrew: |
| 22 | +> **Note:** Native Windows is not yet supported. For Windows, use [Windows Subsystem for Linux (WSL)](https://learn.microsoft.com/en-us/windows/wsl/) and follow the Linux instructions inside your WSL terminal. |
| 23 | +
|
| 24 | +--- |
| 25 | + |
| 26 | +## Installation |
| 27 | + |
| 28 | +### macOS (Homebrew) |
43 | 29 |
|
44 | 30 | ```bash |
45 | 31 | brew install codacy/codacy-cli-v2/codacy-cli-v2 |
46 | 32 | ``` |
47 | 33 |
|
48 | | -### Linux |
| 34 | +### Linux / Windows (WSL) |
49 | 35 |
|
50 | | -For Linux, we rely on the codacy-cli.sh script in the root. To download the CLI, run: |
| 36 | +Run the following in your terminal (Linux or WSL): |
51 | 37 |
|
52 | 38 | ```bash |
53 | 39 | bash <(curl -Ls https://raw.githubusercontent.com/codacy/codacy-cli-v2/main/codacy-cli.sh) |
54 | 40 | ``` |
55 | | -You can either put the downloaded script in a specific file or create an alias that will download the script and look for changes: |
| 41 | + |
| 42 | +Or create an alias for convenience: |
56 | 43 |
|
57 | 44 | ```bash |
58 | 45 | alias codacy-cli="bash <(curl -Ls https://raw.githubusercontent.com/codacy/codacy-cli-v2/main/codacy-cli.sh)" |
59 | 46 | ``` |
60 | 47 |
|
61 | | -## Installation |
| 48 | +--- |
| 49 | + |
| 50 | +## CLI Commands |
| 51 | + |
| 52 | +### `init` — Bootstrap Project Configuration |
| 53 | + |
| 54 | +Bootstraps the CLI configuration in your project's folder. This command creates a `.codacy` directory containing a `codacy.yaml` file, which specifies the runtimes and tools that will be used and installed. |
| 55 | + |
| 56 | +- If you provide Codacy repository information (API token, provider, organization, repository), the configuration will be fetched from Codacy, ensuring consistency with your organization's standards. |
| 57 | +- If no Codacy information is provided, all available tools will be included. For each tool, if a local configuration file exists in your project, it will be used; otherwise, Codacy's suggested defaults will be used. |
| 58 | + |
| 59 | +- **Local mode (local configuration files):** |
| 60 | + ```bash |
| 61 | + codacy-cli init |
| 62 | + ``` |
| 63 | +- **Remote mode (fetch configuration from Codacy):** |
| 64 | + ```bash |
| 65 | + codacy-cli init --api-token <token> --provider <gh|gl|bb> --organization <org> --repository <repo> |
| 66 | + ``` |
| 67 | + |
| 68 | +**Flags:** |
| 69 | +- `--api-token` (string): Codacy API token (optional; enables fetching remote config) |
| 70 | +- `--provider` (string): Provider (`gh`, `gl`, `bb`), required with `--api-token` |
| 71 | +- `--organization` (string): Organization name, required with `--api-token` |
| 72 | +- `--repository` (string): Repository name, required with `--api-token` |
| 73 | + |
| 74 | +### `install` — Install Runtimes and Tools |
62 | 75 |
|
63 | | -Before running the analysis, install the specified tools: |
| 76 | +Installs all runtimes and tools specified in `.codacy/codacy.yaml`: |
| 77 | +- Downloads and extracts runtimes (Node, Python, Dart, Java, etc.) |
| 78 | +- Installs tools (ESLint, Trivy, Pylint, PMD, etc.) using the correct package manager or direct download |
| 79 | +- Handles platform-specific details |
| 80 | +- Skips already installed components (e.g., Node, Python, Dart, Java, etc.) |
| 81 | +- Shows a progress bar and reports any failures |
64 | 82 |
|
65 | 83 | ```bash |
66 | 84 | codacy-cli install |
67 | 85 | ``` |
| 86 | +- Optionally specify a custom registry: |
| 87 | + ```bash |
| 88 | + codacy-cli install --registry <url> |
| 89 | + ``` |
68 | 90 |
|
69 | | -## Run Analysis |
| 91 | +### `analyze` — Run Code Analysis |
70 | 92 |
|
71 | | -To run ESLint and output the results to the terminal: |
| 93 | +Runs all configured tools, or a specific tool, on your codebase. |
72 | 94 |
|
73 | 95 | ```bash |
| 96 | +# Run all tools |
| 97 | +codacy-cli analyze |
| 98 | + |
| 99 | +# Run a specific tool (e.g., ESLint) |
74 | 100 | codacy-cli analyze --tool eslint |
| 101 | + |
| 102 | +# Output results in SARIF format |
| 103 | +codacy-cli analyze --tool eslint --format sarif |
| 104 | + |
| 105 | +# Store results as SARIF in a file |
| 106 | +codacy-cli analyze -t eslint --format sarif -o eslint.sarif |
| 107 | + |
| 108 | +# Analyze a specific file with all configured tools |
| 109 | +codacy-cli analyze path/to/file.js |
| 110 | + |
| 111 | +# Analyze a specific file with a specific tool (e.g., ESLint) |
| 112 | +codacy-cli analyze --tool eslint path/to/file.js |
75 | 113 | ``` |
76 | 114 |
|
77 | | -To output results in SARIF format to the terminal: |
| 115 | +**Flags:** |
| 116 | +- `--output, -o`: Output file for the results; if not provided, results will be printed to the console |
| 117 | +- `--tool, -t`: Tool to run analysis with (e.g., eslint) |
| 118 | +- `--format`: Output format (e.g., `sarif`) |
| 119 | +- `--fix`: Automatically fix issues when possible |
| 120 | + |
| 121 | +### `upload` — Upload SARIF Results to Codacy |
| 122 | + |
| 123 | +Uploads a SARIF file containing analysis results to Codacy. |
78 | 124 |
|
79 | 125 | ```bash |
80 | | -codacy-cli analyze --tool eslint --format sarif |
| 126 | +# With project token |
| 127 | +codacy-cli upload -s path/to/your.sarif -c <commit-uuid> -t <project-token> |
| 128 | + |
| 129 | +# With API token |
| 130 | +codacy-cli upload -s path/to/your.sarif -c <commit-uuid> -a <api-token> -p <provider> -o <owner> -r <repository> |
81 | 131 | ``` |
82 | 132 |
|
83 | | -To store the results as SARIF in a file: |
| 133 | +**Flags:** |
| 134 | +- `--sarif-path, -s`: Path to the SARIF report (required) |
| 135 | +- `--commit-uuid, -c`: Commit UUID |
| 136 | +- `--project-token, -t`: Project token for Codacy API |
| 137 | +- `--api-token, -a`: User-level token for Codacy API |
| 138 | +- `--provider, -p`: Provider name (e.g., gh, gl, bb) |
| 139 | +- `--owner, -o`: Repository owner |
| 140 | +- `--repository, -r`: Repository name |
| 141 | + |
| 142 | +### `update` — Update the CLI |
| 143 | + |
| 144 | +Fetches and installs the latest version of the CLI. |
84 | 145 |
|
85 | 146 | ```bash |
86 | | -codacy-cli analyze -t eslint --format sarif -o eslint.sarif |
| 147 | +codacy-cli update |
87 | 148 | ``` |
88 | 149 |
|
89 | | -## Upload Results |
| 150 | +### `version` — Show Version Information |
90 | 151 |
|
91 | | -To upload a SARIF file to Codacy: |
| 152 | +Displays detailed version/build information for the CLI. |
92 | 153 |
|
93 | 154 | ```bash |
94 | | -codacy-cli upload -s path/to/your.sarif -c your-commit-uuid -t your-project-token |
| 155 | +codacy-cli version |
95 | 156 | ``` |
96 | 157 |
|
97 | | -## Breaking Changes |
| 158 | +--- |
| 159 | + |
| 160 | +## Configuration |
| 161 | + |
| 162 | +- **`.codacy/codacy.yaml`**: Main configuration file specifying runtimes and tool versions. |
| 163 | +- **`.codacy/tools-configs/`**: Tool-specific configuration files (auto-generated or fetched from Codacy). |
| 164 | + |
| 165 | +--- |
98 | 166 |
|
99 | | -Some behaviors have changed with the new updates of the CLI. To rely on a specific version, you can add that version to your environment: |
| 167 | +## Example Usage |
100 | 168 |
|
| 169 | +```bash |
| 170 | +# 1. Initialize project (local or remote) |
| 171 | +codacy-cli init |
| 172 | +# or |
| 173 | +codacy-cli init --api-token <token> --provider gh --organization my-org --repository my-repo |
| 174 | + |
| 175 | +# 2. Install all required runtimes and tools |
| 176 | +codacy-cli install |
| 177 | + |
| 178 | +# 3. Run analysis (all tools or specific tool) |
| 179 | +codacy-cli analyze |
| 180 | +codacy-cli analyze --tool eslint |
| 181 | + |
| 182 | +# 4. Upload results to Codacy |
| 183 | +codacy-cli upload -s eslint.sarif -c <commit-uuid> -t <project-token> |
101 | 184 | ``` |
102 | | -export CODACY_CLI_V2_VERSION="1.0.0-main.133.3607792" |
| 185 | + |
| 186 | +--- |
| 187 | + |
| 188 | +## Troubleshooting |
| 189 | + |
| 190 | +### WSL (Windows Subsystem for Linux) |
| 191 | +- **Always use a WSL terminal** (e.g., Ubuntu on Windows) for all commands. |
| 192 | +- Ensure you have the latest version of WSL and a supported Linux distribution installed. |
| 193 | +- If you see errors related to missing Linux tools (e.g., `curl`, `tar`), install them using your WSL package manager (e.g., `sudo apt install curl tar`). |
| 194 | + |
| 195 | +### MacOS: Errors related to `docker-credential-osxkeychain` not found |
| 196 | + |
| 197 | +Install the docker credential helper: |
| 198 | + |
| 199 | +```bash |
| 200 | +brew install docker-credential-helper |
103 | 201 | ``` |
104 | 202 |
|
| 203 | +--- |
| 204 | + |
105 | 205 | ## Example Repository |
106 | 206 |
|
107 | | -As an example, you can check https://github.com/troubleshoot-codacy/eslint-test-examples for a repository that has an action relying on this CLI. |
| 207 | +See [eslint-test-examples](https://github.com/troubleshoot-codacy/eslint-test-examples) for a repository using this CLI in GitHub Actions. |
108 | 208 |
|
109 | | -## Troubleshooting |
| 209 | +--- |
110 | 210 |
|
111 | | -#### Errors related to `docker-credential-osxkeychain` not found when running analysis |
| 211 | +## Breaking Changes & Version Pinning |
112 | 212 |
|
113 | | -Install the docker credential helper. For example, in MacOS: |
| 213 | +Some behaviors have changed with recent updates. To rely on a specific CLI version, set the following environment variable: |
114 | 214 |
|
115 | 215 | ```bash |
116 | | -brew install docker-credential-helper |
| 216 | +export CODACY_CLI_V2_VERSION="1.0.0-main.133.3607792" |
117 | 217 | ``` |
| 218 | + |
| 219 | +Check the [releases](https://github.com/codacy/codacy-cli-v2/releases) page for all available versions. |
| 220 | + |
| 221 | +--- |
0 commit comments