Jenkinsfile syntax highlighting in Java project using Intellij Idea

The name of the picture


Jenkinsfile syntax highlighting in Java project using Intellij Idea



We already tried the approaches as listed below:



After having searched the web for many hours on multiple days, we still haven't found a helpful resource on this matter. Thus, it appears to make sense to ask a new question here.



We are developing our Java projects in IntelliJ idea and want to integrate our builds with Jenkins. When we create a Jenkinsfile in Idea, we do not get syntax highlighting or auto completion. Since we are new to Jenkins, those features would be really useful to us. How can we make Idea be more supportive with Jenkinsfiles?



If there is no way to get syntax highlighting and auto completion for a Jenkinsfile in idea, what other editors would be helpful?



Please note:



We've already tried the plugin https://github.com/oliverlockwood/jenkinsfile-idea-plugin. When the plugin is activated, the Jenkinsfile is recognized as such, but instead of syntax highlighting we get an error message, please see below.


pipeline {
agent { docker 'maven:3.3.3' }
stages {
stage('build') {
steps {
sh 'echo Hello, World!'
}
}
}
}



Idea highlights the 'p' of 'pipeline' as error. The error message reads:



JenkinsTokenType.COMMENT, JenkinsTokenType.CRLF or JenkinsTokenType.STEP_KEY expected, got 'p'



Thanks for any help!





There is a custom plugin: plugins.jetbrains.com/plugin/10127-jenkinsfile-idea-plugin and feature request: youtrack.jetbrains.com/issue/IDEA-158104
– y.bedrov
Dec 13 '17 at 15:39





@y.bedrov thanks for your comment. We already had tried the stated plugin, but only got an error message, but no syntax highlighting or auto completion. What's your experience with the plugin, does it work for you?
– rexford
Dec 13 '17 at 16:16





I have no idea why you would need a plugin for this basic stuff. Just add "Jenkinsfile" as a file type for Groovy (Settings > Editor > File Types > Groovy and add "Jenkinsfile" as a "Registered Pattern"). What's missing after that setup?
– Tom
Jan 17 at 8:29







@Tom thanks! Actually, your comment is about what we were looking for. Please provide this information as an answer, and I will accept it as correct / most helpful answer.
– rexford
Jan 18 at 9:31




4 Answers
4



If you want IDEA to recognize a Jenkinsfile as a Groovy file, then you can add the String "Jenkinsfile" as a valid file name pattern (normally contains file endings) for Groovy files. This is supported "out of the box" without requiring any additional Plugin (except the "Groovy" Plugin, but that is already part of IDEA).



To do that go to the settings menu, open the "Editor" item and then "File Types". Now select "Groovy" in the upper list and add "Jenkinsfile". You can also use a regex like "Jenkinsfile*" if you want to be more flexible regarding an optional file ending for the Jenkinsfile.
The setting should now look like this:
IDEA file type settings



Your example now looks like this in IDEA (with the Dracula theme):
Jenkinsfile syntax highlight



So IDEA now provides syntax highlighting and auto completion as far as I can tell. It suggests existing function/method names while writing, but I'm not a Groovy developer, thus I can't tell if some suggestions are missing.



Looking at the source code, it looks like COMMENTS are not defined (they are commented out in the code)
The STEP_KEY is defined as: STEP_NAME="sh" | "parallel"



I'm not sure the plugin does much more and it hasn't been updated in 2 years.



Below is the feedback we've got from IntelliJ. We will follow up on this if we are able to provide a working solution.



Andrey Dernov, IntelliJ, Dec 14, 07:50 CET



Hello Georg, there is no special Editor intellisense support for editing Jenkinsfiles in IDE. You will need to write a custom language plugin for this.



Or you can consider implementing such support via writing Kotlin DSL. Some examples to start with.



Another option is to use a shabang on top of the Jenkinsfile like this #!/usr/bin/env groovy. Also you can try out gdsl: https://st-g.de/2016/08/jenkins-pipeline-autocompletion-in-intellij
but so far it doesn't support declarative pipelines: https://issues.jenkins-ci.org/browse/JENKINS-40127


#!/usr/bin/env groovy






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Keycloak server returning user_not_found error when user is already imported with LDAP

PHP parse/syntax errors; and how to solve them?

How to scale/resize CVPixelBufferRef in objective C, iOS