• Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Alex Aitken

Technical and Engineering Leadership, Coaching, and Mentorship

Adding a Shared Script for Jenkins

October 1, 2018 By Alex Leave a Comment

Recently, we discovered that we had a lot of duplication in our build scripts. Pretty much the same copy+paste code everywhere (we have a mono-repository). So, it came to me to try and resolve this duplication of logic across our builds (multiple services).

For context, we use the declarative pipeline, which for some projects involves utilising a docker image to set up dependencies. This makes it harder to include a shared script as things like “load filename.groovy” won’t work. But, there is a way around this.

There is a function, and you use a trusted library that’s in your source code. There are other ways to do this such as specifying a repository where some “shared code” lives and using that. But for us, we wanted a more straightforward way.

evaluate readTrusted('./path/to/your/file.groovy')

Now, you can only do this when you have the source code checked out. We also evaluate our script inside of the pipeline as an extra step – in which we also set some variables that we can use.

So, what’s happening in the file.groovy? Well, it’s pretty simple actually. It’s the same sort of library file you would expect when you use load.

And that’s it. Now your declarative pipeline can have shared code within one repository without having to use the shared library functionality from Jenkins (which looks like a lot of work to me). It also means that you can test this per branch. So, you can make changes on a branch to the common code and test it out before merging to master.

I hope you found this useful. It took me a lot of searching to find this functionality. 

Reposted on Medium.

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook

Like this:

Like Loading...

Related

Filed Under: Coding

Alex

Reader Interactions

Leave a ReplyCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

About the author

Alex is an AVP of Engineering currently working at Bukalapak. He is a leader in full-stack technologies. Read More…

Pages

  • Speaking Experience
  • About

Social Profiles

  • LinkedIn
  • Medium
  • ADPList

Recent Posts

  • Interviewing as an Engineering Leader
  • Managing Low Performers
  • Getting Docker, React, .NET Core, Postgres, and Nginx Playing Nice
  • What Makes a Good Software Engineering Manager?
  • “Am I There Yet?” Said an Engineer

Archives

  • January 2025
  • August 2024
  • July 2024
  • October 2023
  • August 2023
  • October 2020
  • May 2020
  • February 2020
  • June 2019
  • March 2019
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • January 2018

Categories

  • Coding
  • Essay
  • Leadership
  • Management
  • Roundtable
  • Strategy

Footer

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy

Copyright © 2025

%d