1. Overview

In this tutorial, we’ll see how we can use comments in Jenkinsfile. We will cover different types of comments and their syntax.

2. Comments in a Jenkinsfile

The syntax of Jenkinsfile is based on Groovy, so it’s possible to use Groovy syntax for comments. Let’s take a simple example of Pipeline Linter and try to comment it out.

2.1. Single-Line Comments

Single-line comments in Jenkinsfile are the same as we see in popular languages like Java, C++, and C#.

They start with two forward slashes (//). Any text between // and the end of the line is commented and ignored in Jenkinsfile.

Let’s use single-line comments to comment out a basic pipeline definition:

//pipeline {
//    agent any
//    stages {
//        stage('Initialize') {
//            steps {
//                echo 'Hello World'
//            }
//        }
//    }
//}

2.2. Block Comments

Block comments in Jenkinsfile are used to comment out a block of code. Again, the pattern is similar to Java and C++.

A block comment starts with a forward-slash followed by an asterisk (/*) and ends with an asterisk followed by a forward-slash (*/). The beginning (/*) and ending (*/) characters will be added to the appropriate places to mark the selected block as a comment.

In this example, we’ll use block comments to comment out the pipeline definition:

/*
pipeline {
    agent any
    stages {
        stage('Initialize') {
            steps {
                echo 'Placeholder.'
            }
        }
    }
}
*/

2.3. Comments in Shell Script

While inside of a shell (sh) section, we’ll be using the shell comment character, hash (#), for commenting:

pipeline {
    agent any
    stages {
        stage('Initialize') {
            steps {
                sh '''
                cd myFolder
                # This is a comment in sh & I am changing the directory to myFolder
                '''
            }
        }
    }
}

3. Conclusion

In this short article, we covered different types of comments in a Jenkinsfile. First, we looked at single-line comments. Next, we saw how to use block comments. Finally, we showed how to add comments within a shell section of a Jenkinsfile.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.