Global variables for Jenkins pipelines

--

Ever found yourself needing to be able to use the same variable over and over for many pipelines? Here’s how:

  1. Make a vars/globals.groovy file in your shared library
  2. Put this in it:
#!/usr/bin/env groovy
import groovy.transform.Field

@Field final String GITHUB_URL = 'https://github.com/'
@Field final String GITHUB_SLUG = 'k-caps'

3. Access the constant from your Jenkinsfiles:

repoUrl = globals.GITHUB_URL + globals.GITHUB_SLUG

You can access it from other files under vars the exact same way, too.

--

--

Kobi Rosenstein
Kobi Rosenstein

Written by Kobi Rosenstein

Devops engineeer. This blog chronicles my “gotcha” moments — Each post contains an answer I would have like to have found when trawling google.

No responses yet