• April 18, 2018
  • Iurii Medvedev

Jenkins as CI/CD tool

Jenkins Logo

Jenkins offers a simple way to set up a continuous integration or continuous delivery environment for almost any combination of languages and source code repositories using pipelines, as well as automating other routine development tasks.

While Jenkins doesn’t eliminate the need to create scripts for individual steps, it does give you a faster and more robust way to integrate your entire chain of build, test, and deployment tools than you can easily build yourself.

Tips and tricks

Getting list instaled plugins

Go to Jenkins script console (http(s)://JENKINS_URL/script) and execute script below

  plugin ->
    println ("${plugin.getDisplayName()} (${plugin.getShortName()}): ${plugin.getVersion()}")

As result you get list of a kind:

Name_of_the_plugin Short_name Version

If you want use list of plugins for auto installation, just modify previous script

  plugin ->
    println ("${plugin.getShortName()}")

Jenkins delete failed build

Go to Jenkins script console (http(s)://JENKINS_URL/script) and execute script below

MAX_BUILDS = 2 // max builds to keep

def jobs = Jenkins.instance.items;

for (job in jobs) {
    println "Job: " + job.name
    def recent = job.builds.limit(MAX_BUILDS)
    println "Recent Builds: "  + recent
    println "============================="
    for (build in job.builds) {
        if (!recent.contains(build) && !build.isBuilding()) {
            println "Deleting: " + build
            println ""

This script will delete failed build

Jenkins disable CSP (Content Security Policy)

If you want temporarily disable CSP. Go to Jenkins script console and execute command

System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "sandbox allow-scripts; default-src 'self'; style-src 'self' 'unsafe-inline'; script-src * 'unsafe-inline';")

If you want permanently disable CSP.

If you are running Jenkins on Ubuntu:

$ vim /etc/default/jenkins

Find the line with JAVA_ARGS and add the CSP policy like this: JAVA_ARGS="-Djava.awt.headless=true -Dhudson.model.DirectoryBrowserSupport.CSP=\"default-src 'self' 'unsafe-inline' 'unsafe-eval'; img-src data:;\""

If you are running Jenkins on CentOS:

$ vim /etc/sysconfig/jenkins
Find the line with JENKINS_JAVA_OPTIONS and add the CSP policy like this: JENKINS_JAVA_OPTIONS="-Djava.awt.headless=true -Dhudson.model.DirectoryBrowserSupport.CSP=\"default-src 'self' 'unsafe-inline' 'unsafe-eval'; img-src data:;\""

After changes you should to restart Jenkins service

How to get all Jenkins slaves ip

Go to Jenkins script console (http(s)://JENKINS_URL/script) and execute script below

import hudson.model.Computer.ListPossibleNames

for (slaves in hudson.model.Hudson.instance.slaves) {
  println('Name: ' + slaves.name);
  def node = jenkins.model.Jenkins.instance.getNode( slaves.name );
  println node.computer.getChannel().call(new ListPossibleNames());


Deliting unused workspaces

Just create new pipeline job with script

import com.cloudbees.hudson.plugins.folder.Folder
import hudson.FilePath
import jenkins.model.Jenkins

def boolean isFolder(String name) {
    def item = Jenkins.instance.getItemByFullName(name)
    return item instanceof Folder

def deleteUnusedWorkspace(FilePath root, String path) {
    root.list().each { child ->
        String fullName = path + child.name
        if (isFolder(fullName)) {
            deleteUnusedWorkspace(root.child(child.name), "$fullName/")
        } else {
            if (Jenkins.instance.getItemByFullName(fullName) == null) {
                println "Deleting: $fullName "

for (node in Jenkins.instance.nodes) {
    println "Processing $node.displayName"
    def workspaceRoot = node.rootPath.child("workspace");
    deleteUnusedWorkspace(workspaceRoot, "")