Using properties in Jenkins scripted pipeline

Using properties in Jenkins scripted pipeline

The open source CI/CD software Jenkins got a really cool integration of pipelines. If you are using the version 1.x you have to install a pipeline plugin, but it is highly recommended to use the newer version 2.x. In this version, you can also install the new UI called “blue ocean”. It’s a new way to display pipelines and the actions inside them, but let’s get back to topic.

Jenkins scripted pipeline as engine for automation

If you are using Jenkins pipelines (version 2.5+) within multibranch, the best way is via Jenkins file inside a git repository, you can’t edit the settings of the job. It is only possible to take a look at it, but you can’t save your changes. It’s possible to issue some lines inside the pipeline syntax to take control of these properties and settings. Therefore the is a special part of the syntax, only for properties in front of the job, but you have to take a deeper look on what type of pipeline you are using. Jenkins provides two types of pipelines.

First one is the declarative pipeline and Jenkins.io describes it as follows: “Declarative Pipeline is a relatively recent addition to Jenkins Pipeline which presents a more simplified and opinionated syntax on top of the Pipeline sub-systems”. You can describe the agent, options, parameters or triggers. Everything is in the top part of the syntax, so you can be sure it’s loaded when your pipeline will start.

jenkins-pipeline

The other side is the scripted pipeline as Jenkins.io described it like this: “Scripted Pipeline, like Declarative Pipeline, is built on top of the underlying Pipeline sub-system. Unlike Declarative, Scripted Pipeline is effectively a general purpose DSL built with Groovy. Most functionality provided by the Groovy language is made available to users of Scripted Pipeline, which means it can be a very expressive and flexible tool with which one can author continuous delivery pipelines”. By the way, DSL here means “Domain Specific Language” what is very good described here at wikipedia.org.

Practically use of properties

For a configurational reason, in my cases, we are using the scripted pipeline type. First thing, the documentation of it on the official site includes round about 30 lines. If we take a deeper look at declarative pipelines it’s 10 times bigger :-D. So what, we have to get a running syntax to use the properties on top of scripted pipelines because we want to use cronjobs triggered by Jenkins.

Inside the documentation of the declarative pipeline, it is described this way

Jenkinsfile (Declarative Pipeline)
pipeline {
   agent any
   triggers {
       cron('H 4/* 0 0 1-5')
   }
   stages {
       stage('Example') {
           steps {
               echo 'Hello World'
           }
       }
   }
   }

But this won’t work inside the scripted pipeline, believe me, I’ve tested it :-D. It is possible to change properties and options if we make something like this here.

properties(
           [
                   pipelineTriggers([cron('0 2 * * 1-3')])
           ]
   )

Inside this properties part, we can add as much “settings” and “parameters” we want. It’s separated by a comma and for a better reading by line. So you can add for example params or options like the log rotator.

properties(
   [
       [
           $class: 'BuildDiscarderProperty',
           strategy: [$class: 'LogRotator', numToKeepStr: '10']
       ],
       pipelineTriggers([cron('H/30 * * * *')])
   ]
   )

1 Comment

  • Johannes

    This is awesome! The first webpage I found after hours of searching explained how to declare parameters in scripted! Thanks very much.

Leave a Reply

Your email address will not be published. Required fields are marked *

© 2022 Sascha Lewandowksi