Codefresh CLI
The `codefresh` package provides the official command-line interface (CLI) for interacting with the Codefresh platform, enabling users to manage CI/CD pipelines, deployments, and various resources directly from their terminal. It supports a wide array of operations including managing builds, images, tags, authentication contexts, and Helm releases. The current stable version is 1.1.3. Releases appear to be frequent, often weekly or bi-weekly, addressing bugs, security updates, and new features. Its primary differentiator is providing a comprehensive, scriptable interface to Codefresh's full suite of CI/CD capabilities, serving as a powerful tool for automation and integration within development workflows. It requires Node.js v24.0.0 or higher to run.
Common errors
-
codefresh: command not found
cause The Codefresh CLI is not installed globally or is not available in your system's PATH.fixInstall the CLI globally using npm: `npm install -g codefresh`. -
Error: API key is missing or invalid. Please generate one from your Codefresh account settings.
cause The provided API key during `codefresh auth create-context` is incorrect, expired, or was not set.fixGenerate a new API key from your Codefresh account settings and ensure it is correctly provided: `codefresh auth create-context --api-key {{YOUR_API_KEY}}`. -
Error: Minimum Node.js version 24.0.0 is required. Detected version X.Y.Z
cause You are attempting to run the Codefresh CLI with an unsupported, older version of Node.js.fixUpgrade your Node.js installation to version 24.0.0 or higher. Tools like `nvm` (Node Version Manager) can help manage multiple Node.js versions. -
Cannot find module 'codefresh' or 'codefresh/lib/some_module'
cause Attempting to `import` or `require` the `codefresh` package as a regular JavaScript library.fixThe `codefresh` package is a CLI tool. Do not import it directly. Instead, execute its commands via `child_process.spawn` or `execa` for programmatic interaction.
Warnings
- breaking The `codefresh` CLI now explicitly requires Node.js version 24.0.0 or higher. Running with older Node.js versions will result in an error and prevents the CLI from functioning.
- breaking Multiple security vulnerabilities were addressed in recent releases, including fixes for supply chain issues and updated dependencies. Older versions may contain known security flaws.
- gotcha The `codefresh` package is a command-line interface tool, not a JavaScript library. It is not designed for direct programmatic import (`import {} from 'codefresh'`). Its functionality is exposed via shell commands.
- gotcha Authentication with the Codefresh CLI relies on API keys. Storing API keys directly in source code or committing them to version control poses a significant security risk.
Install
-
npm install codefresh -
yarn add codefresh -
pnpm add codefresh
Imports
- CLI Execution
import { execa } from 'execa'; await execa('codefresh', ['version']); - Executable Path
import { fileURLToPath } from 'url'; import { dirname, join } from 'path'; const __dirname = dirname(fileURLToPath(import.meta.url)); const cliPath = join(__dirname, 'node_modules', '.bin', 'codefresh'); - Library Import (Discouraged)
import { getBuilds } from 'codefresh'; // This is not an exposed library API.
Quickstart
import { execa } from 'execa'; // npm install execa
async function setupAndUseCodefreshCLI() {
console.log('--- Installing Codefresh CLI globally ---\n');
try {
await execa('npm', ['install', '-g', 'codefresh'], { stdio: 'inherit' });
console.log('\nCodefresh CLI installed successfully.\n');
} catch (error) {
console.error('Failed to install Codefresh CLI:', error.message);
return;
}
const apiKey = process.env.CODEFRESH_API_KEY ?? '';
if (!apiKey) {
console.error('CODEFRESH_API_KEY environment variable is not set. Please generate an API key from Codefresh account settings and set it.\n');
return;
}
console.log('--- Authenticating with Codefresh CLI ---\n');
try {
await execa('codefresh', ['auth', 'create-context', '--api-key', apiKey], { stdio: 'inherit' });
console.log('\nAuthentication successful. Context created.\n');
} catch (error) {
console.error('Authentication failed. Check your API key and network connection:', error.message);
return;
}
console.log('--- Getting Codefresh CLI version ---\n');
try {
const { stdout } = await execa('codefresh', ['version']);
console.log(`Codefresh CLI Version: ${stdout.trim()}\n`);
} catch (error) {
console.error('Failed to get CLI version:', error.message);
}
console.log('--- Listing Codefresh contexts (example) ---\n');
try {
const { stdout } = await execa('codefresh', ['get', 'contexts']);
console.log('Codefresh Contexts:\n', stdout);
} catch (error) {
console.error('Failed to list contexts:', error.message);
}
}
setupAndUseCodefreshCLI();