ci-detector

ci-detector

PHP库实现持续集成环境智能检测及信息提取

CI Detector是一款专门用于检测持续集成环境和读取构建信息的PHP库。该库支持GitHub Actions、GitLab、Jenkins等多种CI服务器,可识别构建编号、提交哈希和分支名称等关键数据。开发者可利用CI Detector轻松适配不同CI平台,使CLI脚本和工具能根据具体CI环境灵活调整行为。

CI Detector持续集成环境检测构建信息PHP库Github开源项目

CI Detector

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 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:

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()):

MethodExample valueDescription
getCiName()GitHub ActionsName of the CI server.<br>The value is one of CiDetector::CI_* constants.
getBuildNumber()33Get number of this concrete build.<br>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<br>or empty stringGet 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<br>or empty stringGet name of the git (or other VCS) branch which is being built or empty string if it cannot be determined.<br>Use getTargetBranch() to get name of the branch where this branch is targeted.
getTargetBranch()main<br>or empty stringGet name of the target branch of a pull request or empty string if it cannot be determined.<br>This is the base branch to which the pull request is targeted.
getRepositoryName()OndraM/ci-detector<br>or empty stringGet name of the git (or other VCS) repository which is being built or empty string if it cannot be determined.<br>This is usually in form "user/repository", for example OndraM/ci-detector.
getRepositoryUrl()https://github.com/OndraM/ci-detector<br>or empty stringGet URL where the repository which is being built can be found or empty string if it cannot be determined.<br>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 instanceDetect whether current build is from a pull/merge request.<br>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).<br>Use condition like if ($ci->isPullRequest()->yes()) { /*...*/ } to use the value.
describe()[...]<br>(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 (❌).

CI serverConstant of CiDetectoris​PullRequestget​Branchget​Target​Branchget​Repository​Nameget​Repository​Urlget​Build​Url
AppVeyorCI_APPVEYOR
AWS CodeBuildCI_AWS_CODEBUILD
Azure PipelinesCI_AZURE_PIPELINES
BambooCI_BAMBOO
Bitbucket PipelinesCI_BITBUCKET_PIPELINES
BuddyCI_BUDDY
CircleCICI_CIRCLE
CodeshipCI_CODESHIP
continuousphpCI_CONTINUOUSPHP
droneCI_DRONE
GitHub ActionsCI_GITHUB_ACTIONS
GitLabCI_GITLAB
JenkinsCI_JENKINS
SourceHutCI_SOURCEHUT
TeamCityCI_TEAMCITY
[Travis CI][travis-ci]CI_TRAVIS
WerckerCI_WERCKER

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:

[travis-ci]:

编辑推荐精选

Vora

Vora

免费创建高清无水印Sora视频

Vora是一个免费创建高清无水印Sora视频的AI工具

Refly.AI

Refly.AI

最适合小白的AI自动化工作流平台

无需编码,轻松生成可复用、可变现的AI自动化工作流

酷表ChatExcel

酷表ChatExcel

大模型驱动的Excel数据处理工具

基于大模型交互的表格处理系统,允许用户通过对话方式完成数据整理和可视化分析。系统采用机器学习算法解析用户指令,自动执行排序、公式计算和数据透视等操作,支持多种文件格式导入导出。数据处理响应速度保持在0.8秒以内,支持超过100万行数据的即时分析。

AI工具酷表ChatExcelAI智能客服AI营销产品使用教程
TRAE编程

TRAE编程

AI辅助编程,代码自动修复

Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。

AI工具TraeAI IDE协作生产力转型热门
AIWritePaper论文写作

AIWritePaper论文写作

AI论文写作指导平台

AIWritePaper论文写作是一站式AI论文写作辅助工具,简化了选题、文献检索至论文撰写的整个过程。通过简单设定,平台可快速生成高质量论文大纲和全文,配合图表、参考文献等一应俱全,同时提供开题报告和答辩PPT等增值服务,保障数据安全,有效提升写作效率和论文质量。

AI辅助写作AI工具AI论文工具论文写作智能生成大纲数据安全AI助手热门
博思AIPPT

博思AIPPT

AI一键生成PPT,就用博思AIPPT!

博思AIPPT,新一代的AI生成PPT平台,支持智能生成PPT、AI美化PPT、文本&链接生成PPT、导入Word/PDF/Markdown文档生成PPT等,内置海量精美PPT模板,涵盖商务、教育、科技等不同风格,同时针对每个页面提供多种版式,一键自适应切换,完美适配各种办公场景。

AI办公办公工具AI工具博思AIPPTAI生成PPT智能排版海量精品模板AI创作热门
潮际好麦

潮际好麦

AI赋能电商视觉革命,一站式智能商拍平台

潮际好麦深耕服装行业,是国内AI试衣效果最好的软件。使用先进AIGC能力为电商卖家批量提供优质的、低成本的商拍图。合作品牌有Shein、Lazada、安踏、百丽等65个国内外头部品牌,以及国内10万+淘宝、天猫、京东等主流平台的品牌商家,为卖家节省将近85%的出图成本,提升约3倍出图效率,让品牌能够快速上架。

iTerms

iTerms

企业专属的AI法律顾问

iTerms是法大大集团旗下法律子品牌,基于最先进的大语言模型(LLM)、专业的法律知识库和强大的智能体架构,帮助企业扫清合规障碍,筑牢风控防线,成为您企业专属的AI法律顾问。

SimilarWeb流量提升

SimilarWeb流量提升

稳定高效的流量提升解决方案,助力品牌曝光

稳定高效的流量提升解决方案,助力品牌曝光

Sora2视频免费生成

Sora2视频免费生成

最新版Sora2模型免费使用,一键生成无水印视频

最新版Sora2模型免费使用,一键生成无水印视频

下拉加载更多