Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins shared library with intellij

I started implement Jenkins shared libraries and trying to write my jenkinsfile with intellij as my ide.

How do I get functions from the shared lib repository to the other repository which holds the jenkins file inside it.

Just to clarify things are working for me when running from jenkins but I need an easy way to write my jenkins file with the usage of functions from the shared-lib repo.

like image 445
Shahar Hamuzim Rajuan Avatar asked Nov 18 '18 17:11

Shahar Hamuzim Rajuan


2 Answers

You can try using the IntelliJ IDEA GDSL file provided by Jenkins in your IDE.

Download the content from http://<JENKINS>/job/<PIPELINE_JOB>/pipeline-syntax/gdsl to a pipline.gdsl file and add this file to your IntelliJ Jenkins Pipeline project's source folder. (You may also want to add that file to your SCMs ignore-list.)

Autocompletion before and after adding GDSL file:

autocompletion without GDSL autocompletion with GDSL

On the Jenkins UI you find that GDSL file in your pipeline project under "Pipeline Syntax > IntelliJ IDEA GDSL".

Jenkins GDSL

However, I found some caveats with this solution that required me to manually edit the pipeline.gdsl file, but that could be related to my not up-to-date Jenkins installation (?):

  • custom global variables (from the /var folder) are considered in the GDSL file, but methods defined on that variable are not.
  • the parallel step was not exported properly due to an error(?) on the ParallelStep class

(Source of knowledge: Veaceslav Gaidarji's blog.)

like image 138
Markus Mitterauer Avatar answered Sep 28 '22 02:09

Markus Mitterauer


I have had good experiences with jenkins-pipeline-shared-libraries-gradle-plugin by mkobit in combination with IntelliJ IDEA.

Some great features of this plugin are

  • Basic Groovy compilation to validate source code
  • Unit test using Jenkins Pipeline Unit
  • Usage of plugin and Jenkins core classes in library
  • @Grab support for libraries (testing limited to @JenkinsRule style integration tests due to an issue)
  • @NonCPS annotation can be used in main source code
  • Integration test using the Jenkins Test Harness

Personally, I develop my pipelines in the shared lib project and integrate the pipeline into the target repository after passing the unit tests.

There is also an example repository for a demonstration of using this plugin.

like image 21
SlashGordon Avatar answered Sep 28 '22 03:09

SlashGordon