2017 © Pedro Peláez
 

library ci-detector

image

ondram/ci-detector

  • Friday, June 1, 2018
  • by OndraM
  • Repository
  • 1 Watchers
  • 27 Stars
  • 41,381 Installations
  • PHP
  • 4 Dependents
  • 0 Suggesters
  • 1 Forks
  • 5 Open issues
  • 22 Versions
  • 13 % Grown

The README.md

CI Detector

Latest Stable Version Packagist Downloads Coverage Status GitHub Actions Build Status Travis Build Status AppVeyor Build Status, (*1)

PHP library to detect continuous integration environment and to read information of the current build., (*2)

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., (*3)

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., (*4)

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., (*5)

Supported continuous integration servers

These CI servers are currently recognized:, (*6)

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

Installation

Install using Composer:, (*8)

$ 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()):, (*9)

Method Example value Description
getCiName() GitHub Actions Name of the CI server.
The value is one of CiDetector::CI_* constants.
getBuildNumber() 33 Get number of this concrete build.
Build number is usually human-readable increasing number sequence. It should increase each time this particular job was run on the CI server. Most CIs use simple numbering sequence like: 1, 2, 3... However, some CIs do not provide this simple human-readable value and rather use for example alphanumeric hash.
getBuildUrl() https://github.com/OndraM/ci-detector/commit/abcd/checks
or empty string
Get URL where this build can be found and viewed or empty string if it cannot be determined.
getCommit() b9173d94(...) Get hash of the git (or other VCS) commit being built.
getBranch() my-feature
or empty string
Get name of the git (or other VCS) branch which is being built or empty string if it cannot be determined.
Use getTargetBranch() to get name of the branch where this branch is targeted.
getTargetBranch() main
or empty string
Get name of the target branch of a pull request or empty string if it cannot be determined.
This is the base branch to which the pull request is targeted.
getRepositoryName() OndraM/ci-detector
or empty string
Get name of the git (or other VCS) repository which is being built or empty string if it cannot be determined.
This is usually in form "user/repository", for example OndraM/ci-detector.
getRepositoryUrl() https://github.com/OndraM/ci-detector
or empty string
Get URL where the repository which is being built can be found or empty string if it cannot be determined.
This is either HTTP URL like https://github.com/OndraM/ci-detector but may be a git ssh url like ssh://git@bitbucket.org/OndraM/ci-detector
isPullRequest() TrinaryLogic instance Detect whether current build is from a pull/merge request.
Returned TrinaryLogic object's value will be true if the current build is from a pull/merge request, false if it not, and maybe if we can't determine it (see below for what CI supports PR detection).
Use condition like if ($ci->isPullRequest()->yes()) { /*...*/ } to use the value.
describe() [...]
(array of values)
Return key-value map of all detected properties in human-readable form.

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

CI server Constant of CiDetector is​PullRequest get​Branch get​Target​Branch get​Repository​Name get​Repository​Url get​Build​Url
AppVeyor CI_APPVEYOR
AWS CodeBuild CI_AWS_CODEBUILD
Azure Pipelines CI_AZURE_PIPELINES
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
SourceHut CI_SOURCEHUT
TeamCity CI_TEAMCITY
Travis CI CI_TRAVIS
Wercker CI_WERCKER

Testing

Check codestyle, static analysis and run unit-tests:, (*11)

composer all

To automatically fix codestyle violations run:, (*12)

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., (*13)

Changelog

For latest changes see CHANGELOG.md file. This project follows Semantic Versioning., (*14)

Similar libraries for other languages

Similar "CI Info" libraries exists for some other languages, for example:, (*15)

The Versions

01/06 2018

dev-test-travis

dev-test-travis

  Sources   Download

11/04 2018

dev-master

9999999-dev

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

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

by Ondřej Machulda

continuous integration adapter interface teamcity gitlab travis jenkins continuousphp bamboo circleci codeship appveyor drone

10/04 2018

dev-feature/update-deps

dev-feature/update-deps

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

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

by Ondřej Machulda

continuous integration adapter interface teamcity gitlab travis jenkins continuousphp bamboo circleci codeship appveyor drone

20/02 2018

3.1.0

3.1.0.0

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

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

by Ondřej Machulda

continuous integration adapter interface teamcity gitlab travis jenkins continuousphp bamboo circleci codeship appveyor drone

17/01 2018

dev-feature/composer-script

dev-feature/composer-script

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

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

by Ondřej Machulda

continuous integration adapter interface teamcity gitlab travis jenkins continuousphp bamboo circleci codeship appveyor drone

01/12 2017

dev-feature/php-7.2

dev-feature/php-7.2

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

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

by Ondřej Machulda

continuous integration adapter interface teamcity gitlab travis jenkins continuousphp bamboo circleci codeship appveyor

09/11 2017

dev-feature/drone

dev-feature/drone

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

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

by Ondřej Machulda

continuous integration adapter interface teamcity gitlab travis jenkins continuousphp bamboo circleci codeship appveyor drone

09/11 2017

dev-test-drone

dev-test-drone

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

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

by Ondřej Machulda

continuous integration adapter interface teamcity gitlab travis jenkins continuousphp bamboo circleci codeship appveyor

05/11 2017

dev-feature/continuousphp

dev-feature/continuousphp

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

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

by Ondřej Machulda

continuous integration adapter interface teamcity gitlab travis jenkins continuousphp bamboo circleci codeship appveyor

05/11 2017

dev-fix-circleci-php

dev-fix-circleci-php

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

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

by Ondřej Machulda

continuous integration adapter interface teamcity gitlab travis jenkins bamboo circleci codeship appveyor

27/10 2017

3.0.0

3.0.0.0

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

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

by Ondřej Machulda

continuous integration adapter interface teamcity gitlab travis jenkins bamboo circleci codeship appveyor

26/05 2017

2.1.0

2.1.0.0

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

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

The Development Requires

by Ondřej Machulda

continuous integration adapter interface teamcity gitlab travis jenkins bamboo circleci codeship appveyor

26/05 2017

dev-feature/appveyor

dev-feature/appveyor

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

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

The Development Requires

by Ondřej Machulda

continuous integration adapter interface teamcity gitlab travis jenkins bamboo circleci codeship appveyor

26/05 2017

dev-test-appveyor

dev-test-appveyor

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

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

The Development Requires

by Ondřej Machulda

continuous integration adapter interface teamcity gitlab travis jenkins bamboo circleci codeship

07/01 2017

2.0.0

2.0.0.0

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

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

The Development Requires

by Ondřej Machulda

continuous integration adapter interface teamcity gitlab travis jenkins bamboo circleci codeship

06/01 2017

1.1.0

1.1.0.0

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

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

The Development Requires

by Ondřej Machulda

continuous integration adapter interface teamcity gitlab travis jenkins bamboo circleci codeship

06/01 2017

dev-feature/detect-branch

dev-feature/detect-branch

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

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

The Development Requires

by Ondřej Machulda

continuous integration adapter interface teamcity gitlab travis jenkins bamboo circleci codeship

20/08 2016

1.0.0

1.0.0.0

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

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

The Development Requires

by Ondřej Machulda

continuous integration adapter interface teamcity gitlab travis jenkins bamboo circleci codeship

05/08 2016

0.3.0

0.3.0.0

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

  Sources   Download

MIT

The Development Requires

by Ondřej Machulda

continuous integration adapter interface teamcity travis jenkins bamboo circleci

05/08 2016

0.4.0

0.4.0.0

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

  Sources   Download

MIT

The Development Requires

by Ondřej Machulda

continuous integration adapter interface teamcity travis jenkins bamboo circleci

29/07 2016

0.2.0

0.2.0.0

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

  Sources   Download

MIT

The Development Requires

by Ondřej Machulda

continuous integration adapter interface teamcity travis jenkins bamboo circleci

29/07 2016

0.1.0

0.1.0.0

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

  Sources   Download

MIT

The Development Requires

by Ondřej Machulda

continuous integration adapter interface teamcity travis jenkins bamboo circleci