ondram/ci-detector

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

3.5.1 2020-09-04 11:21 UTC

This package is auto-updated.

Last update: 2020-09-08 13:06:16 UTC


README

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

PHP library to detect continuous integration environment and to provide a unified interface to read the build information.

The detection is based on environment variables injected to the build environment by each of CI 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:

However not all information are provided by some CI servers. See table below.

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
    echo 'You are running this script on CI server!';
    $ci = $ciDetector->detect();    // Returns class implementing CiInterface or throws CiNotDetectedException

    // Example output when run in Travis:
    echo $ci->getCiName();                 // "Travis CI" - returns value of one of CiDetector::CI_* constants.
    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.)

    // Conditional code for pull request:
    if ($ci->isPullRequest()->yes()) {
        echo 'This is pull request';
    }

    // Conditional code for specific CI server:
    if ($ci->getCiName() === OndraM\CiDetector\CiDetector::CI_JENKINS) {
        echo 'Current CI server is Jenkins';
    }
} else {
    echo 'This script is not run on CI server';
}

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 (❌).

CI server Constant of CiDetector is​PullRequest get​Git​Branch get​Repository​Name get​Repository​Url get​Build​Url
AppVeyor CI_APPVEYOR
AWS CodeBuild CI_AWS_CODEBUILD
Bamboo CI_BAMBOO
Bitbucket Pipelines CI_BITBUCKET_PIPELINES
Buddy CI_BUDDY
CircleCI CI_CIRCLE
Codeship CI_CODESHIP
continuousphp CI_CONTINUOUSPHP
drone CI_DRONE
GitHub Actions CI_GITHUB_ACTIONS
GitLab CI_GITLAB
Jenkins CI_JENKINS
TeamCity CI_TEAMCITY
Travis CI CI_TRAVIS
Wercker CI_WERCKER

Testing

Check codestyle, static analysis and run unit-tests:

composer all

To automatically fix codestyle violations run:

composer fix

Similar libraries for other languages

Similar "CI Info" libraries exists for some other languages, for example: