A Jenkinsfile can be written in a declarative or scripted pipeline syntax. This allows Jenkins to understand the institutions and steps which are defined in the pipeline. We can use the Jenkinsfile to describe a wide variety of tasks such as builds, testing, deployment, and more.
In a Jenkinsfile, a comment refers to the lines of text that are ignored by the Jenkins pipeline engine during execution. Comments are beneficial as they allow the DevOps engineers to add the documentation to the pipeline file.
In this tutorial, we will learn how you can create the various comments in a Jenkinsfile to add a meta documentation to your pipeline file.
Comments in Jenkinsfile
The syntax of a Jenkinsfile is based on the Groovy programming language. This means that we can use the Groovy comment format to add the comments in a Jenkinsfile.
Jenkinsfile Single-Line Comments
We can create the single-line comments in a Jenkinsfile. These types of comments span to the end of a given line. The single-line comments are useful to add a meta information about a given code block.
To add a single-line comment in a Jenkinsfile, we can use the two forward slash characters (//). Any text that is added after the characters until the end of the line is treated as comments.
The following shows the example of a single-line comment in Jenkinsfile:
agent any
stages {
stage('Build') {
steps {
// load the OWASP Dependency Checker plugin
dependencyCheck(odcInstallation: 'owasp', additionalArguments: '--format HTML --format XML ')
dependencyCheckPublisher()
}
}
// another single-line comment
}
}
Jenkinsfile Block Comments
The second type of comments in Jenkins are block comments. These are the type of comments that span multiple lines creating a block.
Block comments are useful to add a detailed information about a given code block. You can also use it to add a meta information such as author information, creation date, dependencies, and more.
To create the block comments in a Jenkinsfile, we start with a forward slash and an asterisk character (/*). To close a block comment, we start with an asterisk followed with a forward slash (*/).
Any text that is placed between the opening and closing characters are treated as comments. The following example shows the example of a block comment in a Jenkins file:
agent any
stages {
stage('Build') {
steps {
/* load the OWASP Dependency Check Plugin
….. also a comment
… and another comment
…… until we close */
And a f
dependencyCheck(odcInstallation: 'owasp', additionalArguments: '--format HTML --format XML ')
dependencyCheckPublisher()
}
}
}
}
NOTE: The elipses characters in the previous code are part of the comment and are not required for a block comment.
Jenkinsfile Shell Comments
In some cases, you may need to execute a shell script inside your Jenkins pipeline. In such a case, you can add the shell comments using a pound character (#).
Example:
agent any
stages {
stage('Start') {
steps {
sh '''
cd ~
# shell comment navigating to the home folder
'''
}
}
}
}
Conclusion
You learned about the various types of comments in Jenkins pipeline file. You also learned how to create such type of comments in a Jenkinsfile and their corresponding usage.