Skip to content

gfkse/jenkins-shared-library

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Declarative Pipeline - Jenkins shared library

One has to note the difference between Declarative [1] and Scripted [2] pipelines, mainly it can be summed up to:

  • declarative p. → must be enclosed within a pipeline {} block [3]

  • scripted p. → always are enclosed within a node {} block

Use case

We chose the declarative [1] approach because this one suited our scenario better. We had to deploy Jenkinsfile in +10 repositories and there was alot of duplicate code, not to mention that when our DevOps where fixing some of the Jenkinsfile, they had to update all the repos.

Therefore, we started from ChrisCooney/jenkins-global-library-simple [4] pushed alot of failing builds to understand how everything works, and when we eventually reached our first working build, we started refactoring everything and splitting the repeating bits into separate reusable components.

Prerequisites

  • secrets [5] must be configured in Jenkins prior to deploy these to your instance

  • configure Jenkins shared-library repository in Jenkins Admin [6]

  • Jenkinsfile will be deployed in each repository (must be in project root dir)

  • shared library must be imported and the pipeline function must be called

Jenkinsfile
@Library('name-of-library-configured-in-jenkins-admin') _
evenOrOdd(currentBuild.getNumber())

How it’s structured

Important
declarative [1] pipeline scripts (written in Groovy) must be placed in the project’s ./vars/ directory in order to be picked up by Jenkins
|-src
|---com
|-----gfk
|-------jenkins
|---------components/     <-- classes (mainly what you put in Jenkinsfile script {} blocks)
|-vars/                   <-- declarative pipelines (*.groovy scripts)

Basically, there’s a unified repository that contains our shared-library stuff and all other repositories just import this library and call the pipeline function name (in our case it’s the file name - CamelCased).