Shared library for the DSL scripts - jenkins

How can i use shared library in jenkins for my dsl groovy scripts?
Most of my scripts have same steps except few changes in name and url, i do not want to create new script every time. Is there any way one script and do the job?

Yes, you can create a Jenkins shared library and configure it in Jenkins.
The shared library is a git repository with defined structure where you declare your shared steps and you can include it in each pipeline script with #Library('<library name>').

Related

jenkins-as-code: purpose of jobs

I want to use Jenkins and store the configuration and the pipeline in my SCM(e.g. git). To do so, I created a directory, let's say "jobs" in the root of my project where I will store jobs.groovy files written as JobDSL plugin files.
Should I do all the things in a single job file, like fetching the source code, testing it, maybe building Docker images if necessary, then deploying on AWS cloud? Or for each operation, should I create different jobs? If so, then how can I create a pipeline using these job files?
look at jenkins configuration as code plugin. following link would be helpful
https://github.com/tomasbjerre/jenkins-configuration-as-code-sandbox

Reuse jenkins pipeline or groovy script?

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.

How do I have to execute a managed script in a Jenkins pipeline

I'm creating a jenkins pipeline. We have a bash script wich need to be executed but which is not in the repository itself.
How do I have to execute this script? I tried:
configFileProvider: use the configFileProvider to get the script in a variable. The execution did not work and I'm also thinking this is not the way to do it? This is meant for config files and not real scripts?
I have a shared library which contains resources/. From here I've copied the script and executed it with sh after copying the content in a file.. This went well but I have the script in my workspace which I do not prefer. I want to execute commands on my workspace but when I perform git commit's etc. I don't want the file to be in my workspace if that's possible.
What is the right way to execute a managed script (from managed scripts or from in git) in my jenkins pipeline?
Without pipelines I use: Managed scripts. When I execute it I see in the logs:
executing script 'test-xxx.sh'
[test-xxx] $ /bin/bash /tmp/build_step_template3284004xxx.sh param1 param2 param3
This is the ideal solution I want to replicate in Jenkins pipelines.
My script which is NOT in my workspace but is executed on my workspace and is temporary.
I used to use the Scriptler plugin, however it had security issues raised against it.
When I moved all my stuff from freestyle builds to pipelines (expressed in Groovy) in 2017/8 I migrated that functionality to Shared Libraries (as an aside: right place to put code that runs against the Jenkins object model).
All paths in a Jenkins job (freestyle or pipeline) are %JENKINS_WORKSPACE% relative. Jenkins does not natively like you going above this directory on the filesystem.
I would highly recommend all the stuff your build process needs be in the primary Jenkins workspace (preferably via a git checkout or a shared library). Versioning everything in git will always be your friend. If this is not what you want to do, other options could be:
Shared Workspace
This post

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.

Jenkins: Can I Generate groovy script from existing Job?

I have a job in Jenkins and want the groovy script for the same? Is there a way I can do that?
I have created a Job using Jenkins, like add shell Command, Sync from repo,etc. Now I wish to have groovy script for the same, but I dont want to go to the trouble of writing the entire thing again. Is there something like Export to .groovy?
P.S. I am not sure of the correct tags.
You want to copy your configuration using Groovy script to create a new job or to persist.
Below Links are helpful for clone/ copy and create jobs using groovy script.
http://jenkins-ci.361315.n4.nabble.com/How-to-Copy-Clone-a-Job-via-Groovy-td4097412.html
Can I use Jenkins CLI or some groovy scripts to create a new job
https://wiki.jenkins-ci.org/display/JENKINS/Clone+all+projects+in+a+View
In case you want to persist your Job, always backup your resources file such as config.xml, jenkins.xml,etc..
you can recreate job from config.xml which holds all your job configuration
Check out this plugin. It seems to be appropriate for your purpose. Though it has some restrictions for plugins in your jobs that are not capable of Pipeline syntax. Anyway, it could be useful for code generation.

Resources