Jenkins

Install and Use the Text Finder Jenkins Plugin

Like any tool in the development world, you will encounter scenarios where you need to search and locate a given string in Jenkins console logs or a set of files. This works by using regular expression patterns.

A regular expression refers to a sequence of characters that defines a search pattern. These search patterns are mainly used in pattern matching with strings or string matching, “find and replace” operations. The search pattern can be anything from a simple character, a fixed string, or a complex expression containing special characters describing the pattern.

Therefore, it is no surprise that regular expressions are the go-to feature when you search for a given string within an extensive collection of values. You can also use regular expressions for validating user input, searching and replacing text, extracting data from large text files, etc.

This tutorial will discuss how you can install and use the Text Finder plugin in Jenkins, allowing you to search for a given string using regular expressions.

Installing the Text Finder Plugin

The first step is to install the Text Finder plugin on your Jenkins server. You can do this by navigating to Manage Jenkins -> Manage Plugins -> Available Plugins and search for “Text Finder” plugin.

Once installed, Restart Jenkins to apply the changes.

Using the Text Finder Plugin

To use the Text Finder plugin, we can specify the findText block in a Jenkins pipeline as shown.

The syntax below shows how to use the Text Finder plugin to search for a specific pattern in Jenkins console output:

findText(textFinders: [textFinder(regexp: '<regular expression>', alsoCheckConsoleOutput: true)])

For example, to search for the string “set \+x” using the Text Finder plugin, we can run the query as shown:

For simplicity, we will use the GUI interface instead of a pipeline as shown:

  1. Go to the job’s configuration page.
  2. Scroll down to the “Post-build Actions” section.
  3. Click on the “Add post-build action” button and select “Text Finder” from the list of options.
  4. In the “Regular expression” field, enter the regular expression set \+x.
  5. In the “Failure message” field, you can enter a message that will be displayed if the search pattern is not found in the console output.
  6. Click the “Save” button to save the configuration.

When the job is run, the Text Finder plugin will search the console output for the string “set +x” using the specified regular expression. The job will be marked as a success if the string is found. If the string is not found, the job will be marked as a failure and Jenkins will display the failure message.

To run the search as a pipeline, use:

stage('Text Search') {
     steps {
          findText(textFinders: [textFinder(regexp: 'set +x', alsoCheckConsoleOutput: true)])
     }
}

To search for multiple regular expression patterns, use the syntax as shown:

findText(textFinders: [
 textFinder(regexp: '<regular expression 1>', [...]),
 textFinder(regexp: '<regular expression 2>', [...]),
 textFinder(regexp: '<regular expression 3>', [...])
])

You can explore the documentation to learn more.

Conclusion

This is on how to install and use the Text Finder plugin to search for a given string in Jenkins console output or a set of files using regular expressions.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list