Formatting Jenkins Declarative Pipeline Code

Hello Jenkins Community,

I do not understand how to format my Jenkins Declarative Pipeline code that I have written in a Jenkinsfile. By “format” I mean applying proper indentation to every line. I have found some extensions for VS Code, like Groovy Lint, and some paid IntelliJ extensions, but I am not satisfied with them.

Can anyone help me with how to properly indent Jenkinsfile code?

Best regards
Alexander Bohn

Honestly, properly indenting Groovy files depends entirely on your development team. It’s up to you to decide how that actually looks.

That said, I am personally happy with how IntelliJ IDEA formats Groovy code by default. All formatting in IDEA can be configured however you prefer.

Groovy Lint uses CodeNarc under its hood, which is venerable and technically supports custom configuration - but dealing with it has always been a pain to me. AFAIK the only real competitor to npm-groovy-lint + codenarc duo would be Spotless. I don’t have any experience with it, but from what I understand, it’s supposed to integrate into Maven / Gradle buildsystem.

If you use Prettier, there are some Groovy plugins for it – but they are in various state of disarray.

Hmm, that’s an interesting idea. We’re currently integrating Spotless into Maven projects to handle checking our code and I know we can also include the Maven POMs as well. Adding the Jenkinsfile to that would be very interesting.

Hello Ken and Artalus,

thank you for your responses.