0
0
Jenkinsdevops~5 mins

Pipeline utility functions in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
Pipeline utility functions in Jenkins help automate common tasks like reading files, writing files, and handling JSON or YAML data inside your build pipelines. They make your pipeline scripts cleaner and easier to manage.
When you want to read configuration data from a file during a build.
When you need to write test results or logs to a file inside the pipeline.
When you want to parse JSON or YAML data to make decisions in your pipeline.
When you want to share common code snippets across multiple pipeline scripts.
When you want to simplify complex pipeline steps by using reusable utility functions.
Config File - Jenkinsfile
Jenkinsfile
pipeline {
    agent any
    stages {
        stage('Example') {
            steps {
                script {
                    // Read a JSON file
                    def jsonData = readJSON file: 'config.json'
                    echo "Project name is: ${jsonData.projectName}"

                    // Write some text to a file
                    writeFile file: 'output.txt', text: 'Build completed successfully'

                    // Load a shared utility script
                    def utils = load 'utils.groovy'
                    utils.sayHello('Jenkins User')
                }
            }
        }
    }
}

This Jenkinsfile shows how to use pipeline utility functions:

  • readJSON reads JSON data from a file.
  • writeFile writes text to a file.
  • load loads a Groovy script with utility functions.

These functions help keep your pipeline code clean and reusable.

Commands
Check that Jenkins Job Builder is installed and see its version to ensure compatibility with pipeline utilities.
Terminal
jenkins-jobs --version
Expected OutputExpected
1.0.0
Display the Jenkinsfile content to verify the pipeline utility functions usage.
Terminal
cat Jenkinsfile
Expected OutputExpected
pipeline { agent any stages { stage('Example') { steps { script { // Read a JSON file def jsonData = readJSON file: 'config.json' echo "Project name is: ${jsonData.projectName}" // Write some text to a file writeFile file: 'output.txt', text: 'Build completed successfully' // Load a shared utility script def utils = load 'utils.groovy' utils.sayHello('Jenkins User') } } } } }
Show the content of the utility Groovy script that contains reusable functions.
Terminal
cat utils.groovy
Expected OutputExpected
def sayHello(name) { echo "Hello, ${name}!" } return this
Key Concept

If you remember nothing else from pipeline utility functions, remember: they help you reuse code and handle files easily inside Jenkins pipelines.

Common Mistakes
Trying to use pipeline utility functions outside a script block.
Pipeline utility functions like readJSON and writeFile must be called inside a script block to run Groovy code.
Always wrap utility function calls inside a script { } block in your Jenkinsfile.
Not loading the utility Groovy script before calling its functions.
Without loading, Jenkins does not know about the functions defined in external scripts, causing errors.
Use the load step to import the Groovy script before calling its functions.
Summary
Use readJSON and writeFile inside script blocks to handle files in Jenkins pipelines.
Load external Groovy scripts with load to reuse utility functions.
Keep pipeline code clean and maintainable by using these utility functions.