Check if we're in a GitHub Action / tracis ci / circle ci etc. testing environment

Question:

I would like to programatically determine if a particular Python script is run a testing enviroment such as

  • GitHub action
  • Tracis 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.

Asked By: Nico Schlömer

||

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.

Answered By: GuiFalourd