Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I import a groovy script from a relative directory into a Jenkinsfile?

I've got a project structured like this:

/
/ Jenkinsfile 
/ build_tools /
              / pipeline.groovy # Functions which define the pipeline
              / reporting.groovy # Other misc build reporting stuff
              / dostuff.sh # A shell script used by the pipeline
              / domorestuff.sh # Another pipeline supporting shell-script

Is it possible to import the groovy files in /build_tools so that I can use functions inside those 2 files in my Jenkinsfile?

Ideally, I'd like to have a Jenkins file that looks something like this (pseudocode):

from build_tools.pipeline import build_pipeline
build_pipeline(project_name="my project", reporting_id=12345)

The bit I'm stuck on is how you write a working equivalent of that pretend import statement on line #1 of my pseudocode.

PS. Why I'm doing this: The build_tools folder is actually a git submodule shared by many projects. I'm trying to give each project access to a common set of build tooling to stop each project maintainer from reinventing this wheel.

like image 545
Salim Fadhley Avatar asked May 09 '17 22:05

Salim Fadhley


People also ask

Does Jenkinsfile use Groovy?

Within a Pipeline Project (read plugin), Jenkins introduces a domain-specific language (DSL) based on 'Groovy', which can be used to define a new pipeline as a script. The flow that would typically require many “standard” Jenkins jobs chained together, can be expressed as a single script.

How do I run a Groovy script in Jenkins pipeline?

In Jenkinsfile, simply use load step to load the Groovy script. After the Groovy script is loaded, the functions insides can be used where it can be referenced, as shown above.

How do I import libraries into Jenkins pipeline?

Create a separate git repo for the Jenkins pipeline library & push the shared library code to that repo. Integrate the shared library repo in Jenkins under the Manage Jenkins section. Create Jenkinsfile in the project. In that Jenkinsfile, Import & use the shared library.


1 Answers

The best-supported way to load shared groovy code is through shared libraries.

If you have a shared library like this:

simplest-jenkins-shared-library master % cat src/org/foo/Bar.groovy
package org.foo;

def awesomePrintingFunction() {
  println "hello world"
}

Shove it into source control, configure it in your jenkins job or even globally (this is one of the only things you do through the Jenkins UI when using pipeline), like in this screenshot:

tell jenkins about a shared library

and then use it, for example, like this:

pipeline {
  agent { label 'docker' }
  stages {
    stage('build') {
      steps {
        script {
          @Library('simplest-jenkins-shared-library')
          def bar = new org.foo.Bar()
          bar.awesomePrintingFunction()
        }
      }
    }
  }
}

Output from the console log for this build would of course include:

hello world

There are lots of other ways to write shared libraries (like using classes) and to use them (like defining vars so you can use them in Jenkinsfiles in super-slick ways). You can even load non-groovy files as resources. Check out the shared library docs for these extended use-cases.

like image 168
burnettk Avatar answered Sep 28 '22 07:09

burnettk