Jenkinsfile syntax highlighting in a Java project using Intellij Idea

We have already tried the approaches listed below:

After repeatedly searching the Internet for several days, we still have not found a useful resource on this subject. Thus, it seems appropriate to ask a new question here.

We are developing our Java projects with IntelliJ and want to integrate our builds with Jenkins. When we create a Jenkinsfile in Idea, we don't get syntax highlighting or auto-completion. Since we are new to Jenkins, these features would be really useful for us. How can we make the idea more favorable with Jenkinsfiles?

If there is no way to get syntax highlighting and auto-complete for Jenkins idea in idea, what other editors would be useful?

Note:

  • we work with java projects, not groovy projects.
  • We have already tried the plugin https://github.com/oliverlockwood/jenkinsfile-idea-plugin . When the plugin is activated, the Jenkins file is recognized as such, but instead of syntax highlighting, we get an error message, see below.

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

    The idea highlights the "p" of the "pipeline" as an error. Error message:

    JenkinsTokenType.COMMENT, JenkinsTokenType.CRLF or JenkinsTokenType.STEP_KEY expected to receive 'p'

Thanks for any help!

+28
source share
5 answers

If you want IDEA to recognize the Jenkinsfile as a Groovy file, you can add the String "Jenkinsfile" as a valid file name template (usually contains the end of the file) for Groovy files. This is supported out of the box, without requiring an additional plugin (except for the Groovy plugin, but this is already part of IDEA).

To do this, go to the settings menu, open the "Editor", and then "File Types". Now select "Groovy" in the top list and add "Jenkinsfile". You can also use a regular expression like "Jenkinsfile *" if you want to be more flexible about the optional file ending for a Jenkins file.
The setup should now look like this: IDEA file type options

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

Thus, IDEA now provides syntax highlighting and automatic completion, as far as I can tell. It offers existing function / method names during recording, but I'm not a Groovy developer, so I can't tell if any suggestions are missing.

+68
source

Looking at the source code, it looks like the comments are undefined (they are commented out in the code)

STEP_KEY is defined as: STEP_NAME = "sh" | "parallel"

I am not sure if the plugin does much more and it has not been updated for 2 years.

+1
source

Below is the feedback received from IntelliJ. We will follow up on this if we can provide a working solution.

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

Hi Georg, There is no specific support for the Intellisense editor for editing Jenkinsfiles in the IDE. To do this, you need to write a custom language plugin.

Or you might consider introducing such support by writing Kotlin DSL. Some examples to get you started.

0
source

Another option is to use a shabang at the top of the Jenkinsfile, for example: #!/usr/bin/env groovy . You can also try gdsl: https://st-g.de/2016/08/jenkins-pipeline-autocompletion-in-intellij, but so far it does not support declarative pipelines: https://issues.jenkins-ci.org/ view / JENKINS-40127

0
source

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

 steps { sh """ echo 'Hello, World!' """ } 
0
source

Source: https://habr.com/ru/post/1274044/


All Articles