Groovy 3 and Jenkins Embedded Scripting? - jenkins

I've just started playing around with Jenkins - and I'm looking at Pipelines.
I'm looking to brush-up on my Groovy skills to facilitate this.
The latest literature is all "Groovy 3" based and comes with some significant new features, but despite having the latest Jenkins install (Jenkins 2.249.2), the embedded scripting engine seems to be older - from Jenkins' script console on my master:
println GroovySystem.version
Gives 2.4.12.
My question - Is the version of Groovy easily/sensibly configurable in Jenkins or is it usual to stick with whatever ships with Jenkins?

You must choose system groovy if you want ready access to the Jenkins internals, jobs, etc. See Groovy Script vs System Groovy Script, and Known limitations; you get what is packaged.
If you use external groovy and don't need to access Jenkins internals, your choice. Lots of internal debate on upgrading as it's lots of work. You can follow JENKINS-51823 and Pipeline Groovy Epic

Related

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.

How to add Sonarqube into Jenkins

I have installed sonar and jenkins. Now I want to add Sonarqube into Jenkins. But in the manage plugins, it doesn't show me the Sonarqube.the display I get
SonarQube is a standalone server. It offers a web user interface to visualize bugs, code smells and vulnerabilities. You cannot include this web-ui SonarQube in Jenkins.
However, you can trigger a scan as part of your Jenkins job. This scan can "send" its findings into a SonarQube installation - either hosted by your own (on-premise), or using the hosting offer at sonarcloud.io.
There are a couple of different ways to include the scanner in your job, but the setup is specific to your programming language and build tools (Maven, VisualStudio, command line, ...). Check the sonarcloud docs for the way, that fit's best to your situation.

How does the Build Pipeline Plugin relate to the Jenkins 2 Pipeline Plugin?

Currently, I use the Build Pipeline Plugin to orchestrate the delivery of my code through the different environments:
Build the code and execute unit tests
Manually deploy to the development environment
Automatically execute tests on the development environment
Manually release the software and put the version number to the released version.
Manually deploy to the integration test environment by downloading the artefact from a repository, based on the version put by the release build.
Manually deploy to ...
With Jenkins 2.0 comes the Pipeline plugin. But how do these two plugins relate to each other?
Should I migrate to the latest plugin? The things I seem to miss from the Jenkins 2 Pipeline plugin:
Manually trigger a stage. I can wait for an input, but it does not seem to be so elegant
Restart a stage to retrigger a deployment. This does not seem possible.
Visibility into the parameters that were used to trigger a stage, e.g. the version number of the software that was deployed.
Am I missing the point here? Should the two of them be combined? Or how are you approaching a pipeline like this?
With the current state of Jenkins 2 pipelines you are correct to state all the 'missing features' you listed.
One of the advantages of the Jenkins 2 pipeline plugin is that rather than chaining together a series of jobs as with the Build Pipeline Plugin, your entire pipeline is 1 'job', which makes user administration much easier IMO.
The other advantage of Jenkins 2 pipelines would be 'configuration as code', so you can track changes to your pipeline as you would any other file in version control.
Jenkins 2 pipelines are very much the new 'hotness', and there's many plugins implementing compatability day after day.
Once the new UI becomes production ready, I'd imagine that the old build pipeline plugin will begin to be deprecated.
Also you should be aware that the Build Pipeline plugin is not maintained by the Jenkins or CloudBees teams as far as I know, whereas Jenkins 2 pipelines are.
Would I recommend migrating now? No, I personally still don't consider the Jenkins 2 pipelines mature enough for deployment to production in an organisation. I'd stay to stick with what you know for now while you wait for the Jenkins 2 Pipeline ecosystem to mature.
My reasoning I gave in a blog post a few weeks ago (read more here if you want, but I've extracted out the 'weaknesses' here for you):
There are still plenty of plugins that I and many others will consider 'core to their CI pipeline' missing full or partial support for pipelines.
The lack of 'per-project-configuration' in pipelines for many plugins. e.g. Slack - the current implementation 'assumes' that all Jenkins 2 Pipeline projects should be communicated to the same Slack channel/team - whereas you may want to have multiple Slack teams configured. There are multiple other plugins like this.
At present the documentation of Jenkins 2 Pipelines is very limited, though this is improving.

Configure block equivalent for Jenkins 2.0 Pipeline

For the Job DSL Plugin (https://github.com/jenkinsci/job-dsl-plugin) you can script your Jenkins jobs using this plugins DSL script with the help of their built in methods to support most plugins.
When you encounter a plugin that is not yet supported you can still use it by using a "Configure" block (https://github.com/jenkinsci/job-dsl-plugin/wiki/The-Configure-Block) that tells the plugin how to build the XML manually.
Is there an equivalent feature for the new Jenkins 2.0 pipeline (https://jenkins.io/solutions/pipeline/) to support plugins that are either not updated often or at least until the plugin author adds support?
You should have a look at new Jenkinsfile capabilities. In essence, you can now configure your entire job using the Jenkinsfile and pipeline approach.

How to identify projects using a specific JDK installation in Jenkins

I would like to clean up the list of JDKs managed by our Jenkins CI server.
For instance there are several versions of JDK 8 whereas I'd like to only have one of them (the latest). So I am looking for a simple way for identifying all those Jenkins jobs explicitly using one of the other JDK installations so I can change them to use the one remaining JDK install.
Is there a better way to do so apart from manually clicking through all jobs and examine which JDK they use?
I often use the Jenkins Script Console for tasks like this. The Groovy plugin provides the Script Console, but if you're going to use the Script Console for periodic maintenance, you'll also want the Scriptler plugin which allows you to manage the scripts that you run.
From Manage Jenkins -> Script Console, you can write a groovy script that iterates through the jobs:
for (job in Jenkins.instance.items) {
println job.name
...examine more details of job...
}
It often takes some iteration to figure out the right set of job properties to examine. It's not clear to me how your jobs are configured, so you'll have to figure out what property describes the JDK used by the job. The Change JVM Options script might provide some hints.
As I describe in another answer, you can write functions to introspect the objects available to scripting. And so with some trial and error, develop a script that walks the list of jobs and examines the properties that you're interested in.

Resources