定制 mlocati/ci-info 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mlocati/ci-info

Composer 安装命令:

composer require mlocati/ci-info

包简介

Get informations about the current continuous integration environment

README 文档

README

This package lets you get details about the current Continuous Integration environment.

Supported environments are:

  • AppVeyor
  • GitHub Actions
  • TravisCI

Installation

You can install this package in two ways:

Usage from within your PHP scripts

Setup

First of all, if you don't use Composer, you need to include the autoload.php file you can find in the root directory of this project:

require_once 'path/to/autoload.php';

Usage

Determine the current CI Environment

$driver = (new \CIInfo\DriverList())->getDriverForEnvironment();
if ($driver === null) {
    // CI environment Not detected
} else {
    if ($ci->getHandle() === \CIInfo\Driver\GithubActions::HANDLE) {
        // We are running in a GitHub Actions build
    }
}

Get info about the current job

try {
    $state = (new \CIInfo\StateFactory())->getCurrentState();
} catch (\CIInfo\Exception $whoops) {
    echo "Something went wrong: " . (string) $whoops;
    return;
}

switch ($state->getEvent()) {
    case \CIInfo\State::EVENT_PUSH:
        // $state is an instance of the \CIInfo\State\Push (or its \CIInfo\State\PushWithoutBaseCommit subclass) class
        echo "We are in a build triggered by a push.\n";
        break;
    case \CIInfo\State::EVENT_PULLREQUEST:
        // $state is an instance of the \CIInfo\State\PullRequest class  
        echo "We are in a build triggered by a pull request.\n";
        break;
    case \CIInfo\State::EVENT_TAG:
        // $state is an instance of the \CIInfo\State\Tag class
        echo "We are in a build triggered by the creation of a tag.\n";
        break;
    case \CIInfo\State::EVENT_SCHEDULED:
        // $state is an instance of the \CIInfo\State\Scheduled class
        echo "We are in a build triggered by a scheduled job.\n";
        break;
    case \CIInfo\State::EVENT_MANUAL:
        // $state is an instance of the \CIInfo\State\Manual class
        echo "We are in a build triggered manually (via APIs, manual builds, repository_dispatch events, ...).\n";
        break;
}

To see the methods available for every class, see the source code.

Usage from a shell

You can also use this library in your shell scripts (bash, sh, powershell, ...).

First of all, you have to determine the path of the ci-info file. It's under the bin directory (or composer/vendor/bin for composer-based projects).

ci-info can provide details about the current environment/job.

Here's a sample POSIX script:

$ driver="$(./bin/ci-info driver)"
$ echo "The current CI environment is: $driver"
The current CI environment is: travis-ci

Here's a sample PowerShell script:

PS> $driver="$(.\bin\ci-info driver)"
PS> Write-Host "The current CI environment is: $driver"
The current CI environment is: github-actions

To get the full list of the features of the ci-info command, type:

$ ./bin/ci-info --help

Which outputs:

Syntax:
  ./bin/ci-info [-q|--quiet] [-h|--help] <command>

Options:
-q|--quiet: turn off displaying errors
-h|--help : show this syntax message and quits

Allowed values for <command> are:
# driver
Print the handle identifying the current environment.
Possible results are:
- appveyor: AppVeyor
- github-actions: GitHub Actions
- travis-ci: Travis CI

# event
Print the current operation type.
Possible results are:
- cron: Scheduled event
- manual: Manually triggered event (API calls, repository_dispatch events, forced rebuilds, ...)
- pr: Pull request event
- push: Push event
- tag: Tag creation event

# sha1
Print the SHA-1 of the most recent commit (it's the merge commit in case of pull requests)

# pr:base:branch
Print the target branch of a pull request

# pr:base:sha1
Print the SHA-1 of the last commit in the target branch

# pr:head:sha1
Print the SHA-1 of the last commit in the pull request branch

# pr:wrongsha1
Print the wrong SHA-1 of the merge commit of a pull request event as defined by the current environment.
For example, the TRAVIS_COMMIT environment variable defined in TravisCI may be wrong (see https://travis-ci.community/t/travis-commit-is-not-the-commit-initially-checked-out/3775 )
If there merge commit SHA-1 is correct, nothing gets printed out. 

# pr:range
Print the commit range of pull request events (example: 123456abcded...abcded123456)

# push:branch
Print the name of the branch affected by a push event

# push:range:has
Check if the commit range is available.
In case it's not available, the exit code is 1, otherwise it's 0.
The reason why the range is not available is printed out to the standard error (use -q to prevent that).

# push:prev:sha1
Print the SHA-1 of the commit prior to the last commit for a push event

# push:range
Print the commit range of push events (example: 123456abcded...abcded123456)

# tag:name
Print the tag name (for tag jobs)

# manual:branch
Print the current branch in a manually-triggered job

# cron:branch
Print the current branch in a scheduled job

Exit code:
0: success
1: failure

Tests

This library is tested against two types of cases:

  • Offline tests, implemented as GitHub Actions and managed by phpunit, which test the library against well-known environment statuses of the supported CI environment
    • Status: Offline Tests
  • Online tests, executed directly in every supported CI enviromnent
    • AppVeyor: AppVeyor Online Tests
    • GitHub Actions: GitHub Actions Online Tests
    • TravisCI:
      • Push tests: TravisCI Online Tests for pushes
      • Pull request tests: TravisCI Online Tests for pull requests

mlocati/ci-info 适用场景与选型建议

mlocati/ci-info 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 142.94k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2020 年 06 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「github」 「ci」 「travis」 「request」 「continuous」 「actions」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 mlocati/ci-info 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 mlocati/ci-info 我们能提供哪些服务?
定制开发 / 二次开发

基于 mlocati/ci-info 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 142.94k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 24
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-06-25