Search code examples
pythongithub-actionstravis-cicicdcircleci

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.


Solution

  • 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.