How do I detect my test is running on a Jenkins environment? How do I detect my test is running on a Jenkins environment? jenkins jenkins

How do I detect my test is running on a Jenkins environment?


Since they are easy to test for and passed to every program that gets executed by the build, I opted to base the check on environment variables.

Though rohit answer shows one way of setting variables on Jenkinsfile, I'd rather rely on things that Jenkins itself does, so I checked the environment variables set on my Jenkins jobs, and there's many options to pick from:

General Jenkins Information

These seem to be constant on a Jenkins instance.

  • HUDSON_URL
  • JENKINS_URL

Not Jenkins-specific, but the following is also useful:

  • USER

Our Jenkins runs under user jenkins, so testing that value is also a possibility.

Job Information

These have constant values across builds for the same job.

  • JOB_URL
  • JOB_NAME
  • JOB_BASE_NAME
  • JOB_DISPLAY_URL

Build Information

These have constant values across the same build (that is, for different sh invocations).

  • BUILD_URL
  • BUILD_TAG
  • RUN_CHANGES_DISPLAY
  • RUN_DISPLAY
  • BUILD_DISPLAY_NAME
  • BUILD_ID
  • BUILD_NUMBER

Node Information

These are related to the node on which the current command is being executed.

  • JENKINS_HOME
  • JENKINS_NODE_COOKIE
  • NODE_LABELS
  • NODE_NAME

Special

This one changes with each build, may change from node to node, and may even change with Jenkinsfile configuration:

  • WORKSPACE

Miscellaneous

I'm not sure about these. They are definitely from Jenkins, but I don't know what their lifecycle is.

  • HUDSON_SERVER_COOKIE
  • JENKINS_SERVER_COOKIE
  • HUDSON_COOKIE

Final Considerations

For my own purposes, I decided to go with JENKINS_HOME. The name is very Jenkins-specific, and it seems more fundamental than, say, JENKINS_URL. While it doesn't imply the build is being run by Jenkins, it will always be set in that case. I don't mind false positives, as long as I don't get false negatives.


Couple of ways you can achieve this:

  1. You can make you application accept arguments and then pass aTRUE/FALSE value indicating it is running from Jenkins or not.

  2. You can also read the system properties of os

e.g. System.getProperty("os.arch");

But this will not work if your Jenkins environment and yourworkspace are on the same machine

  1. You can just set an env variable in your pipeline(exists only in pipeline) and in the application you can read that value

Like so :

Pipeline- option1

     pipeline {            environment {                FROM_JENKINS= "TRUE"             } stage('test'){                    sh "mvn test"             }        }

Pipeline- option2

     pipeline {            stage('test'){                     sh '''FROM_JENKINS="TRUE" // setting the env variable in the same shell where you are running mvn                        mvn test'''             }        }

Application

boolean isJenkinsBuild = Boolean.valueOf(System.getenv("FROM_JENKINS"));boolean isSoftwarePresent = new ProcessBuilder("check software presence").start().waitFor() == 0;Assume.assumeTrue("Software not present", isSoftwarePresent || isJenkinsBuild);

Hope it helps :)