Shared library in Jenkins with pipeline script - devops

In vars I have tagdeploy.groovy in which parameters are docker repo and image name...how I call that groovy file in my pipeline script and deploy...any help would be appreciated

Are you talking about Jenkins shared library, under your shared library vars folder you have this file called ‘tagdeploy.groovy’ which has a function call() - then you can call this inside scripted or declarative Pipeline using its direct name like a function call - tagdeploy()

Related

Call groovy function in Jenkinsfile, where jenkins and groovy files are in different folders

I have a jenkins pipeline and try to call a printCounter function (on vars folder) in the DQJenkinsfile (on Jenkinsfile folder). The DQJenkinsfile and printCounter file (groovy code) are in different folders. Here is the git repo for that link
In the Jenkins script I created the counter object and make that object call the printCounter function. However, I got error in the jenkins that it is unable to resolve class counter. Could anyone recommend me the solution?

Run external jenkinsfile in another jenkinsfile

let's say I have 'global' Jenkinsfile stored in separated git repo where I've defined all possible stages that any of my pipelines might want to use. Some of those steps are inside if statement to give possibility to skip them if needed.
Is there any chance to in my project create dedicated Jenkinsfile that include this global jenkinsfile and pass some parameter?
Thanks from advance.
The possibility to call certain kind of methods in your pipeline is delivered by JenkinsSharedLibrary. So the best way to do these conditional pipelinesteps would be to define your stages in Closures and then call these Closures as required in your Jenkinsfile.
Example Closure defined in your Shared Library:
//Closure which defines Groovy or Jenkins Pipeline DSL to be executed
Closure javaBuildStage = {
stage('Build Java') {
echo "This is the build stage for Java apps"
sh("./mvn clean package")
}
}
Example Jenkinsfile:
#Library('YourSharedLibrary#master')
if(project == "java"){
javaBuildStage()
}
You can decentralize all your functions with the Jenkins Shared Libraries. The Jenkins Shared Libararies will be located in a Git Repository.
For triggering same Jenkins file for all repositories, you can use Remote File Plugin.
For details you can check this answer
https://stackoverflow.com/a/58877133/6110485

Share Functions among Jobs

I am using Jenkins pipeline, I created 4 Jobs, each job has some functions and Their is a redundant function existing in all those Jobs.
How to make that redundant function in a shared place and all those jobs can call this function ?
You are looking for Jenkins shared library
As the name suggest, you create a library - a pipeline shared among jenkins jobs - in a SCM (git svn ...) and in your project you create a simple Jenkinsfile calling the library.
So, every build will checkout your project, read the Jenkinsfile and then checkout the library with the pipeline.
I did it by:
Created Folder in Jenkins working home
In that folder => I Created file.groovy contains the functions i need
At the end of that file should contain
return this
in JenkinsFile add
node{shared_functionality = load "FilePath.groovy"}
This number four will include functions in .groovy file in your jenkinsFile
So you can add node statement in your JenkinsFiles to include Functions you need

How does DSL extension in Jenkins plugin work

I want to create DSL extension for my Jenkins plugin (built using maven) just like in the example of Docker plugin for Jenkins. I see that the groovy file Docker.groovy is in: src/main/resources/org/jenkinsci/plugins/docker/workflow/Docker.groovy
Does this groovy file have to be within org.jenkinsci.plugin.docker.workflow, or can I just put it inside resources? What is the difference?
Also, If I define my DSL extension within the groovy file in this manner is the DSL extension available to call implicitly in the pipeline file?
In order to make a step available in the Pipeline DSL through your plugin, you need to define a subclass of Step that performs the needed task. This can be completely done within Java, and is the preferred method for adding expanding the Pipeline DSL within a Jenkins plugin.
The Docker example you linked is unusual in this instance, and doesn't define a typical Pipeline DSL step (the docker directive in Pipeline functions like a cross between an agent, a step and a context block). Furthermore, it appears to include a Java class that loads the Groovy script dynamically, which acts as the entry point into the directive.
Groovy can be used to expand the Pipeline DSL; however this is done within the context of a shared library, which is meant to be more of a boilerplate reducing tool to be used internally.

Load multiple scripts from a shared folder in pipeline

I would like to share a global repository with a few python scripts that could be called from the pipeline Jenkinsfile.
I created the global share and added the #Library('...') _ to the Jenkinsfile. It clones the repo specified but I do not know how to call the scripts from that shared pipeline folder or do I have to put the scripts in a resource/ folder?
I haven't been able to find any specifics for this. Some of the scripts in that repo depend on each other. Any help appreciated.
In your Jenkinsfile - load the script with libraryResource
script = libraryResource 'my_script.py'
and use it
sh script

Resources