Jenkins shared library with intellij
Asked Answered
A

3

16

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.

Aesir answered 18/11, 2018 at 17:50 Comment(0)
M
10

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

Download the content from http://<JENKINS>/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.)

Myself answered 27/2, 2019 at 15:58 Comment(2)
I cannot find http://<JENKINS>/job/<PIPELINE_JOB>/pipeline-syntax/gdsl. How did you find that link? Is there constraints to get it (jenkins version, need admin right, ...)? Does it cope with organization/project/subproject structure?Machuca
Found it: no need for the job/<PIPELINE_JOB>/ part. It is directly accessible on http://<JENKINS>/pipeline-syntax/gdslMachuca
S
7

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.

Stelle answered 20/11, 2018 at 9:55 Comment(0)
R
3

You can create jenkins shared library as a maven project (or gradle, it doesn't matter) with specific group id, artifact id and version. You should perform 'mvn install' whenever you change your shared library (this goal used to add shared library jar artifact to the local maven repository). In another repository where you will write your jenkins file, you should also create a maven project and just add your shared library as a maven dependency. And now you can use code from the shared library in your jenkins file.

Revel answered 19/11, 2018 at 20:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.