Aleks
Aleks

Reputation: 5340

How to detect whether rspec tests were run in local or in Continuous Integration server ?

The title basically says it all - I would like to detect whether the tests are being run on local machine or in CI?
(so I could re-run the test in case it fails for some reason in the CI.)

Upvotes: 1

Views: 901

Answers (2)

jibiel
jibiel

Reputation: 8303

There is a great article by Adam Johnson on the topic:

https://adamj.eu/tech/2020/03/09/detect-if-your-tests-are-running-on-ci/

CI systems set various environment variables, so if you check for their presence you can tell if your code is running there. Here’s a list of some variables you can use for the most popular CI systems. (...)

Azure Pipelines sets TF_BUILD to true - docs.
Bamboo sets bamboo.buildKey - docs.
Buildkite sets BUILDKITE to true - docs.
Circle CI sets CIRCLECI to true - docs.
Cirrus CI sets CIRRUS_CI to true - docs.
CodeBuild sets CODEBUILD_BUILD_ID - docs.
GitHub Actions sets GITHUB_ACTIONS to true - docs.
GitLab CI sets GITLAB_CI - docs.
Heroku CI sets HEROKU_TEST_RUN_ID - docs.
Hudson sets BUILD_ID - docs.
Jenkins sets BUILD_ID - docs.
TeamCity sets TEAMCITY_VERSION - docs.
Travis CI sets TRAVIS to true - docs.

Also, some systems like Circle CI, GitHub Actions, GitLab CI, Heroku, and Travis CI set the generic variables `BUILD_ID`, `CI`, and others. These are intended to be provider-agnostic ways of detecting if you’re running on a CI environment, but this idiom doesn’t seem very widely adopted, so I wouldn’t rely on it.

Upvotes: 4

born4new
born4new

Reputation: 1687

I would try to figure out the host that is currently running the specs:

require 'socket'

Socket.gethostname

And base my decision on this.

Upvotes: 0

Related Questions