Jenkinsfile syntax highlighting in Java project using IntelliJ IDEA
Asked Answered
G

8

179

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 IntelliJ IDEA, what other editors would be helpful?


Please note:

  • we are working with Java projects, not Groovy projects.

  • 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!'
             }
         }
       }
     }
    

IntelliJ 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!

Godavari answered 13/12, 2017 at 15:29 Comment(5)
There is a custom plugin: plugins.jetbrains.com/plugin/10127-jenkinsfile-idea-plugin and feature request: youtrack.jetbrains.com/issue/IDEA-158104Bronchia
@Bronchia 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?Godavari
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?Germinative
@Germinative 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.Godavari
@Germinative What's missing is autocompletion and validation of the actual Jenkins DSLMetaphysic
G
31

At long last we found a solution that works for us and provides syntax highlighting and code completion for the Jenkinsfile present in an otherwise normal Java project in Idea. The solution is taken from here, here (and from additional personal experiments / research)

  1. Download the Groovy SDK (if you did not do so already) from the Groovy Page and configure it on your Java project. For help on this see here
  2. Download the pipeline GDSL file from your Jenkins instance which should be available under a link like https://yourJenkinsInstance.tld/pipeline-syntax/gdsl, and add it to the classpath of your Java project. E.g. by creating a new folder src/main/jenkins, putting the pipeline GDSL file there and marking the folder as source root in IntelliJ IDEA. Make sure that the file extension is .gdsl, not .groovy. Otherwise, code completion will not work.
  3. Add "Jenkinsfile" as a valid file name pattern for groovy files as described here
  4. To avoid the error message 'node' cannot be applied to '(groovy.lang.Closure<java.lang.Object>), you can add this line at the top of your Jenkinsfile:
    // noinspection GroovyAssignabilityCheck
    
Godavari answered 14/12, 2017 at 17:57 Comment(0)
G
368

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.

Germinative answered 18/1, 2018 at 16:42 Comment(6)
Features that people would want include validation/autocompletion/quickdocs for the GDSL and integration of the linter. An ideal plugin would make everything "just work" with minimal configuration (e.g. just point it at a Jenkins instance).Metaphysic
Just a note, the groovy file type is only available in InteliJ and Android studio, not in PyCharm 2018.3.2. I think because this plugin is required plugins.jetbrains.com/plugin/1524-jetgroovyArdellardella
Also consider downloading the gdsl file from your Jenkins instance at https://<Jenkins>/job/<Job Name>/pipeline-syntax/gdsl and put them into the Groovy classpath of your project.Chengtu
This is a nice idea but requires a Groovy SDK to be configuredYerkovich
Brilliant! TY so much! I was able to reassign our Jenkinsfile.declarative files in the same manner so that we get both syntax highlighting and code completion. For us *.declarative was assigned to the Text file type.Fleeman
Idea will recognize any file as Groovy if it contains shebang #!groovy on first linePrecocious
G
31

At long last we found a solution that works for us and provides syntax highlighting and code completion for the Jenkinsfile present in an otherwise normal Java project in Idea. The solution is taken from here, here (and from additional personal experiments / research)

  1. Download the Groovy SDK (if you did not do so already) from the Groovy Page and configure it on your Java project. For help on this see here
  2. Download the pipeline GDSL file from your Jenkins instance which should be available under a link like https://yourJenkinsInstance.tld/pipeline-syntax/gdsl, and add it to the classpath of your Java project. E.g. by creating a new folder src/main/jenkins, putting the pipeline GDSL file there and marking the folder as source root in IntelliJ IDEA. Make sure that the file extension is .gdsl, not .groovy. Otherwise, code completion will not work.
  3. Add "Jenkinsfile" as a valid file name pattern for groovy files as described here
  4. To avoid the error message 'node' cannot be applied to '(groovy.lang.Closure<java.lang.Object>), you can add this line at the top of your Jenkinsfile:
    // noinspection GroovyAssignabilityCheck
    
Godavari answered 14/12, 2017 at 17:57 Comment(0)
T
18

If you add

#!groovy​

header to your jenkinsfile then you should get groovy syntax highlighting in IDE.

Tarnetgaronne answered 5/3, 2020 at 7:26 Comment(1)
In intellij it's not working and this answer is too vague in generalPuerilism
N
10

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

Newfashioned answered 24/7, 2018 at 10:20 Comment(0)
I
5

go to the settings menu, open the "Editor"--> "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. enter image description here

enter image description here

Ioab answered 19/4, 2022 at 6:45 Comment(1)
Isn’t this copied from the accepted answer to this question?!Endstopped
M
3

Jenkinsfile is a groovy like script, but normally IntelliJ map it to TextMate editor.

To do that go to the settings menu, open the "Editor" item and then "File Types". Now select "TextMate" 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.

Melidamelilot answered 26/4, 2022 at 7:48 Comment(1)
worked for Pycharm 2022.1.2 professional !! thanksHyacinthie
W
1

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.

Williamwilliams answered 17/1, 2018 at 8:22 Comment(0)
H
-21

Use sh like this and the error should go away (worked for me)...

steps {
    sh """
        echo 'Hello, World!'
    """
}
Hypermeter answered 9/11, 2018 at 14:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.