mlocati/ci-info

获取当前持续集成环境的详细信息

1.1.0 2020-07-06 07:33 UTC

This package is auto-updated.

Last update: 2024-08-29 14:12:32 UTC


README

此包允许您获取当前持续集成环境的详细信息。

支持的环境包括

  • AppVeyor
  • GitHub Actions
  • TravisCI

安装

您可以通过以下两种方式安装此包:

在您的PHP脚本中使用

设置

首先,如果您不使用Composer,您需要包含此项目根目录中可找到的autoload.php文件

require_once 'path/to/autoload.php';

使用

确定当前的CI环境

$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
    }
}

获取当前任务的信息

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;
}

要查看每个类的可用方法,请参阅源代码

在shell中使用

您也可以在shell脚本(bash,sh,powershell等)中使用此库。

首先,您必须确定ci-info文件的位置。它位于bin目录下(或基于composer的项目为composer/vendor/bin)。

ci-info可以提供关于当前环境/作业的详细信息。

以下是一个POSIX脚本的示例

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

以下是一个PowerShell脚本的示例

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

要获取ci-info命令的所有功能列表,请输入

$ ./bin/ci-info --help

输出

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.cnmunity/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

测试

此库针对两种类型的案例进行测试

  • 离线测试,作为GitHub Actions实现并由phpunit管理,测试库对支持的CI环境的已知环境状态
    • 状态:[离线测试](https://github.com/mlocati/ci-info/actions?query=workflow%3A%22Offline+Tests%22) ![离线测试](https://github.com/mlocati/ci-info/workflows/Offline%20Tests/badge.svg)
  • 在线测试,直接在每个支持的CI环境中执行
    • AppVeyor:[在线测试](https://ci.appveyor.com/project/mlocati/ci-info/history) ![AppVeyor在线测试](https://camo.githubusercontent.com/817908fe206efd6fa084edc96ebd1542cb887d35c5d3950c40dfb5cca9acde12/68747470733a2f2f63692e6170707665796f722e636f6d2f6170692f70726f6a656374732f7374617475732f78356a306672657032637238643269332f6272616e63682f6d61737465723f7376673d74727565)
    • GitHub Actions:[在线测试](https://github.com/mlocati/ci-info/actions?query=workflow%3A%22Online+Tests%22) ![GitHub Actions在线测试](https://github.com/mlocati/ci-info/workflows/Online%20Tests/badge.svg)
    • TravisCI
      • 推送测试:[在线测试](https://travis-ci.org/github/mlocati/ci-info/branches) ![TravisCI推送测试](https://camo.githubusercontent.com/164c60ed2ced1974f7332b64f061300456f7393241b2ee2bbc76731cd4019b07/68747470733a2f2f7472617669732d63692e6f72672f6d6c6f636174692f63692d696e666f2e7376673f6272616e63683d6d6173746572)
      • 拉取请求测试:[在线测试](https://travis-ci.org/github/mlocati/ci-info/pull_requests) ![TravisCI拉取请求测试](https://camo.githubusercontent.com/164c60ed2ced1974f7332b64f061300456f7393241b2ee2bbc76731cd4019b07/68747470733a2f2f7472617669732d63692e6f72672f6d6c6f636174692f63692d696e666f2e7376673f6272616e63683d6d6173746572)