Loading Custom jar files in Jenkins without the Classpath Field - jenkins

How can I load a custom jar file for my Jenkins Jobs DSL groovy scripts without the classpath field?
I've inherited a Jenkins instance. This Jenkins instance has an older version of the Jenkins Jobs DSL plugin. This version of the jobs plugin has a class path fields
By loading this jar file, my script (in the DSL Scripts) has additional symbols/classes it may import.
However, the latest versions of the Jenkins Jobs DSL have removed this class path field. The impression I get from the mailing list) is this was done for security reasons.
Without this option, is it possible to set a different class path (or include extra jar files in my Jenkins Jobs DSL groovy scripts without this field?

No, there isn't any direct option available. Following are the options available
Option 1: As Daniel has suggested (https://groups.google.com/forum/#!topic/job-dsl-plugin/lYgX3boW0Pk) , you can use the Script Security Plugin to add custom classpath and include the jar(s). The only overhead with this approach is, even if there is a minor difference in the jar, an approval would be required.
Option 2(Least Preferred, Last Option): If you are running Jenkins under a company network and if your confident on security, then you can consider to uncheck the Enable Security for Job DSL from Jenkins-> Configure Global Secutiry

Related

Jenkins Execute Groovy Script Before Loading Plugins

I need to change some plugin config files before plugins are loaded. I looked into the init.groovy.d, however it seems to run Groovy scripts in that directory after plugins have been loaded and therefore would require a restart to apply. Is there a way to run Groovy scripts before Jenkins loads the plugins?
What you are requesting is not necessary. Generally, when adding plugins, they come unconfigured. Jenkins starts, loads the plugins, then you can configure via init.groovy, CasC, etc., similar to you adding via GUI (add, restart, configure).
We start w/war file, wrapper, init.groovy.d, plus a variant of the docker install_plugins.sh. Other than the war, the wrapper and wrapper.conf, install_plugins.sh and plugin list, and all the init scripts are controlled in a git repo, which we pull down.
dumping the plugins into the plugins dir, then launch jenkins.sh.
The init.groovy runs automatically after initialization and configures all system, global, tools and plugins values, as well as credentials values, also creating/configuring nodes.
NB: best to use 1 init script per section or plugin as a failure in any init script will quietly fail, effectively skipping the rest of the script.
You may need to .save() after setting most parameters via init.goovy. Perhaps that's why you did not see the changes.
If you were really paranoid, you could first invoke Hudson.instance.doQuietDown(), which effective blocks the queue (multiple init.groovy scripts execute in lexical order), do all the configurations, then invoke doCancelQuietDown(), but we'd had no issues w/o that.
This approach (init.groovy.d) works fine, but looking to switch to JCasC now that it's matured. CasC is a simpler to manage (again, using separate config files for each plugin) and read.

Missing Ant Global Tool Configuration in Jenkins

Why doesn't the Ant Configuration in my Jenkins appear under Global Tool Configuration?
See the image below:
There are only tabs for JDK and Maven.
In the other references I checked, there are others like Git and Ant, etc.
Make sure that the Ant Plugin is installed in your Jenkins instance. Go to Manage Jenkins -> Manage Plugins and search for the plugin there under the Available tab.

Howto set up classpath for System Groovy Script in Jenkins

Documentation for the Groovy Plugin of Jenkins states that
The system groovy script, OTOH, runs inside the Jenkins master's JVM.
Thus it will have access to all the internal objects of Jenkins, so
you can use this to alter the state of Jenkins. It is similar to the
Jenkins Script Console functionality.
Yet I find that I have a groovy script that I can successfully run in Jenkins Script Console but which does NOT run if entered as a "System Groovy Script" on a build configuration. There are compiler errors. Clearly, the Jenkins Script Console is running with a different classpath than the script in my build. But I can't find information on what the default classpath is when running a script for a build or what the classpath is when running from the Script Console, so I might duplicate that for my script.
Also, the plugin offers a classpath entry field for running the script as a file but that option does not exist for entering the script as text.
I can't get my script to work either way.
What am I missing?
I think the answer is that the Script Console auto-imports the whole Jenkins library. That is not the case with the System Groovy Script. So what worked for me was to run the script, and for every compiler error about an unknown class, add an import statement for that class. I learned what packages they were from by looking at Javadocs.
Automating this would be a nice improvement to the plugin.
May be use the grab dependency management to resolve the library to add

Search for a module in all Jenkins jobs

Is it somehow possible so search for a module within all Jenkins jobs?
Let's say there's the module common-messaging. It's built in Job 1 and Job 2.
When I search for the module name I want both jobs to be shown.
Jenkins store the jobs configuration on disk in XML files. So you can grep the file for whatever XML element is created by the plugin.
create a new job that just have that plugin enabled.
get the XML file: /ci/job/name_of_your_job/config.xml
look for an XML element that correspond to common-messaging, lets say common-messaging.plugin
Then if using a UNIX system:
grep common-messaging.plugin /var/lib/jenkins/jobs/*/config.xml
Windows must have a similar command.
If you have admin access to the server, install the Scriptler plugin (which gives you more easy access to the Groovy console), and fetch and run this shared Scriptler snippet: http://scriptlerweb.appspot.com/script/show/486001

Is it possible to run Sonar plugin on Jenkins without any build process?

I would like to run Sonar plugin on Jenkins without any build process (my intent was to integrate Sonar analysis within Jenkins and take advantage of the subversion plugins and configurations we already had on there).
I do not want to run the build process since that would take up unnecessary time; I would only like to have a Jenkins job dedicated for Sonar analysis.
You can do that. You have to triggering the analysis with the SonarQube Runner.
Go to the Build section, click on Add build step and choose Invoke Standalone Sonar Analysis
Configure the SonarQube analysis. You can either point to an existing sonar-project.properties file or set the analysis properties directly in the Project properties field
When you analyse with SonarQube Runner , then you should give the following mandatory properties:
sonar.projectKey=my:project
sonar.projectName=My project
sonar.projectVersion=1.0
# Path to the parent source code directory.
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# Since SonarQube 4.2, this property is optional. If not set, SonarQube starts looking for source code
# from the directory containing the sonar-project.properties file.
sonar.sources=src
In this case you may miss some rule violations (like FindBugs), because .class files are not provided. You have to build the project manually and set the sonar.binaries property to your class files. If you never want to build the project, then you can use the SourceMeter plugin for SonarQube too. It only needs the source files, but can produce more metrics and issues if you needed.

Resources