We already tried the approaches as listed below:
https://github.com/oliverlockwood/jenkinsfile-idea-plugin
https://st-g.de/2016/08/jenkins-pipeline-autocompletion-in-intellij
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!
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).
https://i.stack.imgur.com/9n0SA.jpg
https://i.stack.imgur.com/SppcN.jpg
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.
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)
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 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 Add "Jenkinsfile" as a valid file name pattern for groovy files as described here To avoid the error message 'node' cannot be applied to '(groovy.lang.Closure
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
If you add
#!groovy
header to your jenkinsfile then you should get groovy syntax highlighting in IDE.
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.
https://i.stack.imgur.com/yVaIC.png
https://i.stack.imgur.com/b1unD.png
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.
Use sh
like this and the error should go away (worked for me)...
steps {
sh """
echo 'Hello, World!'
"""
}
Success story sharing
*.declarative
was assigned to the Text file type.