A zero-configuration commit message linting CLI tool specifically designed for Travis CI environments. It provides intelligent commit range detection and seamless integration with Travis CI build processes, automatically determining which commits to lint based on the build context (pull request vs single commit).
npm install --save-dev @commitlint/travis-cliThis package provides a CLI binary rather than programmatic imports:
# Binary command available after installation
commitlint-travisAdd to your Travis CI configuration:
# .travis.yml
script:
- commitlint-travisThe tool automatically detects the appropriate commit range based on Travis CI environment variables and passes any additional arguments to the underlying commitlint command:
# With additional commitlint options
commitlint-travis --config ./config/commitlint.config.js --verboseThe tool operates as a wrapper around @commitlint/cli with the following key components:
The primary interface is the commitlint-travis binary command that automatically handles commit message linting in Travis CI environments.
commitlint-travis [commitlint-options...]Parameters:
commitlint-options: Any valid commitlint CLI options (passed through unchanged to underlying @commitlint/cli command)Dependencies:
@commitlint/cli: The underlying commitlint command-line interfacetinyexec: Used for spawning child processesnode:child_process, node:moduleEnvironment Requirements:
CI="true" - Must be running in CI environmentTRAVIS="true" - Must be running on Travis CITRAVIS_COMMIT - Current commit hash being builtTRAVIS_COMMIT_RANGE - Range of commits for pull requestsTRAVIS_EVENT_TYPE - Build event type (e.g., "pull_request")TRAVIS_REPO_SLUG - Repository slug (owner/repo)TRAVIS_PULL_REQUEST_SLUG - Pull request source repository slugExecution Sequence:
git stash -k -u --quiet (only if working directory is not clean)git remote add base https://github.com/${TRAVIS_REPO_SLUG}.gitgit remote add source https://github.com/${TRAVIS_PULL_REQUEST_SLUG}.gitgit fetch --quietgit stash pop --quietBehavior Modes:
TRAVIS_EVENT_TYPE === "pull_request" and TRAVIS_COMMIT_RANGE is available, parses the range (format: "commit1.commit2") and lints all commits using commitlint --from <start> --to <end>git log -n 1 --pretty=format:%B <commit> and pipes it to commitlint for validationUsage Example:
# Basic usage (in .travis.yml)
script:
- commitlint-travis
# With custom configuration
script:
- commitlint-travis --config ./custom-commitlint.config.js
# With verbose output
script:
- commitlint-travis --verbose --help-url https://company.com/commit-guidelinesFor testing and customization purposes, the tool supports environment variable overrides:
# Override git binary path (primarily for testing)
TRAVIS_COMMITLINT_GIT_BIN=/path/to/git
# Override commitlint binary path (primarily for testing)
TRAVIS_COMMITLINT_BIN=/path/to/commitlintThe tool performs comprehensive validation and provides clear error messages:
Environment Validation Errors:
CI !== "true" or TRAVIS !== "true")
"@commitlint/travis-cli is intended to be used on Travis CI""Expected <VAR_NAME> to be defined globally, it was not.""Expected <VAR1>, <VAR2>, ... to be defined globally, they were not."Git Operation Errors:
git stash, git remote add, git fetch, git stash popCommitlint Execution Errors:
# .travis.yml - Basic setup
language: node_js
node_js:
- "18"
install:
- npm ci
script:
- npm test
- commitlint-travis
# For monorepos or complex configurations
script:
- commitlint-travis --config ./packages/lint-config/commitlint.config.js{
"devDependencies": {
"@commitlint/travis-cli": "^19.8.1",
"@commitlint/config-conventional": "^19.8.1"
},
"scripts": {
"lint:commits": "commitlint-travis"
}
}The tool works with any commitlint configuration:
// commitlint.config.js
module.exports = {
extends: ['@commitlint/config-conventional'],
rules: {
'type-enum': [2, 'always', ['feat', 'fix', 'docs', 'style', 'refactor', 'test', 'chore']],
'subject-max-length': [2, 'always', 72]
}
};