PHP code example of ondram / ci-detector
1. Go to this page and download the library: Download ondram/ci-detector library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ondram / ci-detector example snippets
$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';
}