Jenkins Pipeline - Call functions in shared jar - jenkins

So here is my project setup
A separate groovy project
Multiple pipelines
All the pipeline scripts refer to the shared groovy project. I went through the shared libraries and all of the needs to be registered in Jenkins global configuration.
Is there any way to do without it? I tried using Grab but ended up with the error
java.lang.RuntimeException: No suitable ClassLoader found for grab

Firstly for Grab to work your Jenkins needs to have access to the internet.
Shared Libraries are definitely the way to go here.
Like many things the secret sauce is in the syntax.

Related

Sonarscan using JPAC for angular project

I have to work on sonar scan for an angular project using JPAC (jenkins pipeline as code). I have searched on the internet but couldn't find any proper tutorial how to do it. Some says we need to have sonar-project.properties file in the root dir, but that doesn't work in Jenkinsfile. How to write properties in Jenkinsfile and what command needs to run to trigger sonar scan? Can someone please provide some resources or tell exactly how to do it?

How can I have a non-global (locally scoped) shared library for a declarative pipeline?

I would like to define some groovy code that is imported into one or more declarative pipelines that are all stored in the same git repo.
I do NOT want to create a global shared library that different pipelines from different repos share.
For example, in a repo I might have the following files:
shared-library.groovy
pr-pipeline-unit-tests.groovy
pr-pipeline-ui-tests.groovy
I want both pr-pipeline-unit-tests.groovy and pr-pipeline-ui-tests.groovy to be able to import shared-library.groovy.
These pipelines are executed on PRs, and updates to shared-library.groovy should only affect that PR - this is why I do not want a jenkins globally stored shared library.
As bonus, it would be cool if shared-library.groovy could be a standalone gradle project that is "imported" into the pipelines, similar to how buildSrc is imported into gradle project configuration files. Even better if the shared code could be Kotlin!

Jenkins/Groovy move variables out to a config file

I've been asked to move some variable from a Groovy script out into a configuration file. I'm fine using something like :-
readFile('../xx-software.cfg').split('\n').each { fileName ->
sh "wget ${theURL}${fileName}"
}
However, even though I have added xx-software.cfg into the same directory as my Groovy script it does become available for use within that groovy script.
I hope this makes sense!?
How can I move my variables out into a config file to make it easier for the application support team to make future edits without changing the code?
There are a few approaches you could use.
Firstly, file format for the configuration and how to read the data into variables. You could use Java Properties format, YAML or JSON and these are all handled by the Pipeline Utility Steps plugin with steps here. You can read the file with these steps:
readProperties
readYaml
readJSON
Next problem, how to get the file available to your pipeline so it can be read from the workspace using these steps. Possibilities are:
In source control with your pipeline code. It can be fetched with the pipeline.
In a separate source control for configuration, your pipeline will need a step to fetch it.
Use the Jenkins Config File Provider plugin. It has a step to provide a config file managed in Jenkins.
Provide it as a Custom Tool zipped archive from a binary server like Artifactory. You can use custom tool definition pipeline steps to make this available to the pipeline.
The Config File Provider option might provide any easy way to have a file that can be updated, but there won't be any version control of it.

Add dependency to jenkins job dsl

I'm trying to add a dependency to my seed job, but no matter what I try, I always get the exception in Jenkins that it can't find the classes that I import in my groovy job. I've tried adding the dependency as compile, testCompile, lib, everything in my build.gradle file, but it doesn't seem to do anything... I'm trying to import org.yaml.snakeyaml.Yaml from the org.yaml:snakeyaml:1.17 dependency.
Any idea on how I can somehow get jenkins to get a hold of that dependency when trying to execute that seed job?
Thanks!
Alternatively you might use Grape to download any dependency directly from your Jenkinsfile. If you add
#Grab(group='org.yaml', module='snakeyaml', version='1.20')
on top of your Jenkinsfile, Jenkins pipeline will download this dependency and it will get available in your pipeline script.
Never mind, I've found the solution. I copied the dependency to a specific folder during the gradle build and added that to the additional classpath of the job dsl. It works now!

Jenkins environment variables

I am a novice to Jenkins administration, trying to help a friend set up Jenkins for his startup.
I know there are plugins in Jenkins but not quite familiar on how to achieve his requirements.
He has a spring application which uses dispatcher.xml to reference jdbc.properties (one for each environment eg: jdbc_dev, jdbc_test, jdbc_prod.
How do I setup environment variables in Jenkins so the respective jdbc.properties is picked when the war file is built ?
I think following plugins will help your friend a lot.
1) EnvInject Plugin:- To inject environment variables depending on the requirements.
2) Role strategy :- To manage authorization and permissions of various users. For eg: Admin,Tester,Developer,guest.
For more info on configuration on this plugin read this Answer
3) JobConfigHistory Plugin:- To revert changes if you screw anything.
Hope it helps.
See Parameterized Build:
Sometimes, it is useful/necessary to have your builds take several "parameters".
...
The parameters are available as environment parameters variables. So e.g. a shell ($FOO, %FOO%) or Ant (${env.FOO}) can access these values.
[Corrections by me.]
See also the Matrix Project Plugin:
A multi-configuration project is useful for instances where your builds will make many similar build steps, and you would otherwise be duplicating steps.

Resources