Search code examples
shellunixjenkinsjenkins-declarative-pipeline

unable to understand what `sh !` does inside a Jenkins pipeline code


I have a Jenkins pipeline code having

stages {
    stage('Monitoring logs') {
       steps {
          script {
              sh '! grep "wow" output.log
                 }
              }
          }
      }

Can someone please explain what does this part of the code do -> sh '! grep "wow" output.log' and what impact will it have if any on the execution or the status of the pipeline execution incase "wow" string is found in output.log?

I understand that grep "wow" output.log will search for "wow" string in output.log but i dont know what would sh '! grep "wow" output.log' do?


Solution

    1. grep "wow" output.log succeeds (exits 0) if output.log contains wow, and fails (exits non-0) otherwise.

    2. ! negates the exit code (0 becomes 1; non-0 becomes 0).

    3. Jenkins’ sh function runs a shell script and throws an exception, which will make the pipeline fail, on a non-0 exit code.*

    Ergo the pipeline will fail if output.log contains wow.

    * Through the use of the -e option.