0
  • I have a long standing declarative pipeline infrastructure
  • I would like to start putting repeated code into shared libraries

The problem I am facing is calling the git plugin from a shared library function/class. I'm a bit lost as my experience is really only with Jenkins declarative stuff, not the Groovy/Java specifics.

Here is a snippet of the Jenkinsfile, (before using shared library):

pipeline {              
 agent any

 stages {
  stage('Prep Workspace') {
   steps {
    script {
     if ((env.BRANCH_NAME == 'staging') || (env.BRANCH_NAME == 'production')) {
      BRANCH=env.BRANCH_NAME
     } else {
      BRANCH='master'
     }
    }

    echo "||------ Get ProjectOne Dependency ------||"
    dir('deps/ProjectOne') {
     git branch: "${BRANCH}",
     changelog: false,
     credentialsId: 'jenkinsgit',
     poll: false,
     url: 'git@github.com:myprivateorg/ProjectOne.git'
    }

    echo "||------ Get ProjectTwo Dependency ------||"
    dir('deps/ProjectTwo') {
     git branch: "${BRANCH}",
     changelog: false,
     credentialsId: 'jenkinsgit',
     poll: false,
     url: 'git@github.com:myprivateorg/ProjectTwo.git'
    }
   }
  }
 }
}

Note the repeated calls to pull down project files from git repos. The goal here, is to move the repeated code to a shared function call.


I've read the following portion in the manual, on how to use git in shared library:
https://www.jenkins.io/doc/book/pipeline/shared-libraries/#accessing-steps

Using the example in the documentation I've created the shared library file

In src/org/test/gitHelper.groovy:

package org.test;

def checkOutFrom(String repo, String branch='master') {
  echo "||------ CLONING $repo ------||"
  
  git branch: branch, changelog: false, credentialsId: 'jenkinsgit', poll: false, url: "git@github.com:myprivateorg/$repo.git"
}

return this


Then in the Jenkinsfile:

@Library('jenkins-shared-library') _

pipeline {              
 agent any

 stages {
  stage('Prep Workspace') {
   steps {
    script {
     if ((env.BRANCH_NAME == 'staging') || (env.BRANCH_NAME == 'production')) {
      BRANCH=env.BRANCH_NAME
     } else {
      BRANCH='master'
     }

     def g = new org.test.gitHelper()
     g.checkOutFrom('ProjectOne')
     g.checkOutFrom('ProjectTwo')

    }
   }
  }
 }
}

This loads the class and calls the function fine, but fails when it hits git itself:

groovy.lang.MissingPropertyException: No such property: git for class: java.lang.String

I used g.getClass() to confirm it's of type class org.test.gitHelper and NOT java.lang.String so I'm not sure where it's getting that type from.


Please note I have also tried this way:

vars/pullRepo.groovy

def call(String repo, String branch) {
  echo "||------ CLONING $repo ------||"
  dir("deps/$repo") {
    git branch: branch, changelog: false, credentialsId: 'jenkinsgit', poll: false, url: "git@github.com:myprivateorg/$repo.git"
  }
}

Jenkinsfile:

pullRepo('ProjectOne', 'master')

I get the exact same error: groovy.lang.MissingPropertyException: No such property: git for class: java.lang.String

emmdee
  • 1,541
  • 3
  • 25
  • 46

1 Answers1

0

For me, it works to pass the Jenkins context to the shared library like so:

Jenkinsfile:

    pullRepo(this, repo, branch)
vars/pullRepo.groovy:
def call(def context, String repo, String branch) {
  echo "||------ CLONING $repo ------||"
  dir("deps/$repo") {
    context.git branch: branch, changelog: false, credentialsId: 'jenkinsgit', poll: false, url: "git@github.com:myprivateorg/$repo.git"
  }
}

Note that I'm passing the Jenkins context into the context variable, and calling git as a method of the context. You should also be able to do this by passing the context up to your class.