Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if we're in a GitHub Action / Travis CI / Circle CI etc. testing environment

I would like to programmatically determine if a particular Python script is run a testing environment such as

  • GitHub action
  • Travis CI
  • Circle CI

etc. I realize that this will require some heuristics, but that's good enough for me. Are certain environment variables always set? Is the user name always the same? Etc.

like image 486
Nico Schlömer Avatar asked Dec 02 '25 02:12

Nico Schlömer


2 Answers

An environment variable is generally set for each CI/CD pipeline tool.

The ones I know about:

os.getenv("GITHUB_ACTIONS")
os.getenv("TRAVIS")
os.getenv("CIRCLECI")
os.getenv("GITLAB_CI")

Will return true in a python script when executed in the respective tool environment.

e.g:

  • os.getenv("GITHUB_ACTIONS") == "true" in a Github Action workflow.
  • os.getenv("CIRCLECI") == "true" in a CircleCI pipeline.
  • ...

PS: If I'm not mistaken, to identify the python script is being executed in Jenkins or Kubernetes Service host, the behavior isn't the same.

like image 195
GuiFalourd Avatar answered Dec 03 '25 18:12

GuiFalourd


I think the CI environment variable is defined in most environments

References for your 3 cases:

  • GitHub CI
  • GitLab CI
  • Travis CI
  • Circle CI
like image 23
Can Rau Avatar answered Dec 03 '25 19:12

Can Rau