Skip to content

Detect continuous integration server and provide unified access to properties of current build

License

Notifications You must be signed in to change notification settings

radimvaculik/ci-detector

 
 

Repository files navigation

CI Detector

Latest Stable Version Coverage Status GitHub Actions Build Status Travis Build Status AppVeyor Build Status

A simple PHP library to detect in what continuous integration server is the current script being run and to provide unified interface to read the build properties.

The detection is based on environment variables injected to the build environment by each of continuous integration server. However, these variables are named differently in each CI. This library contains adapter for each supported CI server, which handles these differences so you don't have to, and you can make your scripts (and especially CLI tools) portable for multiple build environments.

Supported continuous integration servers

These CI servers are currently recognized:

If your favorite CI server is missing, feel free to send a pull-request!

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.

Installation

Install using Composer:

$ composer require ondram/ci-detector

Ci-detector requires PHP 7.1+, but if you need compatibility with PHP <7.1, you can still use old ci-detector version 2.x.

Example usage

<?php

$ciDetector = new \OndraM\CiDetector\CiDetector();

if ($ciDetector->isCiDetected()) {  // Make sure we are on CI environment
    $ci = $ciDetector->detect();    // Returns class implementing CiInterface or throws CiNotDetectedException

    // Example output when run in Travis:
    echo $ci->getCiName();                 // "Travis CI"
    echo $ci->isPullRequest()->describe(); // "No" - also note yes(), no() and maybe() methods which returns boolean
    echo $ci->getBuildNumber();            // "35.1"
    echo $ci->getBuildUrl();               // "https://travis-ci.org/OndraM/ci-detector/jobs/148395137"
    echo $ci->getGitCommit();              // "fad3f7bdbf3515d1e9285b8aa80feeff74507bdd"
    echo $ci->getGitBranch();              // "feature/foo-bar"
    echo $ci->getRepositoryName();         // "OndraM/ci-detector"
    echo $ci->getRepositoryUrl();          // "" (empty string) - unsupported on Travis, will return eg. "ssh://git@gitserver:7999/project/repo.git" on Jenkins etc.)
} else {
    echo "CI not detected";
}

Testing

Check codestyle, static analysis and run unit-tests:

composer all

To automatically fix codestyle violations run:

composer fix

About

Detect continuous integration server and provide unified access to properties of current build

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%