Reuse jenkins pipeline or groovy script? - jenkins

We have jenkins pipelines which are reused and some pipelines which use the same functions.
Now is my question: what is the right approach to reuse them.
I use a shared library but I don't know if I have to add groovy scripts or full pipelines?
The groovy scripts seem to be executed in the root instead of my jenkins workspace which is a big issue.
How are you handling this in the right way?

Using shared library is the right approach. You have to add just groovy scripts to your library and use them in your pipeline.
Have a look at an example, Pipeline and the associated library.

Related

Is there some way to pull in Jenkins plugins from a shared pipeline library?

Jenkins publishes plugins which contains primarily, or only, additional pipeline steps.
For example, pipeline-utility-steps would be nice to have, because it has the tar task.
But getting additional plugins installed in the Jenkins instance comes with some difficulty - only one or two devs have access to do so, and they're reluctant to add more plugins, because more plugins installed means more work overall - they have to be tested for cross-compatibility, kept up to date, tracked for vulnerabilities, etc.
Jenkins lets us maintain our own shared pipeline libraries, which we are already using as a place to put all our own custom steps.
Is there some way we can make use of the shared pipeline library feature, to also gain access to existing Jenkins plugins without installing them in Jenkins?
For example, can we directly import a Jenkins plugin into a pipeline somehow to use its steps?
Or can we do something in our shared pipeline library to make it also pull in additional vars from some other Jenkins plugin?
Similar questions:
Is there a way to include a jar in a Jenkins Pipeline folder-level shared library? asks about one specific way this might be possible

What is difference between Jenkins Shared Libraries and Jenkins pipeline templates

I am trying to understand what is exact difference between Jenkins Shared Libraries and Jenkins pipeline templates.
Shared libraries as I understand is used for keeping common code and making it accessible to multiple pipelines.
I am not able to understand then what is difference between Jenkins pipeline template. Also what is the use of Jenkins templates created using template engine. Is it somehow similar to shared library
maintainer of the Jenkins Templating Engine here.
Shared Libraries
Focused on reusing pipeline code. A Jenkinsfile is still required for each individual application and those individual pipelines have to import the libraries.
Jenkins Templating Engine
A pipeline development framework that enables tool-agnostic pipeline templates.
Rather than creating individual Jenkinsfiles, you can create a centralized set of pipeline templates.
These templates invoke steps such as:
build()
unit_test()
deploy_to dev
This common template can be applied across teams, regardless of the technology they're using.
The build, unit_test, and deploy_to steps would come from libraries.
There may be multiple libraries that implement the build step, such as npm, gradle, maven, etc.
Rather than have each team define an entire pipeline, they can now just declare the tools that should be used to "hydrate" the template via a pipeline configuration file:
libraries{
npm // contributes the build step
}
Feel free to check out this CDF Webinar: Pipeline Templating with the Jenkins Templating Engine.

How to develop groovy script effectively for Jenkins pipeline?

I'm very new to Jenkins pipeline and trying to write some Groovy scripts in pipeline. Now, I'm using declarative pipeline and writing Groovy code in Jenkins UI itself which is not helping with auto-population of methods on objects, auto-import etc.
Generally when we use IDE like eclipse, Intellij Idea for Java programming, we can see respective imports are automatically added in the code and also autosuggestion is supported.
How can I groovy code effectively for Jenkins pipeline which would save my time and help with autom-suggestion of methods, imports, compilation error etc?
I have achieved auto-import, auto-completion (and somewhat unit-testing) in IntelliJ using this setup.
The caveat is that I'm using scripted pipeline and the setup is also used for Groovy scripts for Jenkins hooks and Jenkins shared library.

Is it possible to use external jars via #grab from inside the Jenkins groovy postbuild plugin?

I want to send some specific notifications (REST API) back to our server, which triggered our Jenkins build jobs via REST API. For this, I am about to include a jar of our own, which has a lot of convenience methods in it.
I am able to use the jars inside the Groovy Script by utilizing #Grab, that's fine.
But it doesn't seem to work from within Groovy Postbuild Plugin.
It even doesn't recognize referenced script files from within another package, which is next to the script file.
Is there a workaround? How can I send my specific notification using java or groovy and the methods provided in our own jar? Do I need to write a Jenkins Plugin for this (hope not). Thanks in advance, Anne
I would actually recommend to changing your jobs over to Jenkins Declarative pipeline. Then you can use the post handling built into the Jenkins pipeline language. You then have some options. You could use the pipeline steps provided by the HTTP Request plugin to call REST APIs, create a Java tool to call your REST APIs using your Jar library (install as a custom tool which can be used from pipeline) or write a custom plugin to provide the pipeline steps. If you decide to write a plugin have a look at the Webhook Step plugin for how to do it - https://github.com/jenkinsci/webhook-step-plugin. Your best bet might be a custom Java command line tool called from a pipeline shell step (sh or bat) depending on the agent / slave OS you use.

Jenkins Job DSL Plugin - Include another Jenkinsfile

I want to build a Common Jenkinsfile for a couple of Build Jobs in different languages. And then I want to add a specific Jenkinsfile that depends on some parameters.
For example: the common file should contain information about Docker Hub and Nexus Repository. It's always the same. And the specific file should contain language specific build steps.
Is it possible to "include" another file?
Using the Pipeline Shared Groovy Libraries Plugin it is possible to define your own Job DSL. This section of the plugin's manual explains how to do this.

Resources