Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cucumber/ci-environment
Detect CI Environment from environment variables
https://github.com/cucumber/ci-environment
continuous-delivery continuous-integration github-actions gitlab-ci go java javascript jenkins polyglot-release python ruby tidelift
Last synced: 6 days ago
JSON representation
Detect CI Environment from environment variables
- Host: GitHub
- URL: https://github.com/cucumber/ci-environment
- Owner: cucumber
- License: mit
- Created: 2021-10-20T00:11:00.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-01-23T02:51:11.000Z (10 days ago)
- Last Synced: 2025-01-23T03:27:48.465Z (10 days ago)
- Topics: continuous-delivery, continuous-integration, github-actions, gitlab-ci, go, java, javascript, jenkins, polyglot-release, python, ruby, tidelift
- Language: Java
- Homepage:
- Size: 1.41 MB
- Stars: 11
- Watchers: 69
- Forks: 7
- Open Issues: 17
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# CiEnvironment
[![test-java](https://github.com/cucumber/ci-environment/actions/workflows/test-java.yml/badge.svg)](https://github.com/cucumber/ci-environment/actions/workflows/test-java.yml)
[![test-javascript](https://github.com/cucumber/ci-environment/actions/workflows/test-javascript.yml/badge.svg)](https://github.com/cucumber/ci-environment/actions/workflows/test-javascript.yml)
[![test-ruby](https://github.com/cucumber/ci-environment/actions/workflows/test-ruby.yml/badge.svg)](https://github.com/cucumber/ci-environment/actions/workflows/test-ruby.yml)
[![test-go](https://github.com/cucumber/ci-environment/actions/workflows/test-go.yml/badge.svg)](https://github.com/cucumber/ci-environment/actions/workflows/test-go.yml)This library detects the CI environment based on environment variables defined
by CI servers.If a CI server is detected, a `CiEnvironment.json` struct is returned:
```json
{
"name": "...",
"url": "...",
"buildNumber": "...", // optional
"git": { // optional
"remote": "...",
"revision": "...",
"branch": "...", // optional
"tag": "..." // optional
}
}
```Note that some fields are optional (they may not be set if the corresponding environment
variables are not defined).Some CI servers expose usernames and passwords in the environment variable
that is used to detect `git.remote`. For security reasons, this library removes
the username and password from the `git.remote` field in the `CiEnvironment.json` struct.### TypeScript
```shell
npm install @cucumber/ci-environment
``````typescript
import detectCiEnvironment from '@cucumber/ci-environment'const ciEnvironment = detectCiEnvironment(process.env)
console.log(JSON.stringify(ciEnvironment, null, 2))
```### Java
```xml
io.cucumber
ci-environment```
```java
import static io.cucumber.cienvironment.DetectCiEnvironment.detectCiEnvironment;public class CiEnvironmentExample {
public static void main(String[] args) {
CiEnvironment ciEnvironment = detectCiEnvironment(System.getenv()).orElseThrow(() -> new RuntimeException("No CI environment detected"));
System.out.println("ciEnvironment = " + ciEnvironment);
}
}
```### Ruby
```ruby
gem 'cucumber-ci-environment'
``````ruby
require 'cucumber/ci_environment'ci_environment = Cucumber::CiEnvironment.detect_ci_environment(ENV)
p ci_environment
```### Go
```shell
go get github.com/cucumber/ci-environment/go@latest
``````Go
import (
"fmt"
cienvironment "github.com/cucumber/ci-environment/go"
)func main() {
ci := cienvironment.DetectCIEnvironment()
if ci == nil {
fmt.Println("No CI environment detected")
}
}
``````python
import os
from ci_environment import detect_ci_environmentci_environment = detect_ci_environment(os.environ)
```## Supported CI servers
* [Azure Pipelines](https://docs.microsoft.com/en-us/azure/devops/pipelines/build/variables?tabs=yaml&view=azure-devops#build-variables)
* [Bamboo](https://confluence.atlassian.com/bamboo/bamboo-variables-289277087.html)
* [Buddy](https://buddy.works/docs/pipelines/environment-variables#default-environment-variables)
* [Bitrise](https://devcenter.bitrise.io/builds/available-environment-variables/)
* [CircleCI](https://circleci.com/docs/2.0/env-vars/#built-in-environment-variables)
* [CodeFresh](https://codefresh.io/docs/docs/codefresh-yaml/variables/#system-provided-variables)
* [CodeShip](https://documentation.codeship.com/basic/builds-and-configuration/set-environment-variables/)
* [GitHub Actions](https://help.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables)
* [GitLab](https://docs.gitlab.com/ee/ci/variables/predefined_variables.html)
* [GoCD](https://docs.gocd.org/current/faq/dev_use_current_revision_in_build.html)
* [Jenkins](https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#using-environment-variables) and [Jenkins Git plugin](https://plugins.jenkins.io/git/#environment-variables)
* [JetBrains Space](https://www.jetbrains.com/help/space/automation-parameters.html#use-provided-parameters)
* [Semaphore](https://docs.semaphoreci.com/ci-cd-environment/environment-variables/)
* [Travis CI](https://docs.travis-ci.com/user/environment-variables/#Default-Environment-Variables)
* [Wercker](https://devcenter.wercker.com/administration/environment-variables/available-env-vars/)## Contributing
Please see [CONTRIBUTING.md](./CONTRIBUTING.md) for more information.