Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkinsfile syntax highlighting in Java project using Intellij Idea

People also ask

How does IntelliJ integrate with Jenkins?

Open IntelliJ IDEA. Click on Import Project, and in the popup, choose the directory where Jenkins had been cloned. Select 'Create project from existing sources', and click on Next. Click on Finish.

What file type is Jenkinsfile?

As discussed in the Defining a Pipeline in SCM, a Jenkinsfile is a text file that contains the definition of a Jenkins Pipeline and is checked into source control. Consider the following Pipeline which implements a basic three-stage continuous delivery pipeline.

How do you use GDSL?

gdsl” anywhere in the classpath. For an example go to any package in your project src, and right click on it from within the IDE and select new>Groovy Script. Now a dialog will appear and then select “GroovyDSL Script” in the “kind” parameter. Now you have the script file and you can start coding GDSL in here.


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.


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.


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

  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