ondram / ci-detector
Detect continuous integration environment and provide unified access to properties of current build
Installs: 28 482 018
Dependents: 31
Suggesters: 0
Security: 0
Stars: 213
Watchers: 4
Forks: 12
Open Issues: 3
Requires
- php: ^7.4 || ^8.0
Requires (Dev)
- ergebnis/composer-normalize: ^2.13.2
- lmc/coding-standard: ^3.0.0
- php-parallel-lint/php-parallel-lint: ^1.2
- phpstan/extension-installer: ^1.1.0
- phpstan/phpstan: ^1.2.0
- phpstan/phpstan-phpunit: ^1.0.0
- phpunit/phpunit: ^9.6.13
README
PHP library to detect continuous integration environment and to read information of the current build.
Why
This library is useful if you need to detect whether some CLI script/tool is running in an automated environment (on a CI server). Based on that, your script may behave differently. For example, it could hide some information which relevant only for a real person - like a progress bar.
Additionally, you may want to detect some information about the current build: build ID, git commit, branch etc. For example, if you'd like to record these values to log, publish them to Slack, etc.
How
The detection is based on environment variables injected to the build environment by each CI server. However, these variables are named differently in each CI. This library contains adapters for many supported CI servers to handle these differences, so you can make your scripts (and especially CLI tools) portable to multiple build environments.
Supported continuous integration servers
These CI servers are currently recognized:
- AppVeyor
- AWS CodeBuild
- Azure DevOps Pipelines
- Bamboo
- Bitbucket Pipelines
- Buddy
- CircleCI
- Codeship
- continuousphp
- drone
- GitHub Actions
- GitLab
- Jenkins
- SourceHut
- TeamCity
- Travis CI
- Wercker
If your favorite CI server is missing, feel free to send a pull-request!
Installation
Install using Composer:
$ composer require ondram/ci-detector
Example usage
<?php $ciDetector = new \OndraM\CiDetector\CiDetector(); if ($ciDetector->isCiDetected()) { // Make sure we are on CI environment echo 'You are running this script on CI server!'; $ci = $ciDetector->detect(); // Returns class implementing CiInterface or throws CiNotDetectedException // Example output when run inside GitHub Actions build: echo $ci->getCiName(); // "GitHub Actions" echo $ci->getBuildNumber(); // "33" echo $ci->getBranch(); // "feature/foo-bar" or empty string if not detected // Conditional code for pull request: if ($ci->isPullRequest()->yes()) { echo 'This is pull request. The target branch is: '; echo $ci->getTargetBranch(); // "main" } // Conditional code for specific CI server: if ($ci->getCiName() === OndraM\CiDetector\CiDetector::CI_GITHUB_ACTIONS) { echo 'This is being built on GitHub Actions'; } // Describe all detected values in human-readable form: print_r($ci->describe()); // Array // ( // [ci-name] => GitHub Actions // [build-number] => 33 // [build-url] => https://github.com/OndraM/ci-detector/commit/abcd/checks // [commit] => fad3f7bdbf3515d1e9285b8aa80feeff74507bde // [branch] => feature/foo-bar // [target-branch] => main // [repository-name] => OndraM/ci-detector // [repository-url] => https://github.com/OndraM/ci-detector // [is-pull-request] => Yes // ) } else { echo 'This script is not run on CI server'; }
API methods reference
Available methods of CiInterface
instance (returned from $ciDetector->detect()
):
Supported properties of each CI server
Most CI servers support (✔) detection of all information. However some don't expose necessary environment variables, thus reading some information may be unsupported (❌).
Testing
Check codestyle, static analysis and run unit-tests:
composer all
To automatically fix codestyle violations run:
composer fix
Standalone CLI version
If you want to use CI Detector as a standalone CLI command (ie. without using inside code of PHP project), see ci-detector-standalone repository, where you can download CI Detector as a standalone PHAR file with simple command line interface.
Changelog
For latest changes see CHANGELOG.md file. This project follows Semantic Versioning.
Similar libraries for other languages
Similar "CI Info" libraries exists for some other languages, for example:
- Go - KlotzAndrew/ci-info
- JavaScript/Node.js - watson/ci-info
- Python - mgxd/ci-info
- Rust - sagiegurari/ci_info