Jenkinsfile
This page describes, how to do things in scripted Pipeline (Groovy) Jenkinsfiles. This link is a good starting point to learn more about Jenkinsfile.
General
The Jenkinsfile must be located in the root folder of a repository / branch, to be detected by Jenkins automatically. The Jenkinsfile defines, how the build job is performed by Jenkins.
Properties
At the top of the Jenkinsfile properties can be called with an array of global properties that will be set for the pipeline.
... , ... , ...
])
Please note that another call to properties() will overwrite the previous one!
Build Discarder
At the top of the Jenkinsfile add a buildDiscarder to the properties to let Jenkins delete old artifacts or complete job executions:
buildDiscarder(logRotator(numToKeepStr: '50', artifactNumToKeepStr: '5'))
])
Parameters for logRotator (from the source code):
- daysToKeepStr: history is only kept up to this days.
- numToKeepStr: only this number of build logs are kept.
- artifactDaysToKeepStr: artifacts are only kept up to this days.
- artifactNumToKeepStr: only this number of builds have their artifacts kept.
Pipeline Triggers
Instead of having pipelines just triggered on source code changes you can also use cron syntax to define dates and times that shoud automatically trigger the pipeline.
pipelineTriggers([cron('TZ=Europe/Berlin \n 00 08 * * 1-5')])
])
The above example triggers the pipeline at 8:00 on Mo-Fr using the local German time. Without the TZ specification it will use the timezone of the Jenkins server which is UTC for DevOps-as-a-Service.
More about the cron spec is documented at https://jenkins.io/doc/book/pipeline/syntax/#cron-syntax.
Durability Settings
At the top of the Jenkinsfile add a durabilityHint to the properties:
durabilityHint('PERFORMANCE_OPTIMIZED')
])
The possible values and their meaning are documented at: https://jenkins.io/doc/book/pipeline/scaling-pipeline/
If unsure, please don't change the default!
Automated building
The best and easiest approach to use the power of Jenkins pipelines is to use the automatically provided Jenkins Shared Library which will detect the type of your project and automatically build and test it. The pipeline also provides optional deployments to target environments. See Jenkins Shared Library for more information!
Building with tool plug-ins
Some build tools have a special plug-in support in Jenkins which means you can easily call them on any Jenkins node. Jenkins will make sure to install and update them for you. In your Jenkinsfile you just refer with symbolic names to the major version of the tool you want to use. The DevOps-as-a-Service team will make sure that the latest Long Term Support (LTS) version of the tool will be automatically available for you. Most of the time these updates are done together with the update of your Jenkins controller instance.
We strive to make the exact version of the different tools publicly available on the start page of your Jenkins. See the screenshot below as an example. Put please check the current versions available to you at https://CUSTOMER.devops.t-systems.net/jenkins/ where CUSTOMER needs to be replaced by the name of your instance.
Maven
Supported symbolic name is 'maven'. The exact version can be looked up at the start page of your Jenkins, e.g. https://CUSTOMER.devops.t-systems.net/jenkins/
{
sh 'mvn --version'
}
For more information about the withMaven pipeline step see https://www.jenkins.io/doc/pipeline/steps/pipeline-maven/. When you use the Jenkins Shared Library a maven build will be automatically done when a pom.xml file is found.
JDK (Java Development Kit)
The supported symbolic names are listed at the start page of your Jenkins (e.g. https://CUSTOMER.devops.t-systems.net/jenkins/) and start with 'jdk' or 'corretto'.
The following examples use jdk17.
env.PATH="${env.JAVA_HOME}/bin:${env.PATH}"
sh 'java -version'
When you use the Jenkins Shared Library you simply do
sdcPipeline(jdk: 'jdk17')
to use the JDK version of your choice. See Jenkins Shared Library for more information.
nodejs
The supported symbolic names are listed at the start page of your Jenkins (e.g. https://CUSTOMER.devops.t-systems.net/jenkins/) and start with 'nodejs'. As an example 'nodes16' would be refering to the latest release of nodejs v16. The short name 'nodejs' is usually set to the recommended LTS version of nodejs.
The following examples use 'nodejs18'.
env.PATH="${env.NODEJS_HOME}/bin:${env.PATH}"
sh 'node --version'
sh 'npm --version'
When you use the Jenkins Shared Library you simply do
sdcPipeline(nodejs: 'nodejs18')
to use the nodejs version of your choice. See Jenkins Shared Library for more information.